Win a copy of Fixing your Scrum this week in the Agile 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

problem in AES decrypt

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Edit: post removed]
 
author
Posts: 23905
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

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
 
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