I want to do aes-128-cbc encryption in JAVA and Linux, but it keeps giving me different results. For example I want to decode string "my.txt". In Linux I do it in this way:
echo -n my.txt | openssl aes-128-cbc -K 6f838655d1bd6312b224d3d1c8de4fe1 -iv 9027ce06e06dbc8b -a
I also encode it to base64 and it's giving me this result: 86M5fwdUpQ3tbFrz0ddHJw==
In Java I use this method:
public static String encrypt(String key, String initVector, String value) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
System.out.println("encrypted string: "
+ Base64.encodeToString(encrypted, Base64.DEFAULT));
return Base64.encodeToString(encrypted, Base64.DEFAULT);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
And with same data it gives me completely different result: vgk6yxCrQ5iLFvHxMtQO7w==
I also tried to use aes-256-cbc with 32-symbol length iv. In Linux I use aes-256-cbc and in Android I use Spongy Castle library for this purpose, but it give different results too.
What I did wrong? Or maybe you have suggestion to choose different cross-platform algorithm to encryption.