tommed
tommed

Reputation: 1561

Can't get AES cipher working on Android

I can't get a basic AES encryption/decryption round-tripping unit test to pass on Android. Probably something I'm doing, but would greatly appreciate the guidance:

public class EncryptionManager {
    private static final byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    private static final byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

    public byte[] encrypt(byte[] plaintext) throws Exception {
        Cipher cipher = getCipher(true);
        return cipher.doFinal(plaintext);
    }

    public byte[] decrypt(byte [] ciphertext) throws Exception {
        Cipher cipher = getCipher(false);
        return cipher.doFinal(ciphertext);
    }

    private static Cipher getCipher(boolean encrypt) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE,  
                    secretKeySpec, ivParameterSpec);
        return cipher;
    }
}

The failing unit test looks like this:

public class EncryptionManagerTest extends TestCase {
     public void testShouldPbeRoundtrip() throws Exception {
         String unencryptedStr = "foobargum";
         byte[] unencrypted = unencryptedStr.getBytes();
         EncryptionManager encryptionManager = new EncryptionManager();
         byte[] encrypted = encryptionManager.encrypt(unencrypted);
         String encryptedStr = new String(encrypted);
         byte[] decrypted = encryptionManager.decrypt(encrypted);
         String decryptedStr = new String(encrypted);

         // assert
         assertFalse("encryption not done",      
                unencryptedStr.equalsIgnoreCase(encryptedStr));
         assertEquals("decryption didn't work", unencryptedStr, 
                decryptedStr);
     }
 }

Error: "decryption didn't work expected:<[foobargum]> but was:<[�3���jw� �|*�]>"

Upvotes: 0

Views: 282

Answers (2)

President James K. Polk
President James K. Polk

Reputation: 42009

String encryptedStr = new String(encrypted); is a mistake. Encryption produces byte sequences that are not likely to be a valid String for a given charset, in particular not for the UTF-8 charset that is the android default. In converting the byte array encrypted to a String object invalid byte sequences are silently replaced with valid characters in an unrecoverable manner.

Upvotes: 3

user1675642
user1675642

Reputation: 777

I think you have a copy-paste error:

String decryptedStr = new String(encrypted);

Upvotes: 2

Related Questions