【问题标题】:RSA private Key decryption giving "InvalidCipherTextException" using bouncy castleRSA私钥解密使用充气城堡给出“InvalidCipherTextException”
【发布时间】:2015-08-09 09:28:09
【问题描述】:

我正在为 j2me 创建一个使用充气城堡的加密/解密方法。我已经使用 openssl 和硬编码在代码中创建了私钥/公钥。加密工作正常(我得到加密字符串),但是当我尝试解密该密文时,我得到以下异常

org.bouncycastle.crypto.InvalidCipherTextException: unknown block type
 - org.bouncycastle.crypto.encodings.PKCS1Encoding.decodeBlock(PKCS1Encoding.java:362)
 - org.bouncycastle.crypto.encodings.PKCS1Encoding.processBlock(PKCS1Encoding.java:166)
 - com.ust.CryptoJ2me.RSADecrypt(CryptoJ2me.java:564)

PFB代码sn -p

public byte[] RSAEncrypt(byte[] toEncrypt) throws Exception {
    String cipherStr;
    String modStr ="AA878F0D9........";
    String expStr = "10001";
    BigInteger modulus = new BigInteger(modStr,16);
    BigInteger exponent = new BigInteger(expStr,16);
    System.out.println("modulus = "+modulus+"// exponent = "+exponent);
    RSApubKey = new RSAKeyParameters(false,modulus,exponent);

    if (RSApubKey == null)
        throw new Exception("Generate RSA keys first!");

    AsymmetricBlockCipher eng = new RSAEngine();
    eng = new PKCS1Encoding(eng);
    eng.init(true, RSApubKey);
    byte[] cipherByte = eng.processBlock(toEncrypt, 0, toEncrypt.length);
    return cipherByte;//cipherStr;
}

public String RSADecrypt (byte[] toDecrypt) throws Exception {
    System.out.println("toDecrypt = "+toDecrypt);
    //byte[] toDecByte = toDecrypt.getBytes("UTF-8");
    //System.out.println("toDecByte ="+toDecByte);
    String plainText;
    BigInteger RSAmod = new BigInteger("00d1aec38b8d189a0a1..",16);
    BigInteger RSAprivExp  = new BigInteger("2d7af1b1283688dadc16..",16);
    BigInteger RSApubExp = new BigInteger("10001",16);
    BigInteger RSAdp = new BigInteger("00f5847cc67ea018f10f16..",16);
    BigInteger RSAdq = new BigInteger("00daa299bf356c6c6db6a21..",16);
    BigInteger RSAp = new BigInteger("00e28dd601e878dd6b1c0c..",16);
    BigInteger RSAq  = new BigInteger("400ff2e2df018507e4c2be6..",16);
    BigInteger RSAqInv   = new BigInteger("00cf4b2ba101efb2378aee..",16);
    RSAPrivateCrtKeyParameters RSAprivKey = new RSAPrivateCrtKeyParameters(RSAmod, RSApubExp,
            RSAprivExp, RSAp, RSAq, RSAdp, RSAdq, RSAqInv);

  AsymmetricBlockCipher eng = new RSAEngine();

  eng = new PKCS1Encoding(eng);

  eng.init(false, RSAprivKey);
  byte[] plainByte = eng.processBlock(toDecrypt, 0, toDecrypt.length);
  plainText = new String(plainByte);

  return plainText;
}

【问题讨论】:

    标签: java-me rsa bouncycastle


    【解决方案1】:

    公钥和私钥的模数不匹配。它应该匹配。私钥模数可能更小(假设大端符号和相同长度的十六进制字符串),这就是您收到此错误消息的原因。由于私钥包含模数和公共指数分量,您可以在加密代码中替换它们,看看解密是否适用于新的密文。

    RSA 的工作原理是将某些消息提高到 e(公共指数)模m 的幂。这导致密文小于m,但接近它。解密通过将密文提高到 d(私有指数)模 m(相同的模数)的幂。

    如果私钥模数远小于公钥模数,则密文会更大,无法解密。从数学上讲,可以“解密”密文,但您不会取回原始明文。图书馆可能会就此发出警告。

    【讨论】:

    • 这意味着我必须为加密和解密提供相同的模数?
    • 是的,不同的是公共指数和私人指数(e 和 d)。您的私钥数据包含公钥所需的每个组件。只需更改公钥,看看它是否有效。
    • 我使用以下 open ssl 命令获取私钥数据 - openssl rsa -inform PEM -text -noout pubkey。文本文件。在那个文本文件中我得到:模数,publicExponent,privateExponent,prime1,prime2,exponent1,exponent2,系数。我用来解密的那些东西
    • 是的,私有组件之间可能是一致的,但是公钥与私钥不对应。
    • 让我清除它。我遵循的步骤。这样我就可以轻松找出问题所在。 1) [ openssl genrsa -out private_key.pem 1024 ] -> 获取私钥。 2) next [ openssl rsa -inform PEM -text -noout pvtcom ponent.txt ] -> 获取私有组件。3) next [ openssl rsa -inform PEM -modulus -noout mod ulus.txt ] -> 获取公钥模数。 4)十六进制(模数)值从我用于加密的第 3 次 seto 中获得。 5)我用于解密的私人组件。如果我在任何地方错了,请纠正我
    猜你喜欢
    • 2013-05-25
    • 2015-06-29
    • 2016-08-10
    • 2015-06-07
    • 2017-08-21
    • 2014-09-19
    • 1970-01-01
    • 2011-06-11
    • 2015-04-26
    相关资源
    最近更新 更多