• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use AES DECRYPT_MODE?

 
Sultan Altoobi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the following error when I run the code:

javax.crypto.BadPaddingException: Given final block not properly padded
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem most likely is in the skewed encrypted data, show us also code you are using for encryption.
 
Sultan Altoobi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question too difficult for “beginning”. Moving thread.
 
Sultan Altoobi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Question too difficult for “beginning”. Moving thread.


Excuse me, to which fourm did you move it??
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This the code I use to convert from hexadecimal to byte array:
 
Sultan Altoobi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic