Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i used RSA Algorithm to encryption and decryption . i convert to string value to encryption it's working properly. when i convert encryption to decryption that time i get error . below i past my code. please any one help to me
Java
 public final String modulusString = "2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=";
   public final String publicExponentString = "AQAB";
  
Encryption Code :

 public String EncryptionValue(String EncryptionValue) throws Base64DecodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException
    {

        byte[] modulebytes = Base64.decode(modulusString);
        byte[] exponentbytes = Base64.decode(publicExponentString);
       
        BigInteger module = new BigInteger(1,modulebytes);
        BigInteger publicexponent = new BigInteger(1,exponentbytes);
        
            RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(module, publicexponent);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(rsaPubKey);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] plainBytes = EncryptionValue.getBytes("UTF-8");
        byte[] cipherData = cipher.doFinal( plainBytes );
        String encryptedString = Base64.encode(cipherData);
        
        return encryptedString;
    }


Decryption Code :


  public String DecryptionValue(String DecryptionValue) throws Base64DecodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException
    {
        byte[] modulebytes = Base64.decode(modulusString);
        byte[] exponentbytes = Base64.decode(publicExponentString);
        
        BigInteger modulus = new BigInteger(1, modulebytes );
        BigInteger exponent = new BigInteger(1, exponentbytes);

        RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PrivateKey privKey = fact.generatePrivate(rsaPrivKey);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privKey);

        byte[] base64String = Base64.decode(DecryptionValue);
        byte[] plainBytes = new String(base64String).getBytes("UTF-8");
        plainBytes = cipher.update(plainBytes);
        byte[] values = cipher.doFinal(plainBytes);

        return new String(values, "UTF-8");
    }


My Error :

Java
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
	at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
	at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
	at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
	at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
	at javax.crypto.Cipher.doFinal(Cipher.java:2121)
	at cryptocodefinal.CryptoCodeFinal.DecryptionValue(CryptoCodeFinal.java:79)
	at cryptocodefinal.CryptoCodeFinal.main(CryptoCodeFinal.java:148)
Posted
Updated 13-Oct-14 20:32pm
v4

1 solution

I think you'll find that the length of the string being decrypted is wrong.

You need to pad out the data to be the required block size, 256 bytes IIRC, or else you get a bad padding error. The encrypted data is buyte[] and you convert it to String. Try not doing the conversion and you should be able to make things work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900