Coldfusion加密和解密

时间:2014-09-16 23:28:35

标签: encryption coldfusion cryptography coldfusion-9

我在使用cf9加密和解密功能时遇到问题。具体来说,我使用的代码需要能够使用AES / ECB / PKCS5Padding 256加密。我正在尝试使用以下代码将数据发布到第三方银行:

<cfparam name="theKey" default="someStaticKey">
<cfset strName = leaddetail.leadlast&','&leaddetail.leadfirst />
    <cfset stFields = {requesttype = "eftaddonetimecompletetransaction"
            ,clientid = "XXXXXX"
            ,urltoredirect = "#RedirectURl#"
            ,customerid = "#leaddetail.leadid#"
            ,isdebitcardonly = "No"
            ,customername = "#strName#"
            ,customeraddress1 = "#form.billingaddress#"
            ,customercity = "#form.billingcity#"
            ,customerstate = "#form.billingstate#"
            ,customerzip = "#form.billingzip#"
            ,cardbillingaddr1 = "#form.billingaddress#"
            ,cardbillingcity = "#form.billingcity#"
            ,cardbillingstate = "#form.billingstate#"
            ,cardbillingzip = "#form.billingzip#"
            ,accounttype = "CC"
            ,name_on_card = "#form.leadname#"
            ,accountnumber = "#form.ccacctnum#"
            ,expmonth = "#Left(form.ccexpdate,2)#"
            ,expyear = "#Right(form.ccexpdate,2)#"
            ,cvvcode = "#form.ccv2#"
            ,amount = "#NumberFormat(esigninfo.esignpayamt,'9999.99')#"
            ,startdate = "#DateFormat(Now(),'YYYY-MM-DD')#"
            ,transactiontypecode = "WEB"}/>
            <cfset theEncryptedStr = ToBase64(encrypt(serializeJson(stFields),theKey))>
                    <!--- shake hands and login to api --->
    <cfhttp url="https://www.somebank.com/cgi-bin/wsnvptest.vps" method="post" charset="ISO-8859-1" throwonerror="yes" result="httpResponse">
                <!--- login Variables --->
        <cfhttpparam type="Header" name="User-Agent" value="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41">
        <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" >
        <cfhttpparam type="header" name="Accept" value="application/json" >
                <!--- Login Credentials --->
        <cfhttpparam type="formfield"name="nvpvar"value="''"/>
        <cfhttpparam type="formfield"name="requesttype"value="login"/>
        <cfhttpparam type="formfield"name="userid"value="Dauserid"/>
        <cfhttpparam type="formfield"name="password"value="password"/>
        <cfhttpparam type="formfield"name="requestid"value="#uniquerequestid#"/>
        <cfhttpparam type="formfield"name="PostData" value="#theEncryptedStr#"/>
        <cfif isDefined('CheckSession.sessionID') AND CheckSession.sessionID NEQ ''>
            <cfhttpparam type="formfield"name="sessionid" value="#checkSession.sessionID#"/>
        </cfif>
    </cfhttp>

由于某种原因我可以加密,但无法解密响应。是否有人有更多的经验,可以走上成功的道路?


(拒绝编辑更新)

我使用以下脚本解密我的cfhttp响应字符串:

<cfset content = httpResponse.filecontent>
<cfset authdata = structNew()> 
<cfloop index="line" list="#content#"> 
   <cfset dtype = listFirst(line, "=")/> 
   <cfset EncodedString = listRest(line, "=")/> 
   <cfset authdata[dtype] = EncodedString /> 
</cfloop> 

<cfscript> 
   keyInBase64 = "Some64baseKey; 
   // Now decrypt the base64 encoded encryption string 
   decryptedValue = decrypt(EncodedString , keyInBase64 , "AES/ECB/PKCS5Padding" , "base64" ); 
   WriteDump(variables); 
</cfscript> 

我尝试解密的字符串类似于以下内容:

  

nvpvar = fKsJGJ-Fe系2zoqUROAeE8N8a87USuQpE7dVd5IvRa67cHsVkPwx9taKLuXKwsCKFC_-NW3j6IOQAvCnRNc4cRNfq2fqyRHBUrFbFDCCmwpk =

1 个答案:

答案 0 :(得分:6)

  

encrypt(serializeJson(stFields),theKey)

首先,这是 AES加密。您省略了algorithm参数,因此encrypt()默认为遗留CFMX_COMPAT算法(最弱)。 CFMX_COMPAT它不是真正的&#34;加密算法,所以不要使用它 - 任何事情!

其次,使用AES,您limited to 128 bit keys out of the box。要使用256位密钥,必须先安装(JCE) Unlimited Strength Jurisdiction Policy Files

  

将它们复制到/lib/security/目录中。然后重新启动CF服务器以应用更改。

     

注意:如果安装了多个JVM,   一定要更新正确的。即列出的那个   CF管理员)。

重新启动后,您将能够使用256位密钥进行加密。使用ECB模式时,语法为:encrypt(string, key, algorithm, encoding)

  • string - 要加密的纯文本
  • key - 256位密钥,必须是base64编码的
  • algorithm - 表示所需的单个字符串:算法,模式和填充方案。注意:&#34; AES&#34;实际上是更长时间的简写&#34; AES / ECB / PKCS5Padding&#34;
  • encoding - &#34;将数据表示为字符串的二进制编码&#34;

全部放在一起:

  <cfscript>
  // some sample data to encrypt 
  stFields = { clientid = "AA-BB-CC-123"
                    , customername = "ABC Incorporated" 
                  };

  // some 256 bit key, must be base64 encoded
  // hard coded for demo purposes only
  keyInBase64 = "9NJU2L3FZ8Rr0WKZUFC3lyE/yRrQ7sIZmQRk3kx9MLE=";

  // AES encrypt the value, and base64 encode the result 
  encryptedValue = encrypt(  serializeJson(stFields)
                                     , keyInBase64
                                     , "AES/ECB/PKCS5Padding"
                                     , "base64" 
                           );
  // Now decrypt the base64 encoded encryption string 
  decryptedValue = decrypt(  encryptedValue
                                      , keyInBase64
                                      , "AES/ECB/PKCS5Padding"
                                      , "base64"
                           );

  // display results
  WriteDump( variables );
</cfscript>

根据评论进行更新:

我注意到你正在操纵原始的http响应字符串。没有看到API,我猜想要么a)你需要先解密整个字符串,然后提取部分 - 或 - b)代码没有正确提取值,这就是为什么你不能解密它。 cfloop将响应视为逗号分隔列表。根据变量名称,我猜它实际上是由新行分隔,即:

 param1=xxxxx      -- new line
 param2=yyyyy      -- new line
 param3=zzzzz      

但这些只是猜测。检查您的API。返回的响应的实际格式是什么?另外,您能否{@ 3}}包含完整错误消息?