It's hard for us to decrypt a string in ColdFusion, which was previously encrypted using 3DES and C #. Here is the code we used to initially encrypt the string:
public static string EncryptTripleDES(string plaintext, string key) { TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider(); DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key)); DES.Mode = CipherMode.ECB; ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(plaintext); string EncString = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); EncString = EncString.Replace("+", "@@12"); return EncString; }
We tried using the sentences here:
TripleDES Encryption-.NET and ColdFusion don't play well
.. bad luck. Here is our CF code and error:
<cfset variables.theKey = "blahblah" /> <cfset variables.theAlgorithm = "DESede/CBC/PKCS5Padding"> <cfset variables.theEncoding = "Base64"> <cfset strTest = decrypt(#DB.PASSWORD#, variables.theKey, variables.theAlgorithm, variables.theEncoding)>
Error: An error occurred while trying to encrypt or decrypt your input string: "Unable to decode the string" blahblah "
So it looks like it is trying to decrypt the key, not the string, but thatβs not how the decryption function is described in ColdFusion. Any ideas?
UPDATE: Trying to use the following CF code, but the error returned is still "Error trying to encrypt or decrypt your input string: if the last block is incorrectly populated."
<cfset dbPassword = "Hx41SYUrmnFPa31QCH1ArCHN1YOF8IAL"> <cfset finalText = replace(dbPassword, "@@12", "+", "all")> <cfset theKey = "abcdefgh"> <cfset theKeyInBase64 = toBase64(theKey)> <cfset hashedKey = hash( theKeyInBase64, "md5" )> <cfset padBytes = left( hashedKey, 16 )> <cfset keyBytes = binaryDecode( hashedKey & padBytes , "hex" )> <cfset finalKey = binaryEncode( keyBytes, "base64" )> <cfset decrypted = decrypt( finalText, finalKey, "DESede/ECB/PKCS5Padding", "base64" )> Decrypted String: <cfdump var="#decrypted#">
UPDATE:
The solution, if you follow the comments, should have changed:
<cfset hashedKey = hash( theKeyInBase64, "md5" )>
To:
<cfset hashedKey = hash( theKey, "md5" )>
Last code:
<cfset dbPassword = "Hx41SYUrmnFPa31QCH1ArCHN1YOF8IAL"> <cfset finalText = replace(dbPassword, "@@12", "+", "all")> <cfset theKey = "abcdefgh"> <cfset hashedKey = hash( theKey, "md5" )> <cfset padBytes = left( hashedKey, 16 )> <cfset keyBytes = binaryDecode( hashedKey & padBytes , "hex" )> <cfset finalKey = binaryEncode( keyBytes, "base64" )> <cfset decrypted = decrypt( finalText, finalKey, "DESede/ECB/PKCS5Padding", "base64" )> Decrypted String: <cfdump var="#decrypted#">