I am not familiar AES-256 encryption/decryption. I read some tutorial which generate a AES-256 key with following code:
public static SecretKey getAESKey() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256, SecureRandom.getInstanceStrong());
return keyGen.generateKey();
}
I want to share that key with client, but when I convert this key into string:
String secretKeyString = Base64.getEncoder().encodeToString(secretKey.getEncoded());
System.out.println("generated key = "+secretKeyString)
it will generate output: KDAgcwjZ2OWwBLgvZtbYwIJ1F8LqABuCPclJhiYfIwA=
This is not a valid 256 bits key. When I try to decrypt the encrypted text using an online tool with that key, it gives an error that key size is not 256 bits. How can I get the key from SecretKey?