I have the following BASH script to encrypt and decrypt a string given a password.
$ echo 'some-data' | openssl enc -base64 -e -aes-256-cbc -nosalt -pass pass:some-password
luYWG/C5uryBVtPHilO6Pg==
$ echo 'luYWG/C5uryBVtPHilO6Pg==' | openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:some-password
some-data
As you can see I'm able to enter some-password to encrypt and decrypt another string using openssl and AES-256
The problem came when I want to replicate this in Java using java.crypto because some-password don't complies the Key requirements apparently, I have this code:
public static String decrypt(String data, String secret)
throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secret.getBytes(), "AES"));
return new String(cipher.doFinal(Base64.getDecoder().decode(data)));
}
When I run it with those same inputs I get the following exception
java.security.InvalidKeyException: Invalid AES key length: ...
Is there a way that I can make it work? I'm guessing that openssl is able to take my desired string password and convert it to a valid one