wood burning stoves 2.0*
The moose likes Security and the fly likes problem in AES decrypt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "problem in AES decrypt " Watch "problem in AES decrypt " New topic
Author

problem in AES decrypt

Reem aa
Greenhorn

Joined: Mar 07, 2013
Posts: 5

Hello I am trying to decrypt using AES 128-bit key.
I have key dat file and encrypt dat file, and the ouput is decrypt file.
the error appers
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded

can you please help me

the code


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
You seem to be treating encrypted data as text data (by storing it in a String) - that doesn't work. Encrypted data is binary, you need to keep it in a byte[].

Rule of thumb (which applies to all Java code, not just encryption code): using "String.getBytes()" and "new String(byte[])" is an indicator of looming problems - you should generally specify the encoding.


Ping & DNS - my free Android networking tools app
Reem aa
Greenhorn

Joined: Mar 07, 2013
Posts: 5


I convert data file to string <--- is this true?

sorry because I am new in java

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
I'm not sure what you mean by "convert data file to string" - if it means you have a file of encrypted data, and that's what you pass off as a string in line 2 - then yes, that's a problem. Encrypted data is binary, not character data, so you need to treat it as such.
Reem aa
Greenhorn

Joined: Mar 07, 2013
Posts: 5
Thanks A lot for your help,

OK how I can read .dat file?

because I read .dat file, then store in String after that pass as string
I don't know how I can deal with this type of file,

can you help me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
Assuming that that file contains encrypted data: there is no way you can store its contents in a string. Seriously, until you understand why encrypted data is binary data and not character data -and how both kinds differ- you need to stop working on this project, because you won't get anywhere.
Reem aa
Greenhorn

Joined: Mar 07, 2013
Posts: 5
[Edit: post removed]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Reem aa wrote:Thanks A lot for your help,

OK how I can read .dat file?

because I read .dat file ...


Reading the binary file is simple -- just use the java io library to read the binary data directly into a byte array.


Reem aa wrote:then store in String after that pass as string


As others have already mentioned ... This is the part that breaks. Encrypted data can't be stored into a string (unless you happen to get real lucky and run into a case that does). So, this means that the code that you shown so far is broken. It is not possible to create a string that can be converted back to encrypted data for most of the cases.

In other words, it doesn't matter what you do to convert the string to be passed to the method, the method won't work regardless of what you pass. You need to fix the method before you can worry about passing data to it. Either convert the method to use a byte array instead, or (if you really want to only use a string) convert the method to use a string that is holding encoded data.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in AES decrypt
 
Similar Threads
encoding in java and decoding in perl
AES Encryption/Decrypton
AES decryption - InvalidKeyException: Parameters missing
Problem encrypting URL with AES
How to use AES DECRYPT_MODE?