Encrypt / Decrypt help
28 Message(s) by 4 Author(s) originally posted in advanced cfml techniques
| From: WolfShade |
Date: Tuesday, October 23, 2007
|
Hello, all.
In the past, I always used HASH to encrypt the
password for users; all I
needed to do was to check the HASHed value from login and compare to what is in
the db - simple.
Now I have a
client that'd like to add a password retrieval
system to the
site. I'm having difficulties with the values never being the same.
Originally I tried encrypting with BLOWFISH, but that did not work for decrypt.
Here is the
code I have, so far:
APPLICATION.CFM:
<CFSET request.cryptAlgorithm = "DESEDE">
<CFSET request.cryptEncoding = "HEX">
<CFSET request.cryptKey = GenerateSecretKey(request.cryptAlgorithm)>
<CFSET request.cryptPhrase = "Eightcha">
LOGIN ACTION.CFM
<CFSET session.user.password =
URLEncodedFormat(Encrypt("#trim(form.password)#", request.cryptKey,
request.cryptAlgorithm)) />
<CFSET session.user.SQLpassword =
trim(replace(session.user.password,"'","''","all")) />
<CFQUERY name="checkLogin" dbtype="odbc" datasource="#request.data_source #">
EXEC check_login @xxxxxxxxxxx='#trim(session.user.username)#',
@xxxxxxxxxxx='#trim(session.user.SQLpassword)#'
</CFQUERY>
<CFDUMP var="#checkLogin#"><CFABORT>
I'm dumping the results of the
query and aborting to view the values; the
password is always changing. How can I keep the value always the same?
Thanks,
^_^
| From: GArlington |
Date: Wednesday, October 24, 2007
|
wrote in message:
Hello, all.
In the past, I always used HASH to encrypt the password for users; all I
needed to do was to check the HASHed value from login and compare to what is in
the db - simple.
Now I have a client that'd like to add a password retrieval system to the
site. I'm having difficulties with the values never being the same.
Originally I tried encrypting with BLOWFISH, but that did not work for decrypt.
Here is the code I have, so far:
APPLICATION.CFM:
<CFSET request.cryptAlgorithm = "DESEDE">
<CFSET request.cryptEncoding = "HEX">
<CFSET request.cryptKey = GenerateSecretKey(request.cryptAlgorithm)>
<CFSET request.cryptPhrase = "Eightcha">
LOGINACTION.CFM
<CFSET session.user.password =
URLEncodedFormat(Encrypt("#trim(form.password)#", request.cryptKey,
request.cryptAlgorithm)) />
<CFSET session.user.SQLpassword =
trim(replace(session.user.password,"'","''","all")) />
<CFQUERY name="checkLogin" dbtype="odbc" datasource="#request.data_source#">
EXEC check_login @xxxxxxxxxxx='#trim(session.user.username)#',
@xxxxxxxxxxx='#trim(session.user.SQLpassword)#'
</CFQUERY>
<CFDUMP var="#checkLogin#"><CFABORT>
I'm dumping the results of the query and aborting to view the values; the
password is always changing. How can I keep the value always the same?
Thanks,
^_^
On most systems password retrieval is actually a two step
process that
gives a user NEW
random system generated password, that they can
change later.
| From: dacf |
Date: Wednesday, October 24, 2007
|
Have you tried it without the URLEncodedFormat around form,password?
It does not seem necessary if the 'password' is coming from the form scope, and
will actually change a
string if the password contains certain characters, see
the attached code (CF8).
Also, is the
database doing any
sort of encrypt/decrypt functionality? If so,
might want to check if the two match up in a kind of similar way to the code
attached.
<CFSET testVars = ["sometest", "d.if/chars", "?><eues", "%solf"]/>
<ul>
<CFLOOP array="#testVars#" index="testVar">
<li><CFOUTPUT>Orig: #testVar# ?= #URLEncodedFormat(testVar)#
</CFOUTPUT></li>
</CFLOOP>
</ul>
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
I have tried it both with and without the URLEncodedFormat(); with and without
defining the algorithm, the encoding; the same result every time - every
click
of the submit
button dumps the query result (including the query, itself) and
shows that the encrypted password value is never the same, twice. I will
include
three results, here:
#1
EXEC check_login @xxxxxxxxxxx='administrator',
@xxxxxxxxxxx='0JD%2FDM%5C%3EH%25H%3AQ%20BI%5C%3CN3%3F%20P%20%20%0A'
#2
EXEC check_login @xxxxxxxxxxx='administrator',
@xxxxxxxxxxx='02M%3A%27NC%5BGH%3D75CSV%25VHF%2FBP%20%20%0A'
#3
EXEC check_login @xxxxxxxxxxx='administrator',
@xxxxxxxxxxx='0J%3D%2DDGS%3A%25%2C%21DR%3A%21%27O%2CWU4%3B%40%20%20%0A'
Now without the URLEncodedFormat():
#1
EXEC check_login @xxxxxxxxxxx='administrator', @xxxxxxxxxxx='0E Y,/@xxxxxxxxxxx^K"^ZGA(W00W@xxxxxxxxxxx@xxxxxxxxxxx'
#2
EXEC check_login @xxxxxxxxxxx='administrator', @xxxxxxxxxxx='0F@xxxxxxxxxxx;IP0''7K<I6,1?*I,MS/@xxxxxxxxxxx'
#3
EXEC check_login @xxxxxxxxxxx='administrator', @xxxxxxxxxxx='0/%EVXY@xxxxxxxxxxx&Y0?Y_7O#@xxxxxxxxxxx@xxxxxxxxxxx'
If you are familiar with proper encrypt/decrypt usage, could you please post
some
sample code? I tried following the example in livedocs and that is not
working, either.
Thanks,
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
<CFSET request.cryptKey = GenerateSecretKey(request.cryptAlgorithm)>
It looks like the secretKey is changing on every request. When the
key
changes, the encrypted changes. To get the same value you must use the same
key.
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
[q][i]Originally posted by: [b][b]cf_dev2[/b][/b][/i]
<CFSET request.cryptKey = GenerateSecretKey(request.cryptAlgorithm)>
It looks like the secretKey is changing on every request. When the key
changes, the encrypted value changes. To get the same encrypted value you must
use the same key.[/q]
That's what I thought. Originally I had used
[q]Encrypt("#form.password#","8charkey","BLOWFISH")[/q] to encrypt, but
could not get it to DEcrypt from that. Kept getting an
error that the input and
output don't match. Do you know how to decrypt from the above encrypt?
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
Same concept. But again decrypt won't work if you're not using the same key
that was used to encrypt the string.
<CFSET cryptAlgorithm = "BLOWFISH">
<CFSET cryptKey = GenerateSecretKey(cryptAlgorithm)>
<CFSET form.password = "mypassword">
<CFSET encryptedPassword = Encrypt(form.password, cryptKey, cryptAlgorithm)>
<CFSET decryptedPassword = Decrypt(encryptedPassword, cryptKey,
cryptAlgorithm)>
<CFOUTPUT>
Form.Password = #form.password#[HR]
Encrypted = #encryptedPassword#[HR]
Decrypted = #decryptedPassword#[HR]
</CFOUTPUT>
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
Could it be
server related?
Microsoft Windows Server 2003 and CF8.
application.CFM
[h]
<CFSET request.cryptAlgorithm = "BLOWFISH">
<CFSET request.cryptKey = GenerateSecretKey(request.cryptAlgorithm)>
[/h]
loginaction.CFM
[h]
<CFSET session.user.username = trim(form.username) />
<CFSET session.user.password = Encrypt(trim(form.password), request.cryptKey,
request.cryptAlgorithm) />
<CFSET session.user.SQLpassword = session.user.password />
<CFQUERY name="checkLogin" dbtype="odbc" datasource="#request.data_source#">
EXEC cpwr_adm_check_login @xxxxxxxxxxx='#session.user.username#',
@xxxxxxxxxxx='#trim(session.user.SQLpassword)#'
</CFQUERY>
<CFDUMP var="#checkLogin#"><CFABORT>
[/h]
I'm still getting the same result; different output each time.
@xxxxxxxxxxx='08!4[BP%=M!</[JW)/TF! P'
@xxxxxxxxxxx='0C4X\TE.DZKG62]?''F-7ZD@xxxxxxxxxxx'
@xxxxxxxxxxx='0P8%&LU##?L:%Q("7Q4M2.@xxxxxxxxxxx'
?
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
I'm still getting the same result; different output each time.
You're still changing the secretKey on every request. Different key == different encrypted string.
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
Btw, I have no idea what your stored
procedure is doing :)
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
So GenerateSecretKey() gives a different output each time it's
run . Do I *have* to use GenerateSecretKey() in order to decrypt?
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
So GenerateSecretKey() gives a different output each time it's run.
Yes
> Do I *have* to use GenerateSecretKey() in order to decrypt?
When you a [i]key[/i] to Encrypt(), then yes you must use that same key to
Decrypt(). Note, that does not mean calling GenerateSecretKey() inside
Decrypt(). It means saving the key value and passing the saved key to
Decrypt().
You do not [i]have[/i] to use a key at all. You could simply do this
<CFSET encryptedPassword = Encrypt(form.password, cryptAlgorithm)>
<CFSET decryptedPassword = Decrypt(encryptedPassword, cryptAlgorithm)>
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
You do not have to use a key at all.
Scratch that. If you do not use a key I think it'll default to CFMX_COMPAT, which isn't what you want.
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
I have NO idea what just happened. The first time I tried encrypting and
decrypting without using GenerateSecretKey() it errored. Now it's not.
%^/
I'm not going to question it. I'm just going to
accept it. I'll keep
telling myself, "It does not matter, it works. It does not matter, it works."
Thanks for your help.
Feel bad about wasting your time, like that.
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
No. See my last comment. If you do not use a key I think it will
ColdFusion uses CFMX_COMPAT which is [b]not[/b] the same as using BLOWFISH.
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
That's just it. I am using a key, and using BLOWFISH. Why it's working, now,
I do not know; but it was not, before.
application.CFM
<CFSET request.cryptAlgorithm = "BLOWFISH" />
<CFSET request.cryptKey = "16characterskey1" />
loginaction.CFM
<CFSET session.user.username = trim(form.username) />
<CFSET session.user.password = Encrypt(trim(form.password), request.cryptKey,
request.cryptAlgorithm) />
<CFSET session.user.SQLpassword = session.user.password />
<CFQUERY name="checkLogin" dbtype="odbc" datasource="#request.data_source#">
EXEC check_login @xxxxxxxxxxx='#session.user.username#',
@xxxxxxxxxxx='#trim(session.user.SQLpassword)#'
</CFQUERY>
<CFOUTPUT>password is #Decrypt(session.user.SQLpassword, request.cryptKey,
request.cryptAlgorithm)#</CFOUTPUT>
<CFDUMP var="#checkLogin#"><CFABORT>
I'm stumped, but not questioning it. It is working, now. (shrug) Without
GenerateSecretKey().
Thanks,
^_^
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
Okay.. I spoke too soon. It works ON THAT PAGE. As soon as I attempt to
decrypt on another page after pulling the password from the database, it errors.
An error occurred while trying to encrypt or decrypt your input string: The
input and output encodings aren't same..
<CFOUTPUT>Password for this account is #Decrypt(getAdmin.userPW,
request.cryptKey, request.cryptAlgorithm)#</CFOUTPUT>
AFAIK, the database is not changing anything and the datatype is nvarchar(100).
Any idea as to what I'm missing, this time?
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
First I'd recommend creating an example that strips the code to its
simplest form (ie. no trim(), urlencodedformat(), etc.) Just to ensure you
have not inadvertently changed the values somehow.
Next, what about dacf's suggestion? Did you output the two values and see if
they're different? ie. output the encrypted ColdFusion string and the string from your
database query.
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
Okay.. here's what I have got, currently:
STORED PROCEDURE:
CREATE PROCEDURE dbo.check_login
@xxxxxxxxxxx nvarchar(50),
@xxxxxxxxxxx nvarchar(100)
AS
DECLARE @xxxxxxxxxxx int
SET @xxxxxxxxxxx='0'
IF EXISTS (
SELECT [adminID]
FROM [cpwr_org_01].[dbo].[cpwr_admin_users]
WHERE [userName] = @xxxxxxxxxxx AND [userPW] = @xxxxxxxxxxx
)
BEGIN
SET @xxxxxxxxxxx='1'
SELECT [adminID], [firstName], [lastName], [userName], [userLevel],
[datecreated], [lastLogin], [isActive], @xxxxxxxxxxx as statusCode, [userPW]
FROM [cpwr_org_01].[dbo].[cpwr_admin_users]
WHERE [userName] = @xxxxxxxxxxx AND [userPW] = @xxxxxxxxxxx AND [isActive] = 1
END
ELSE
BEGIN
SELECT @xxxxxxxxxxx as statusCode
END
IF (@xxxxxxxxxxx = '1')
BEGIN
UPDATE [cpwr_org_01].[dbo].[cpwr_admin_users]
SET [lastLogin] = getdate()
WHERE [userName] = @xxxxxxxxxxx AND [userPW] = @xxxxxxxxxxx
END
GO
APPLICATION.CFM
<CFSET request.cryptAlgorithm = "BLOWFISH" />
<CFSET request.cryptKey = "8charkey" />
LOGINACTION.CFM
<CFSET session.user.username = trim(form.username) />
<CFSET session.user.password = Encrypt(trim(form.password), request.cryptKey,
request.cryptAlgorithm) />
<CFSET session.user.SQLpassword = session.user.password />
<CFQUERY name="checkLogin" dbtype="odbc" datasource="#request.data_source#">
EXEC cpwr_adm_check_login @xxxxxxxxxxx='#session.user.username#',
@xxxxxxxxxxx='#trim(session.user.SQLpassword)#'
</CFQUERY>
direct encrypted pw is
<CFDUMP var="#session.user.SQLpassword#"><br>
db retrieved pw is
<CFDUMP var="#checkLogin.userPW#">
This is what displays when I run the page:
direct encrypted pw is 0L42&E!R11!?XN3Z#X#$J;
db retrieved pw is 0L42&E!R11!?XN3Z#X#$J;
According to this, they are identical. But when I run the other page that
attempts to decrypt, I get this:
An error occurred while trying to encrypt or decrypt your input string: The
input and output encodings aren't same..
72: Password for this account is #Decrypt(getAdmin.userPW, request.cryptKey,
request.cryptAlgorithm)#
STORED PROCEDURE:
CREATE PROCEDURE dbo.get_admin_edit
@xxxxxxxxxxx int
AS
SELECT [adminID], [firstName], [lastName], [userName], [userPW], [userLevel],
[datecreated], [lastLogin], [isActive], [userEmail]
FROM [cpwr_org_01].[dbo].[cpwr_admin_users]
WHERE adminID = @xxxxxxxxxxx
GO
EDIT PAGE:
Password for this account is #Decrypt(getAdmin.userPW, request.cryptKey,
request.cryptAlgorithm)#
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
strips the code to its simplest form (ie. no trim(), urlencodedformat(), etc
> @xxxxxxxxxxx='#trim(session.user.SQLpassword)#'
It looks like you have still got some excess code in their (ie trim() calls) and
I think that's what's messing you up. Try removing the trim() calls.
Generally speaking, once you encrypt the value, you do not want to change the
encrypted string in any way because encrypt/decrypt are extremely sensitive.
> According to this, they are identical.
Looks can be deceiving ;) You can always use compare() to verify that two
strings actually are identical.
Btw, why are you using CFQUERY to run your stored procedures instead of
CFSTOREDPROC?
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
CFQ vs CFSP - (shrug) Habit, really, I guess. When I first started learning
ColdFusion in 2000, I just used CFQ and never got around to using CFSP.
I will remove the trim(), but I'm paranoid about that. I have always trimmed the
strings prior to db insert; and I always use trim() when displaying. I have had
too many times where a comparison did not match because of a lack of trim().
I will also try the compare to see if there is any difference. Thanks for the
idea.
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
Ah, okay.
Yes, I agree with you that values should be trimmed. But assuming you have used
Encrypt(trim(form.password),...) there's no need to use trim() again on the
encrypted string. Its probably changing the value.
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
Without trim(), it's throwing a blank
space on the end of the encrypted
password being submitted by the form. Even if I add the " " to the end of the
password in the database, it still comes back as user does not exist. Since the
user does not exist, it's not returning the password for compare() to check. :(
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
I do not know if its a
literal space but if that's how the value is encrypted,
its fine. Just leave the string as is. Decrypt will
handle it. In other
words, once you have called encrypt() do not change the value in any way. I tried
a quick test and it worked fine for me, so I suspect trim() might be the source
of the problem.
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
But I have removed the trim() from anywhere outside the Encrypt()
tag for
password.
<CFSET session.user.username = trim(form.username) />
<CFSET session.user.password = Encrypt(trim(form.password), request.cryptKey,
request.cryptAlgorithm) />
<CFSET session.user.SQLpassword = session.user.password />
<CFQUERY name="checkLogin" dbtype="odbc" datasource="#request.data_source#">
EXEC cpwr_adm_check_login @xxxxxxxxxxx='#session.user.username#',
@xxxxxxxxxxx='#session.user.password#'
</CFQUERY>
<CFDUMP var="#checkLogin#">
direct encrypted pw is
<CFDUMP var="#session.user.SQLpassword#"><br>
db retrieved pw is
<CFDUMP var="#checkLogin.userPW#">[h]
<-- Error here, no
records returned because no match[/h]
<CFOUTPUT>
direct password is #Decrypt(session.user.SQLpassword, request.cryptKey,
request.cryptAlgorithm)#
database password is #Decrypt(checkLogin.userPW, request.cryptKey,
request.cryptAlgorithm)#
Comparison = #compare(session.user.SQLpassword,checkLogin.userPW)#
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
What about when the password is first inserted? Try it with a completely new password.
In my tests it seems to be working fine every time. So maybe the existing values were stored incorrectly?
| From: WolfShade |
Date: Wednesday, October 24, 2007
|
Och. That will take some time. I have not created the user add/edit form, yet.
:( And I have got other things that have to be finished by tomorrow morning.
I will get that done ASAP afterwards, though, and let you know if that made any
difference.
Thanks, again, for your thoughts and advice.
^_^
| From: cf_dev2 |
Date: Wednesday, October 24, 2007
|
Okay. I will check back later on to see how you made out.
Next Message: PDFLib Cold Fusion example
Blogs related to Encrypt / Decrypt help
Still having problems with encrypt
We had a similar problem with both our Java and CF
encrypt/
decrypt code. We found that our MS-SQL server was being "helpful" and converting, trimming or otherwise manipulating the string we sent in. We took an additional step and base64
...
Still having problems with encrypt - CFusion_Encrypt() Works!
This works great (out of the 20 or so test I've done so far:
Why encrypt/decrypt isn't working correctly is totally beyond my understanding. ...
encrypt/decrypt bug
I can't tell if the problem is on the encrypt or decrypt side either. During these test, the var "encCODE" was kept constant. I haven't tried yet, but wonder if changing it would make a difference. Will try that next. ...
encrypt/decrypt bug
Hi Les, I've seen similar behavior most typically when I'm also urlencoding the the encrypted value. What I figured out was happening is that if the encrypted value contained a space, when urlencodedformat() was run ...
encrypt/decrypt bug
Could you post the entire encrypt/decrypt code snip? -----Original Message----- From: Les Mizzell [mailto:lesmizz@bellsouth.net] Sent: Friday, April 27, 2007 11:45 AM To: CF-Talk Subject: encrypt/decrypt bug This is driving me crazy! ...
Stop View Source
You're not talking about the CFML code, are you? It IS possible to encrypt your ColdFusion code in the .cfm files, but I that can be decrypted as well back into Java by anyone with access to the web server. ...