convert byte[] to AES key
java
Solution
I believe you're receiving an RSA-encrypted AES key along with some AES-encrypted data, and you still need to perform the second of 2 encryptions. Right?
So, anyway, you can load a key from the byte array.
SecretKeySpec secretKeySpec = new SecretKeySpec(decryptedText, "AES");
Subsequently you'd do something like this, to decrypt the AES-encrypted data, 'encrypted':
Cipher cipherAes = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipherAes.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipherAes.doFinal(encrypted);
String decryptedString = new String(decryptedBytes);
The `/CBC/PKCS7Padding` specification may vary, depending on how it was specified during encryption.
Hope this helps.
Problem
i have a AESkey which encrypted by a public key, and later decrypted by a private key ``` Cipher cipher = Cipher.getInstance("RSA"); PrivateKey privateKey = keyPair.getPrivate(); // decrypt the ciphertext using the private key cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedText = cipher.doFinal(theBytes); ``` theBytes is a byte[] containing a encrypted AESkey, the question is how to convert the decryptedText back to the AESkey?