I'm trying to do AES encryption/decryption in my Android app. I've tried libs like Encryption, java-aes-crypto and implementations described in
- Data Encryption and Decryption in Android;
- Securely store user credentials ( which throws a last byte exception );
- AES Encryption Decryption in Android
No matter how approach I use the decrypt part is different from original data. I always have a result like this:
03-09 21:58:33.457 30329-30329/org.androidapp.test E/ERROR: BEFORE: sDuKOoRteaEUFtA3P0SllSTCpgKJN75FuyPLxdp/ctM=
03-09 21:58:33.459 30329-30329/org.androidapp.test E/ERROR: AFTER: PBSqM3jHZhemw48wd44pKg==
A simple example of what I'm doing now:
    private static byte[] seedValue = {
        0x2d, 0x2a, 0x2d, 0x42, 0x55, 0x49, 0x4c, 0x44, 0x41, 0x43, 0x4f, 0x44, 0x45, 0x2d, 0x2a, 0x2d
};
private static String ALGORITHM = "AES";
private static SecretKeySpec secretKey = new SecretKeySpec(seedValue, "AES");
public static String encrypt( String data ) throws Exception {
    try {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherText = cipher.doFinal(data.getBytes("UTF8"));
        String encryptedString = new String(Base64.encode(cipherText ,Base64.DEFAULT ) );
        return encryptedString;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
public static String decrypt(String data) throws Exception {
    try {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] cipherText = Base64.decode(data.getBytes("UTF8"), Base64.DEFAULT);
        String decryptedString = new String(cipher.doFinal(cipherText),"UTF-8");
        return decryptedString;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
A sample of using these methods (using Jackson for read and write objects):
public static void writeSecurityInfo( String fileName, POJO pojo ){
        try{
            FileUtil.verifyFile( fileName );
            ObjectMapper mapper = new ObjectMapper();
            File file = new File( Environment.getExternalStorageDirectory(), fileName );
            try {
                pojo.setName( CryptUtils.encrypt( pojo.getName() ) );
                pojo.setAddress( CryptUtils.encrypt( pojo.getAddress() ) );
            }
            catch( Exception e ){
                e.printStackTrace();
            }
            ObjectWriter writer = mapper.writer( new DefaultPrettyPrinter() );
            writer.writeValue( file, securityPOJO );
        }
        catch( IOException e ){
            e.printStackTrace();
        }
    }
And for reading:
public static POJO readSecurityInfo( String fileName ){
        try {
            File file = new File( Environment.getExternalStorageDirectory(), fileName );
            ObjectMapper mapper = new ObjectMapper();
            InputStream inputStream = new FileInputStream( file );
            POJO pojo = mapper.readValue( inputStream, POJO.class );
            Log.e("ERROR", "BEFORE: "+ pojo.getName());
            securityPOJO.setName( CryptUtils.decrypt( pojo.getName() ) );
            Log.e("ERROR", "AFTER: "+ pojo.getName());
            pojo.setAddress( CryptUtils.decrypt( pojo.getAddress() ) );
            return pojo;
        }
        catch( Exception e ){
            e.printStackTrace();
        }
        return null;
    }
No success. Am I doing something wrong when using AES?
 
     
     
    