0

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.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Urmil Parikh
  • 323
  • 3
  • 11
  • Do these programs use the same encodings? – Sv Sv Jul 15 '17 at 15:33
  • I think so. Otherwise length of encrypted data would be different, isn't it? – Urmil Parikh Jul 15 '17 at 15:41
  • Its not obligatory – Sv Sv Jul 15 '17 at 15:43
  • Oh yes. Exactly the same. I wonder how I could not find it. Thanks, @ArtjomB. – Urmil Parikh Jul 15 '17 at 15:48
  • 1
    The IV must be different (read: random) for each encryption with the same key. Don't use a static IV, because that makes the cipher deterministic and permits the attacker to deduce the plaintexts if they observed multiple ciphertexts. This is called the many-time pad (or [two-time pad](https://twitter.com/angealbertini/status/425561082841690112/photo/1)). The IV is not secret, so you can send it along with the ciphertext. Usually, it is simply prepended to the ciphertext and sliced off before decryption. – Artjom B. Jul 15 '17 at 15:48

0 Answers0