如何填充一个字符串与null /零字节在ColdFusion& MacOS和Windows之间的CF差异 [英] How to pad a string with null/zero-bytes in ColdFusion & difference between CF on MacOS and Windows

查看:104
本文介绍了如何填充一个字符串与null /零字节在ColdFusion& MacOS和Windows之间的CF差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解我如何使用空字节填充CF中的字符串。在Java中,我会这样做...

I'm having trouble understanding how I can pad a string in CF with null bytes. In Java I would do this...

String ZeroPad = "";
for (int i = 0; i < 32; i++)
    ZeroPad = ZeroPad + "\0";
String strKey = strUsername + strPassword + ZeroPad;
strKey = strKey.substring(0, 32);

而不是空字节。

<cfset var key = arguments.username&arguments.password/>
<cfloop condition="#len(key)# less than 32">
    <cfset key = key & "\0"/>
</cfloop>
<cfset key = key.substring(0,32)/>

续问题。好的,我已经更新我的CFML到这...

Continued problems. Ok, I've updated my CFML to this...

<cfset var strB = createObject("java", "java.lang.StringBuilder")/>

<cfloop from=1 to=32 index="i">
    <cfset zeroPad = zeroPad & URLDecode("%00")/>
</cfloop>

<cfset strB.append(arguments.username)/>
<cfset strB.append(arguments.password)/>
<cfset strB.append(zeroPad)/>

<cfif strB.length() GT 32>
    <cfset key = strB.substring(0,32)/>
<cfelse>
    <cfset key = strB.toString()/>
</cfif>

生成的密钥用于AES加密。在我的本地dev机器(Mac OS X小牛)这工作正常,我可以用生成的密钥加密。但是,在我的生产环境(Windows Server 2008)我得到错误无效的密钥大小。

The key being generated is used for AES encryption. On my local dev machine (Mac OS X Mavericks) this works fine and I'm able to encrypt with the generated key. However, on my Production environment (Windows Server 2008) I'm getting error "Invalid key size". The key size on both shows as 32 and I'm flummoxed.

推荐答案

我使用了此提示,即: URLDecode(%00,utf-8 )

更新

code> encrypt()函数总是将纯文本输入解释为UTF-8字符串,还可以使用 charsetEncode(bytes,utf-8)从单个元素字节数组创建一个空字符。

Since the encrypt() function always interprets the plain text input as a UTF-8 string, you can also use charsetEncode(bytes, "utf-8") to create a null character from a single element byte array.

 charsetEncode( javacast("byte[]", [0] ), "utf-8");

这篇关于如何填充一个字符串与null /零字节在ColdFusion&amp; MacOS和Windows之间的CF差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆