PKCS#1 的 RSA 解密错误:javax.crypto.IllegalBlockSizeException:数据长



我在尝试解密消息时遇到了这个问题。

错误

An exception or error caused a run to abort: Data must not be longer than 256 bytes 
javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes

我的代码如下。

package com.smth.what.api
import java.security.spec.X509EncodedKeySpec
import java.security.{KeyFactory, PrivateKey, PublicKey}
import javax.crypto.Cipher
import org.apache.commons.codec.binary.Base64
object Encryptor {
  private val publicKeyString: String = System.getenv("PUB_KEY")
  private val privateKeyString: String = System.getenv("PRIV_KEY")
  private val publicKey = readPemPublicKey(publicKeyString)
  private val privateKey = readPemPrivateKey(privateKeyString)
  private def readPemPublicKey(publicKey: String): PublicKey = {
    val pemPublicKey = publicKey.replace("n", "")
      .replace("-----BEGIN PUBLIC KEY-----", "")
      .replace("-----END PUBLIC KEY-----", "")
      .replace(" ", "")
    val publicKeyBytes: Array[Byte] = Base64.decodeBase64(pemPublicKey)
    val publicKeySpec = new X509EncodedKeySpec(publicKeyBytes)
    val keyFactory = KeyFactory.getInstance("RSA")
    keyFactory.generatePublic(publicKeySpec)
  }
  private def readPemPrivateKey (privateKey: String): PrivateKey = {
    val pemPrivateKey = privateKey.replace("n", "")
      .replace("-----BEGIN RSA PRIVATE KEY-----", "")
      .replace("-----END RSA PRIVATE KEY-----", "")
      .replace(" ", "")
    val privateKeyBytes: Array[Byte] = Base64.decodeBase64(pemPrivateKey)
    val keyFactory = KeyFactory.getInstance("RSA")
    import java.security.spec.RSAPrivateCrtKeySpec
    import sun.security.util.DerInputStream
    val derReader = new DerInputStream(privateKeyBytes)
    val seq = derReader.getSequence(0)
    val modulus = seq(1).getBigInteger
    val publicExp = seq(2).getBigInteger
    val privateExp = seq(3).getBigInteger
    val prime1 = seq(4).getBigInteger
    val prime2 = seq(5).getBigInteger
    val exp1 = seq(6).getBigInteger
    val exp2 = seq(7).getBigInteger
    val crtCoef = seq(8).getBigInteger
    val keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef)
    keyFactory.generatePrivate(keySpec)
  }
  def encrypt(inputString: String, key: PublicKey = publicKey): String = {
    val cipher: Cipher = Cipher.getInstance("RSA")
    cipher.init(Cipher.ENCRYPT_MODE, key)
    new String(cipher.doFinal(inputString.getBytes("UTF-8")))
  }
  def decrypt(inputString: String, key: PrivateKey = privateKey): String = {
    val cipher: Cipher = Cipher.getInstance("RSA")
    cipher.init(Cipher.DECRYPT_MODE, key)
    val inputStringBytes = inputString.getBytes("UTF-8")
    new String(cipher.doFinal(inputStringBytes))
  }
}

我的密钥大小为 2048 。它是通过openssl genrsa生成的,输出.pem

我使用 IntelliJ 的环境变量来提供公钥和私钥(通过编辑配置,复制和粘贴它们(。

错误来自此行 new String(cipher.doFinal(inputStringBytes)) decrypt函数。

我一直在阅读一些stackoverflow的帖子(例如这篇(,但是,我仍然不明白发生了什么。

如果可能的话,我想要一个非常基本的解释,因为加密/解密对我来说是一个新的领域。

尝试不同的事情后,有效的方法是将Base64编码应用于加密邮件,然后在解密加密邮件时使用Base64解码。这与@James K Polk评论所说的话一致。

查看更新的代码(仅适用于encryptdecrypt函数,因为其余部分保持不变(

  def encrypt(inputString: String, key: PublicKey = publicKey): String = {
    cipher.init(Cipher.ENCRYPT_MODE, key)
    val encrypted: Array[Byte] = cipher.doFinal(inputString.getBytes())
    Base64.encodeBase64String(encrypted)
  }
  def decrypt(inputString: String, key: PrivateKey = privateKey): String = {
    cipher.init(Cipher.DECRYPT_MODE, key)
    val decodedInputString: Array[Byte] = Base64.decodeBase64(inputString)
    val decrypted: Array[Byte] = cipher.doFinal(decodedInputString)
    new String(decrypted)
  }

最新更新