在PHP和ColdFusion9中的AES加密产生了不同的结果。谁能帮帮我?
下面的PHP代码
$key = "12345678123456781234567812345678";
$iv = "1234567812345678";
$data = "This is a plain string.";
echo base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv));给了我G+tdEOfQTtVCQGxW3N5uzkqN207OyfIPxS6zf2xrKKY=
而下面的ColdFusion代码
<cfset thePlainData = "This is a plain string." />
<cfset theKey = "12345678123456781234567812345678" />
<cfset theAlgorithm = "AES/CBC/PKCS5Padding" />
<cfset theEncoding = "base64" />
<cfset theIV = "1234567812345678" />
<cfset encryptedString = encrypt(thePlainData, theKey, theAlgorithm, theEncoding, theIV) />给我KLt55n5 55n5/T3ee6xVq9VGFbyCacJznkHEqC/RDRhL+4nw=
你知道我哪里错了吗?提前谢谢。
发布于 2012-04-25 13:35:03
不幸的是,在所使用的纯文本填充样式方面,ColdFusion和PHP实现之间略有不兼容。AES要求明文块大小可被128整除。要实现这一点,请使用PHP will pad the plaintext input with NULL characters来获得适当的块大小。ColdFusion可以使用多种padding techniques that are supported by Java。不幸的是,ColdFusion和Java都不支持空填充模式,这使得互操作性变得更加困难。ColdFusion的字符串处理不支持空字符,因此您需要使用implement a PKCS5Padding schema within PHP来使它们正确地互操作。
此外,正如评论中所提到的,ColdFusion期望密钥是base64编码的,因此您需要密钥设置如下所示:
<cfset theKey = toBase64("12345678123456781234567812345678") />此外,缺省情况下Java (以及扩展的ColdFusion )仅支持最大128位的密钥大小。这里你使用了一个256位的密钥,它将require the Java Unlimited Strength extension (对于那些试图测试代码并得到一个非法的密钥大小错误的人)。
生成的PHP代码如下所示:
// Function from http://us3.php.net/manual/en/ref.mcrypt.php#69782
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
$key = "12345678123456781234567812345678";
$iv = "1234567812345678";
// Pad data with PKCS #5 to prevent PHP from using NULL padding.
$data = pkcs5_pad("This is a plain string.", 16);
echo base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv));生成的ColdFusion代码如下所示:
<cfset thePlainData = "This is a plain string." />
<cfset theKey = toBase64("12345678123456781234567812345678") />
<cfset theAlgorithm = "AES/CBC/PKCS5Padding" />
<cfset theEncoding = "base64" />
<cfset theIV = "1234567812345678" />
<cfset encryptedString = encrypt(thePlainData, theKey, theAlgorithm, theEncoding, theIV) />
<cfoutput>#encryptedString#</cfoutput>两者都输出相同的base64编码字符串:
G+tdEOfQTtVCQGxW3N5uzlu0mGabRKNxuIdAXArQE80=发布于 2015-03-09 02:28:13
我知道这是一个老话题,但最近也出现了类似的问题。
虽然在本地不受支持,但事实证明,有一种方法可以在CF中生成空填充。This answer by Artjom B.同意在PHP中调整填充可能会更简单,但他指出,您可以使用0x00填充纯文本,将其填充为algorithm's block size的倍数,并使用"NoPadding“方案。
在CF中生成空字符有点棘手,但可以在using URLDecode("%00")中完成。由于encrypt()始终将输入视为UTF8编码,因此您还可以使用charsetEncode()从单个元素字节数组(即charsetEncode( javacast("byte[]", [0] ), "utf-8") )创建空字符。
没有经过高度测试,但在CF10中应该会产生相同的结果:
代码:
thePlainData = nullPad("This is a plain string.", 16);
// NB: JCE unlimited policy files required for 256 bit keys
theKey = toBase64("12345678123456781234567812345678");
theIV = "1234567812345678";
encryptedString = encrypt(thePlainData, theKey, "AES/CBC/NoPadding", "base64", theIV);结果:
G+tdEOfQTtVCQGxW3N5uzkqN207OyfIPxS6zf2xrKKY=函数:
/*
Pads a string, with null bytes, to a multiple of the given block size
@param plainText - string to pad
@param blockSize - pad string so it is a multiple of this size
@param encoding - charset encoding of text
*/
string function nullPad( string plainText, numeric blockSize, string encoding="UTF-8")
{
local.newText = arguments.plainText;
local.bytes = charsetDecode(arguments.plainText, arguments.encoding);
local.remain = arrayLen( local.bytes ) % arguments.blockSize;
if (local.remain neq 0)
{
local.padSize = arguments.blockSize - local.remain;
local.newText &= repeatString( urlDecode("%00"), local.padSize );
}
return local.newText;
}https://stackoverflow.com/questions/10301041
复制相似问题