I have AES encryption and decryption code in Java and Python. While both of them are able to perform encryption / decryption correctly, the encrypted output from both is different.
Java code:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class encdec {
public static void main(String[] args) throws Exception {
String iv = "1234567890123456";
String key = "abcdefghijklmnop";
String text = "simple text";
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher c1 = Cipher.getInstance("AES/CFB/NoPadding");
c1.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encrypted = c1.doFinal(text.getBytes());
System.out.println(toHex(encrypted));
Cipher c2 = Cipher.getInstance("AES/CFB/NoPadding");
c2.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decrypted = c2.doFinal(encrypted);
System.out.println(toHex(decrypted));
System.out.println(new String(decrypted));
}
// https://stackoverflow.com/a/1040876
private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
public static String toHex(byte[] bytes)
{
char[] c = new char[bytes.length*2];
int index = 0;
for (byte b : bytes)
{
c[index++] = HEX_DIGITS[(b >> 4) & 0xf];
c[index++] = HEX_DIGITS[b & 0xf];
}
return new String(c);
}
}
Output:
4013da03a22e39a5468e3b
73696d706c652074657874
simple text
Python code:
from Crypto.Cipher import AES
iv = "1234567890123456"
key = "abcdefghijklmnop"
text = "simple text"
c1 = AES.new(key, AES.MODE_CFB, iv)
encrypted = c1.encrypt(text)
print "".join("{:02x}".format(ord(c)) for c in encrypted)
c2 = AES.new(key, AES.MODE_CFB, iv)
decrypted = c2.decrypt(encrypted)
print "".join("{:02x}".format(ord(c)) for c in decrypted)
print decrypted
Output:
4098fc420c85fddb2f1909 <-- Encrypted output differs
73696d706c652074657874
simple text
This makes it difficult to cross encrypted data between them.
I'm using the same configuration and keys on both sides, but I must be missing something.