Generate 128-bit Aes Key In Java

Generate 128-bit Aes Key In Java Average ratng: 7,3/10 4448 reviews

AES-256 Encryption with Java and JCEKS. All encryption over 128-bit is restricted if the data is traveling outside of the boarder. Algorithm used to create the key (AES/DES/etc) keysize. Encrypt and decrypt AES key message with RSA key. Patricia Andersen. So to explain what i need to do in this programme, is to create an AES key and a private and public key using RSA algorithm. I then wanna encrypt a msg with the AES key and then encrypt that AES key with the RSA public key. Java file APIs (DOC, XLS, PDF.

Given a message, We would like to encrypt & decrypt plain/cipher text using AES CBC algorithm in java. We will perform following operations:

  • Generate symmetric key using AES-128.
  • Generate initialization vector used for CBC (Cipher Block Chaining).
  • Encrypt message using symmetric key and initialization vector.
  • Decrypt the encrypted message using symmetric key and initialization vector.

Program to encrypt & decrypt message in java:

  • Given encryption key & initialization vector.
  • We will use AES algorithm to encrypt & decrypt input text.
  • CryptoMngr is used generate cipher text from plain text & vice versa.

CryptoMngrClient Class: CryptoMngrClient class will generate random input message and will invoke CryptoMngr to encrypt & decrypt input message.

Program output to encrypt & decrypt to / from plain text is java:

Ranch Hand
posted 4 years ago
I am about to break down and cry and therefor after almost weeks of trying to understand and solve this i now need a little push into the right direction.
So to explain what i need to do in this programme, is to create an AES key and a private and public key using RSA algorithm. I then wanna encrypt a msg with the AES key and then encrypt that AES key with the RSA public key. And in the end decrypt the message with the RSA private key.
I have only managed to encrypt the message with AES , i have also encrypted the AES key with RSA public key but i cant seem to get the decrytion to work, in other words to decrypt that message with the private key. Im not sure how to move forward, im totally stuck.
Any advice? here is the code. Im very new to cryptography
lowercase baba
posted 4 years ago
How do you know it doesn't work? What I mean is, do you get a compiler error? a run time error? Does it throw an exception? Does it run to completion, but the data it decrypted doesn't match what was encrypted?
Help us help you and TellTheDetails.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Ranch Hand
posted 4 years agoAes

fred rosenberger wrote:How do you know it doesn't work? What I mean is, do you get a compiler error? a run time error? Does it throw an exception? Does it run to completion, but the data it decrypted doesn't match what was encrypted?
Help us help you and TellTheDetails.

Java
Oh im sorry, i wasnt clear. Well i get the pop up asi want from JOptionPane message dialog the message encrypted but not decrypted. So what i see is a message saying :: text encrpyted : fuhgudhgug and text decrypted : fhdhgidg
(as an example) . So it doesnt decrypt it with the private key i suspect.
Ranch Hand
posted 4 years ago

fred rosenberger wrote:How do you know it doesn't work? What I mean is, do you get a compiler error? a run time error? Does it throw an exception? Does it run to completion, but the data it decrypted doesn't match what was encrypted?
Help us help you and TellTheDetails.


No errors, beautifully smoothly compiling just not decrypting it at all and it has to be decrypted with the RSA private key so im very stuck on what im doing wrong :/
Marshal
posted 4 years ago
Too difficult for this forum: moving.
Also breaking up the excessively long line).
Ranch Hand
posted 4 years ago

Campbell Ritchie wrote:Too difficult for this forum: moving.
Also breaking up the excessively long line).


Sorry, didnt know
Saloon Keeper
posted 4 years ago
You're encrypting your message using a symmetric key, and then you're never using that encrypted data again. You're only decrypting your symmetric key. You still need to decrypt your message using your decrypted key.
Ranch Hand
posted 4 years ago

Stephan van Hulst wrote:You're encrypting your message using a symmetric key, and then you're never using that encrypted data again. You're only decrypting your symmetric key. You still need to decrypt your message using your decrypted key.


so im only decrypting the AES key not the message and RSA key itself? thanks, need to take a look at it.
Saloon Keeper
posted 4 years ago
Keep in mind that both AES and RSA may use block ciphers that use an initialization vector, so when you initialize a cipher for decryption, you may need to pass it the IV used by the encrypting cipher.
Bartender
posted 4 years ago

Stephan van Hulst wrote:Keep in mind that both AES and RSA may use block ciphers that use an initialization vector, so when you initialize a cipher for decryption, you may need to pass it the IV used by the encrypting cipher.


If RSA is being used to encrypt the AES key then it should use something like PKCS1 padding since that padding introduces a random element. AES used with ECB padding is susceptible to ciphertext forgery and in order to avoid this AES should always be used with one of the feedback modes such as CBC and use a random IV. The random IV does not need to be kept secret and can be passed in the clear along with the AES ciphertext. One approach is to pre-pend the IV to the AES ciphertext. Using this approach one would ship the RSA encrypted AES key followed by the IV followed by the AES cyphertext.
Ranch Hand
posted 4 years ago

Richard Tookey wrote:

Stephan van Hulst wrote:Keep in mind that both AES and RSA may use block ciphers that use an initialization vector, so when you initialize a cipher for decryption, you may need to pass it the IV used by the encrypting cipher.


If RSA is being used to encrypt the AES key then it should use something like PKCS1 padding since that padding introduces a random element. AES used with ECB padding is susceptible to ciphertext forgery and in order to avoid this AES should always be used with one of the feedback modes such as CBC and use a random IV. The random IV does not need to be kept secret and can be passed in the clear along with the AES ciphertext. One approach is to pre-pend the IV to the AES ciphertext. Using this approach one would ship the RSA encrypted AES key followed by the IV followed by the AES cyphertext.
Thanks for advice you guys.. my issue is atm that i do not know where in my code to re-use the encrypted data in order to decrypt it. I just feel lost and confused. I have used the PKCS1 padding thanks to your advice and im not getting that kind of error any longer. I thought padding error had to do with the fact that i was trying to convert byte to string but maybe thats not correct? In any case right now im trying to figure out how to re-use my encrypted string 'InputText1'. Im starting to think that maybe it is complicated to decrypt a string that is not a pre-defined specific word or sentence like lets say 'Hello world', or does it matter? It worked to encrypt so should work to decrypt as well. Sorry ive been working with this for a while and i just feel dizzy lately :P

Generate 128-bit Aes Key In Java Pdf

Bartender
posted 4 years ago
It is not obvious from your code what you are trying to do except that it must be an assignment since in general one needs two programs; one to encrypt the cleartext to create the ciphertext and the other to decrypt the ciphertext to recover the cleartext. As an exercise one can just use one program but use two sections; one to encrypt and one to decrypt.
Preliminary -
Create the RSA public and private keys. The public key will be used in the encryption section and the private key used in the decryption.
Encryption section -
1) Create a random AES key.
2) Encrypt this AES key with the RSA public key. Write the encrypted key it to the output.
3) Create a random IV for use with AES encryption.
4) Write it to the output.
5) Encrypt your cleartext with AES using the random AES key and random IV. Write the result to the output.
Decryption section -
1) Read the encrypted AES key from the input.
2) Decrypt the encrypted AES key using the RSA private key.
3) Read the IV from the input.
4) Using the exracted AES key and extracted IV decrypt the rest of the input. This is the recovered cleartext.
Note 1 - DataOutputStream and DataInputStream are very useful in reading and writing since they allow you to write a set of bytes as a length followed by the bytes.
Note 2 - Since this is an exercise you can chain the DataOutputStream to a ByteArrayOutptuStream if you don't actually want to save the output to a file. You can then use the content of the ByteArrayInput to a ByteArrayInputStream chained to a DataInputStream for use in decryption.
Note 3 - You can get away with using ECB mode in the AES cipher as long as you use a random AES key. You would then ignore the IV requirement.

Java Aes 128

Ranch Hand
posted 4 years ago

Richard Tookey wrote:It is not obvious from your code what you are trying to do except that it must be an assignment since in general one needs two programs; one to encrypt the cleartext to create the ciphertext and the other to decrypt the ciphertext to recover the cleartext. As an exercise one can just use one program but use two sections; one to encrypt and one to decrypt. Mac os mojave download versions.
Preliminary -
Create the RSA public and private keys. The public key will be used in the encryption section and the private key used in the decryption.
Encryption section -
1) Create a random AES key.
2) Encrypt this AES key with the RSA public key. Write the encrypted key it to the output.
3) Create a random IV for use with AES encryption.
4) Write it to the output.
5) Encrypt your cleartext with AES using the random AES key and random IV. Write the result to the output.
Decryption section -
1) Read the encrypted AES key from the input.
2) Decrypt the encrypted AES key using the RSA private key.
3) Read the IV from the input.
4) Using the exracted AES key and extracted IV decrypt the rest of the input. This is the recovered cleartext.
Note 1 - DataOutputStream and DataInputStream are very useful in reading and writing since they allow you to write a set of bytes as a length followed by the bytes.
Note 2 - Since this is an exercise you can chain the DataOutputStream to a ByteArrayOutptuStream if you don't actually want to save the output to a file. You can then use the content of the ByteArrayInput to a ByteArrayInputStream chained to a DataInputStream for use in decryption.
Note 3 - You can get away with using ECB mode in the AES cipher as long as you use a random AES key. You would then ignore the IV requirement.


Yes thank you. Its an assignment but we were supposed to create two programmes but it was ok dto do just one if we managed to solve it that way but come to think of it i think its better to do two. Thank you for your help. Ive been thinking about outputstream encrypting a file and send it that way but didnt thinnk it was necessary in just one programme but maybe its better. Thanks for your advice and help.
Saloon Keeper
posted 4 years ago

Richard Tookey wrote:3) Create a random IV for use with AES encryption.


It's not necessary to do this explicitly. Cipher will generate an IV automatically for algorithms that require one. Just call getIV() on the cipher, and send that.
Bartender
posted 4 years ago

Stephan van Hulst wrote:

Richard Tookey wrote:3) Create a random IV for use with AES encryption.


It's not necessary to do this explicitly. Cipher will generate an IV automatically for algorithms that require one. Just call getIV() on the cipher, and send that.
True. I'm just showing how stale I am.
Greenhorn

Generate 128-bit Aes Key In Java Free

posted 2 years ago
Hi Patrica,
Howdy.
I am having the same issue when doing the Encryp/decrypt with aes/rsa mechanism.
can you please share your sample code of doing it.
thanks in advance.
- marc