I have been seeing a lot of problems and resolutions of Coldfusion Decrypt function. However, I have not come across this one at all.
I can encrypt and decrypt properly. I generated a key and encrypt it with:
<cfset encrytedpwd = encrypt(password, mykey, "AES/CBC/PKCS5Padding", "hex")>
This is done in program #1. Then in a different program #2 I decrypted it with:
<cfset mypwd = decrypt(encrytedpwd, mykey, AES/CBC/PKCS5Padding", "hex")>
which is great and works perfectly.
Now the problem: the password for all the records are not always encrypted. Some are encrypted because they passed thru program #1. But some old records are not encrypted. So when the password is retrieved as "encrytedpwd", the decrypt function will crash with the error:
An error occurred while trying to encrypt or decrypt your input string: ''.
I would expect the CF Decrypt function to return maybe "false" or some kind of warning rather than cause the CF to crash.
Another scenario is that if someone temper with the value of the encrypted password before passing to program #2, it will also crash.
Is there any way to check the value of password that it is hex or not before passing to the Decrypt function so that it won't crash ungracefully?
Thanks a lot.
Edited:
I added the try/catch in the code and it does not catch the exception at all.
On top of the CF activate.cfm I include a template call handlerror.cfm which has the following:
<cferror
template = "exception.cfm"
type = "exception"
mailTo = "[email protected]">
The exception.cfm is simply a formatted error page:
...
<li><b>Your Location:</b> #error.remoteAddress#
<li><b>Your Browser:</b> #error.browser#
<li><b>Date and Time the Error Occurred:</b> #error.dateTime#
<li><b>Page You Came From:</b> #error.HTTPReferer#
<li><b>Message Content</b>:
<p>#error.diagnostics#</p>
...
The activate.cfm has
<cfinclude template="handlerror.cfm">
<cfif isdefined("url.chk") and isdefined("url.auth")>
<cfif len(url.chk) gt 0 and len(url.auth) gt 0>
<cftry>
<cfset uid=decrypt(url.chk, mykey, "AES/CBC/PKCS5Padding", "hex")>
<cfcatch>
<div class="msgcenter">
<div class="warning">Sorry, there seems to be an issue with the activation </div><br>
</div>
<cfoutput>
<!--- The diagnostic message from ColdFusion. --->
<p>#cfcatch.message#</p>
<p>Caught an exception, type = #CFCATCH.TYPE#</p>
<p>The contents of the tag stack are:</p>
<cfdump var="#cfcatch.tagcontext#">
</cfoutput>
</cfcatch>
</cftry>
<cfif REFindNoCase("[^a-z0-9_]", uid)>
<div class="warning">Problem with User ID.</div><br>
<cfelse>
.....
So what is happening now is, if the url.chk is tempered or invalid, it goes to display the exception but for the undefined UID for the REFindNoCase because the Decrypt didn't work in the first place and now it is skipping it because of try/catch.
Without the try/catch the exception would show the error message "An error occurred while trying to encrypt or decrypt your input string: ''." But because of the try/catch it is not doing that now. So I am stuck.