i trying to write a method to encrypt data using AES 256 but everytimes when i trying to pass the 256bits key to the java cipher it throw Invalid Key Exception, it works only for 128bits key. Below is part of my code
KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(256); SecretKey secret = keygen.generateKey(); System.out.println(secret.getEncoded().length); SecretKeySpec spec = new SecretKeySpec(secret.getEncoded(),"AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, spec);
It show that the key is 32 bytes which is 256bits but when i put the key into cipher.init() it throw invalid key error. I think it is the transformation "AES" which i pass to cipher.getinstance() is wrong. Anyone know what is the correct transformation for 256bits AES encryption?