PKCS#1的RSA解密错误:javax.crypto.IllegalBlockSizeException:数据不得超过256个字节

时间:2019-07-23 07:51:10

标签: java scala encryption private-key public-key

尝试解密邮件时遇到此问题。

错误

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的帖子(例如one),但是,我仍然不明白发生了什么。

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

1 个答案:

答案 0 :(得分:1)

尝试不同的方法后,有效的方法是对加密的邮件应用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)
  }