Encoding & Decoding

before I used a very basic method to encode and decode passwords. In my Application.cfc I had:


<cfif not structKeyExists( Request, 'PasswordKey')>
    <cfset Request.PasswordKey = "L2OIhfkjsyIJHK23jhfkuIYU11iklmeAA35" />
</cfif>

Before the insert I encrypted it:


<cfset encrypted = Encrypt( Form.password, Request.PasswordKey )>

And when I needed it decrypted:


<cfset decrypted = Decrypt( queryname.password, Request.PasswordKey )>

For the site I’m working on I need a way higher security so decided to read in to using algorithm. Both on Adobe and some other sides I found the following method:


<cfset algorithm = "AES">
<cfset encoding = "hex">
<cfset key = GenerateSecretKey(algorithm)>
<cfset encrypted = Encrypt(Form.password, key, algorithm, encoding)>
<cfset decrypted = Decrypt(encrypted, key, algorithm, encoding)>

When I output encrypted and decrypted I indeed get the encrypted and decrypted, since decrypted is based on encrypted (Form.password). But when I try to Decrypt using the Encrypted value stored in the database:


<cfset algorithm = "AES">
<cfset encoding = "hex">
<cfset key = GenerateSecretKey(algorithm)>
<cfset decrypted = Decrypt(query.password, key, algorithm, encoding)>

I get a padding error:

Could not perform unpadding: invalid pad byte.

How should I decript the encrypted database value in this case?

Thank you in advance