Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to use AES DECRYPT_MODE?

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the following error when I run the code:

javax.crypto.BadPaddingException: Given final block not properly padded
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question too difficult for “beginning”. Moving thread.
 
Sultan Altoobi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Excuse me, to which fourm did you move it??
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
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 Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic