RSA and AES Decrypt and Encrypt problem

Put your problem here if it does not fit any of the other categories.

RSA and AES Decrypt and Encrypt problem

Postby alinescoo » Tue Sep 14, 2010 7:48 am

I have generated on my android application a pair of RSA Keys.

I receive from a web service
- an AES Key, encrypted with my RSA public key
- a String encoded with the AES key.

So I must do the following:
- decrypt the AES Key
- decrypt the string with the obtained AES Key.

To generate the RSA Keys I did:
Code: Select all
     keyGen = KeyPairGenerator.getInstance("RSA");
      keypair = keyGen.genKeyPair();
      privateKey = keypair.getPrivate();
      publicKey = keypair.getPublic();

On RSA decrypt I use :

public static byte[] decryptRSA( PrivateKey key, byte[] text) throws Exception
byte[] dectyptedText = null;

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text);
return dectyptedText;

On AES decrypt I use:

Code: Select all
    public static byte[] decryptAES(byte[] key, byte[] text) throws Exception {   
               SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");   
               Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");   
               cipher.init(Cipher.DECRYPT_MODE, skeySpec);   
               byte[] decrypted = cipher.doFinal(text);   
               return decrypted;   

So, in my code, to obtain the decrypted AES Key I do
Code: Select all
    byte[] decryptedAESKey = sm.decryptRSA(key, Base64.decode(ReceivedBase64EncryptedAESKey));
    byte[] decryptedString = sm.decryptAES(decryptedAESKey, Base64.decode(ReceivedEncryptedAESString));

From the web service I get the ReceivedEncryptedAESString= qjYcPCHkFpbTjntDCpXCGabSY5DFH... and the decryptedAESKey looks like:[121, 98, 51, 100, 108, 112, 118, 107, 118, 97]

The problem appears to be on cipher.init(Cipher.DECRYPT_MODE, skeySpec); as it throws an exception: InvalidKeyException: Key length not 128/192/256 bits. Maybe I do RSA decryption wrong ?
Junior Developer
Junior Developer
Posts: 24
Joined: Fri Aug 20, 2010 11:08 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 12 guests