Link to home
Start Free TrialLog in
Avatar of ronayers
ronayers

asked on

Error "Given final block not properly padded" when decrypting string

I am getting this weird error when i decrypt certain types of strings. I am encrypting strings using DESEDE in CF8. When i decrypt text like pjpeiry1 it will bomb with "Given final block not properly padded". this also seems to be true with text like pjpeiry11 and pjpeiry22. Really need some help on this one.

Thanks
Avatar of _agx_
_agx_
Flag of United States of America image

You mean it bombs when you decrypt in CF8? Can you provide the code snippets you're using.
Avatar of ronayers
ronayers

ASKER

Correct. It bombs when decrypting. Coldfusion encrypts everything without error. just bombs when i decrupt certian strings.
bombs on this line
#Decrypt(Trim(field),"myencryptionkey","DESEDE")#

thanks for your help.
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, removed TRIM() and it still bombed.
Since you say it doesn't occur every time, can you provide sample values we can test  (both encrypt and decrypt)? Because this type of error is usually caused by a code problem.
Also, how is the encrypted value handled in between the encrypt() and decrypt()? ie Is it being stored in a db (cookie, etc...) then transferred to another page and decrypted.  A lot of times, the encrypted value gets unintentionally corrupted somewhere in the process (white space is lost, etc..). So decrypt() fails because the final encrypted string used is not the same as it was. Obviously everything used to decrypt (the string, key, etc..) has to be exactly the same as it was, or it will fail.

One way to check this is to perform a char-by-char comparison of the encrypted string, right after the encrypt() and right before the decrypt().  If the results aren't the same, that explains the error. So you just need to find out where the value is getting corrupted.

<cfoutput>
Encrypted String Length = #len(encryptedString)#<hr>
<cfloop from="1" to="#len(encryptedString)#" index="x">
      <cfset c = mid(encryptedString, x, 1)>
      [#x#] =  #c# ( ASCII #asc(c)# ) <br>
</cfloop>
</cfoutput>
That is about all I can suggest without seeing some real values and code ..