RSA私钥解密使用充气城堡给出“InvalidCipherTextException”

时间:2021-08-23 18:30:09

I am creating a encryption/decryption method using bouncy castle for j2me. I have created private / public keys using openssl and hard coding into the code. Encryption is working fine (I am getting encrypted string), but when I try decryption for that ciphertext I am getting below exception

我正在为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 the code snippet

PFB代码片段

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;
}

1 个解决方案

#1


3  

The modulus doesn't match between public key and private key. It should match. The private key modulus is probably smaller (assuming big endian notation and same length hex strings) and that is why you're getting this error message. Since the private key contains the modulus and public exponent components, you can replace them in the encryption code and see if the decryption works with a new ciphertext.

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

RSA works by raising some message to a power e (public exponent) modulo m. This results in ciphertexts that are smaller than m, but close to it. The decryption works by raising the ciphertext to the power of d (private exponent) modulo m (the same modulus).

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

If the private key modulus is much smaller than the public key modulus, the ciphertexts will be bigger and it cannot be decrypted. Mathematically it is possible to "decrypt" the ciphertexts, but you won't get your original plaintext back. The library probably warns you about this.

如果私钥模数远小于公钥模数,则密文将更大并且不能被解密。数学上可以“解密”密文,但是你不会得到原来的明文。图书馆可能会警告你这件事。

#1


3  

The modulus doesn't match between public key and private key. It should match. The private key modulus is probably smaller (assuming big endian notation and same length hex strings) and that is why you're getting this error message. Since the private key contains the modulus and public exponent components, you can replace them in the encryption code and see if the decryption works with a new ciphertext.

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

RSA works by raising some message to a power e (public exponent) modulo m. This results in ciphertexts that are smaller than m, but close to it. The decryption works by raising the ciphertext to the power of d (private exponent) modulo m (the same modulus).

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

If the private key modulus is much smaller than the public key modulus, the ciphertexts will be bigger and it cannot be decrypted. Mathematically it is possible to "decrypt" the ciphertexts, but you won't get your original plaintext back. The library probably warns you about this.

如果私钥模数远小于公钥模数,则密文将更大并且不能被解密。数学上可以“解密”密文,但是你不会得到原来的明文。图书馆可能会警告你这件事。