Jerry Girgich wrote:The next step is to encrypt the symmetric key in Java using the generated public key and I am stuck on this.
Jerry Girgich wrote:I was supposed to write code to read a file and then the next task was to create a symmetric key and to encrypt data using that key. I should be able to then encrypt the key with the public key and then decrypt using the private key.
Jerry Girgich wrote: I should be able to then encrypt the key with the public key and then decrypt using the private key.
Stephan van Hulst wrote:Yes, you can, but I can only think of a few reasons why you would want to do this. Is this an exercise?
Henry Wong wrote:... and back to the original question...
Jerry Girgich wrote: I should be able to then encrypt the key with the public key and then decrypt using the private key.
The code to encrypt and decrypt the symmetric key, is similar to the code that used the symmetric key to encrypt and decrypt the payload. Except, of course, the configuration setup is different, as it is a different encryption/decryption algorithm.
What issue are you running into?
Henry
Jerry Girgich wrote:
Basically I can't figure out how to extract the public key from the certificate to use. I figured how to encrypt but I can't figure out the best way to use that public key.
Stephan van Hulst wrote:You can't generate a public key for AES, because AES is a symmetric encryption algorithm, which requires a secret key, not a public/private key pair.
Jerry Girgich wrote:
Never mind, I meant that I generated the symmetric key using the AES algorithm
Jerry Girgich wrote:Can I still encrypt the symmetric key with the extracted public key that I get from the above code that I used?
Henry Wong wrote:
The code to encrypt and decrypt the symmetric key, is similar to the code that used the symmetric key to encrypt and decrypt the payload. Except, of course, the configuration setup is different, as it is a different encryption/decryption algorithm.
Jerry Girgich wrote:Exception in thread "main" java.security.InvalidKeyException: Invalid AES key length: 294 bytes
at com.sun.crypto.provider.AESCipher.engineGetKeySize(AESCipher.java:509)
at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1067)
Stephan van Hulst wrote:If that's from the code you use to encrypt the symmetric key, it's logical that you get an exception because you're using AES to perform encryption. I thought you wanted to use asymmetric encryption of your symmetric key? Then you need to use an asymmetric algorithm like RSA.
Jerry Girgich wrote:
No I want to use AES to perform encryption. Sorry if this is coming out confusing, this is all new to me and I am learning as a go along.
Jerry Girgich wrote:Also isn't the symmetric key the same thing as a secret key?
Jerry Girgich wrote:I'm not quite understanding where I am going wrong.
Stephan van Hulst wrote:Well yeah, you're using the same Cipher to encrypt your message and your symmetric key. So both times you're using AES. AES is a symmetric algorithm, so you can't use a public key with it.
Now, once and for all, what do you want to do with your symmetric key after you've used it to encrypt your message:
A) encrypt it asymmetrically with a public key, using an algorithm such as RSA, or
B) encrypt it symmetrically with a secret key, using an algorithm such as AES?
Jerry Girgich wrote:
I want to encrypt it asymmetrically with a public key.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn KlassenKoop
https://coderanch.com/wiki/718759/books/BuildingWorldBackyardPaulWheaton
