I am trying to encrypt some data in a Python program and save it, and then decrypt this data in a Java program. In Python, I encrypt it like this:
from Crypto.Cipher import AES KEY = '12345678901234567890123456789012' def encrypt(data): cipher = AES.new(KEY, AES.MODE_CFB) return cipher.encrypt(data)
And in Java, I decrypt it like this:
import java.security.*; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; public class Encryption { private static byte[] KEY = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2' }; public static byte[] decrypt(byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { Cipher c = Cipher.getInstance("AES/CFB/NoPadding"); Key key = new SecretKeySpec(KEY, "AES"); c.init(Cipher.DECRYPT_MODE, key); return c.doFinal(data); } }
But I get Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters . Itβs clear that I am doing something wrong. But what?