2 years ago

#53026

test-img

StackOverflow Asker

Getting decryption error when decrypting the data string which is encrypted with public key

I have a encrypted private key which I have to decrypt using Shared key,which I did and got byte[]. I have converted the byte[] to utf8 string and then converted that string to Private key. Now, I have to use this private key to decrypt one time encrypted public key. While doing that, I am getting the following error.

javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:370) ~[na:1.8.0_311]

This is how I generated private key.

 public static PrivateKey decrypt(String stringKey) throws Exception {
    JSONObject jsonKey = new JSONObject(stringKey);

    BigInteger e = new BigInteger( jsonKey.getString("e")); // Public exponent
    BigInteger n = new BigInteger(jsonKey.getString("n").getBytes());
    BigInteger d = new BigInteger(jsonKey.getString("d").getBytes());
    BigInteger p = new BigInteger(jsonKey.getString("p").getBytes());
    BigInteger q = new BigInteger(jsonKey.getString("q").getBytes());
    BigInteger dmp1 = new BigInteger(jsonKey.getString("dmp1").getBytes());
    BigInteger dmq1 = new BigInteger(jsonKey.getString("dmq1").getBytes());
    BigInteger coeff = new BigInteger(jsonKey.getString("coeff").getBytes());


    RSAPrivateCrtKeySpec privateKeySpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, dmp1, dmq1, coeff);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
     PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return  privateKey;
}

I tried many solutions from the Stack Overflow but nothing worked. I am getting my private key as 2046 bits but when I looked online I found out that it should be 1024 or 2048 bits. Am I generating my Private key wrong? If it is wrong, how should I add two bits to it to make it 2048 bits.

Thanks in advance.

java

spring-boot

aes

rsa

private-key

0 Answers

Your Answer

Accepted video resources