views:

738

answers:

3

Hi, I am developing a BlackBerry application where the server is in Java. Whenever AES encrypted data is sent to server along with PKCS5 Formatting, The server gets bad padding exception while decrypting it.

Please help.

Badly stuck in this issue

thanks.

+1  A: 

Make sure you're doing the padding etc in the right order first: pad then encrypt, decrypt then unpad.

  • Check the transmitted data length and make sure it's a multiple of blocksize.

  • Make sure blocksize is consistent in all the calls.

  • Make sure your crypto provider settings are exactly matching.

Charlie Martin
+1  A: 

I am using AES/ECB/PKCS5Padding in java. But BlackBerry requires onle AES encryptor engine and PKCS5Formatter engine. ECB cipher mode is default in blackberry.

So can you please guide me to achieve AES/ECB/PKCS5Padding in Blackberry.

/* * BasicCryptoDeveloperLab.java * */

package com.rim.samples.crypto; import java.io.; import net.rim.device.api.crypto.; import net.rim.device.api.util.*;

public class BasicCryptoDeveloperLab {

public static void main( String[] args )
{
    try {
        // Create the data that you want to encrypt.
        String message = "Welcome to the RIM Crypto API.";
        byte[] data = message.getBytes();

        byte[] keyData = "Create AES Key".getBytes();

        // Encrypt the data using
        byte[] ciphertext = encrypt( keyData, data );

        // Decrypt the data.
        byte[] plaintext = decrypt( keyData, ciphertext );

        String message2 = new String( plaintext );

        if( message.equals( message2 )) {
            // The encryption/decryption operation worked as expected.
            System.out.println( "Congratulations! You just encrypted and decrypted data." );
        } else {
            System.out.println( "Oops. The decrypted message should equal the original.
                Check your code." );
        }
    } catch( CryptoException e ) {
        System.out.println("An unexpected exception occurred.
        Please verify your work or ask for help.");
    } catch( IOException e ) {
        System.out.println("An unexpected exception occurred.
        Please verify your work or ask for help.");
    }
}

private static byte[] encrypt( byte[] keyData, byte[] data )
    throws CryptoException, IOException
{
    // Create the AES key to use for encrypting the data.
    // This will create an AES key using as much of the keyData
    // as possible.
    AESKey key = new AESKey( keyData );

    AESEncryptorEngine engine = new AESEncryptorEngine( key );
    PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    BlockEncryptor encryptor = new BlockEncryptor( fengine, output );

    encryptor.write( data );
    encryptor.close();
    output.close();


    return output.toByteArray();
}

private static byte[] decrypt( byte[] keyData, byte[] ciphertext )
    throws CryptoException, IOException
{
            AESKey key = new AESKey( keyData );

            AESDecryptorEngine engine = new AESDecryptorEngine( key );

    PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine( engine );
            ByteArrayInputStream input = new ByteArrayInputStream( ciphertext );
    BlockDecryptor decryptor = new BlockDecryptor( uengine, input );


    byte[] temp = new byte[ 100 ];
    DataBuffer buffer = new DataBuffer();

    for( ;; ) {
        int bytesRead = decryptor.read( temp );
        buffer.write( temp, 0, bytesRead );

        if( bytesRead < 100 ) {
            // We ran out of data.
            break;
        }
    }

    byte[] plaintextAndHash = buffer.getArray();


    return plaintextAndHash ;
}

}

Thanks Deepak

+1  A: 

Is that the Blackberry or the server code? Have you tried using the standard JCE classes? something like this:

Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec key = new SecretKeySpec(yourKeyBytes, "AES");
aes.init(Cipher.DECRYPT_MODE, key);
byte[] cleartext = aes.update(ciphertext, 0, ciphertext.length);

Chochos