• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in AES decrypt

 
Reem aa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Reem aa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

sorry because I am new in java

Thanks
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit: post removed]
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic