my dog learned polymorphism*
The moose likes Security and the fly likes How to use AES DECRYPT_MODE? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "How to use AES DECRYPT_MODE?" Watch "How to use AES DECRYPT_MODE?" New topic
Author

How to use AES DECRYPT_MODE?

Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Hello Everybody,,

I have a code of AES encryption and decryption and it works fine for both.

When I separeate the code of AES into two codes, one for encrypt and another for decrypt.
The code of encryption works fine.

I saved output of encryption as String in a variable.
Also I saved key into a variable.

Now, the decryption code works, But it gives me no output(null)!

Here is the code of decryption:


I don't know why it gives me no output!!!

any help please!
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
I get the following error when I run the code:

javax.crypto.BadPaddingException: Given final block not properly padded
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
The problem most likely is in the skewed encrypted data, show us also code you are using for encryption.
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Here is the full code that works fine for both encryption and decryption:

(*note: instead of using auto generation of key I ask user for a password and I insert it into md5. I take half of hash value and use it as 128bit key for encryption).




here as one code it works fine!
but, when I save the encrypted text and try again to decrypt it it tell me the previous error that I mentioned!

any help please?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Question too difficult for “beginning”. Moving thread.
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
I got a solution:

I saved the ciphertext as hexadecimal!

Then at decryption I convert hexadecimal back to byte array and decrypt it,
and it works fine!

So, do you think the problem is I cannot save encrypted text as String??!!
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Campbell Ritchie wrote:Question too difficult for “beginning”. Moving thread.


Excuse me, to which fourm did you move it??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

Sultan Altoobi wrote:
So, do you think the problem is I cannot save encrypted text as String??!!


Encrypted data is binary data. It can't be safely stored into a String, as strings expect unicode.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

Sultan Altoobi wrote:
Campbell Ritchie wrote:Question too difficult for “beginning”. Moving thread.


Excuse me, to which fourm did you move it??


It looks like it was moved to the security forum -- see top. Regardless, you'll have a link in the beginners forum for the next few days to help in regards to this.

Henry
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Exceuse Henry,,

When I changed the text it gives me the following error:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher


any idea?
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
This the code I use to convert from hexadecimal to byte array:
Sultan Altoobi
Ranch Hand

Joined: Dec 11, 2011
Posts: 34
Sultan Altoobi wrote:Exceuse Henry,,

When I changed the text it gives me the following error:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher


any idea?


I found my mistake!
now it works fine!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use AES DECRYPT_MODE?
 
Similar Threads
AES Encryption/Decrypton
AES decryption - InvalidKeyException: Parameters missing
Problem encrypting URL with AES
Encryption / Decryption Problem
AES Encryption