<i id='7EHZK'><tr id='7EHZK'><dt id='7EHZK'><q id='7EHZK'><span id='7EHZK'><b id='7EHZK'><form id='7EHZK'><ins id='7EHZK'></ins><ul id='7EHZK'></ul><sub id='7EHZK'></sub></form><legend id='7EHZK'></legend><bdo id='7EHZK'><pre id='7EHZK'><center id='7EHZK'></center></pre></bdo></b><th id='7EHZK'></th></span></q></dt></tr></i><div id='7EHZK'><tfoot id='7EHZK'></tfoot><dl id='7EHZK'><fieldset id='7EHZK'></fieldset></dl></div>
    1. <legend id='7EHZK'><style id='7EHZK'><dir id='7EHZK'><q id='7EHZK'></q></dir></style></legend>

    2. <small id='7EHZK'></small><noframes id='7EHZK'>

      <tfoot id='7EHZK'></tfoot>
      • <bdo id='7EHZK'></bdo><ul id='7EHZK'></ul>

      PHP ColdFusion9 AES 加密 - 不同的结果

      PHP ColdFusion9 AES Encryption - Different results(PHP ColdFusion9 AES 加密 - 不同的结果)
      <i id='XIMOF'><tr id='XIMOF'><dt id='XIMOF'><q id='XIMOF'><span id='XIMOF'><b id='XIMOF'><form id='XIMOF'><ins id='XIMOF'></ins><ul id='XIMOF'></ul><sub id='XIMOF'></sub></form><legend id='XIMOF'></legend><bdo id='XIMOF'><pre id='XIMOF'><center id='XIMOF'></center></pre></bdo></b><th id='XIMOF'></th></span></q></dt></tr></i><div id='XIMOF'><tfoot id='XIMOF'></tfoot><dl id='XIMOF'><fieldset id='XIMOF'></fieldset></dl></div>

          <bdo id='XIMOF'></bdo><ul id='XIMOF'></ul>

          1. <legend id='XIMOF'><style id='XIMOF'><dir id='XIMOF'><q id='XIMOF'></q></dir></style></legend>
            • <tfoot id='XIMOF'></tfoot>
                <tbody id='XIMOF'></tbody>

                <small id='XIMOF'></small><noframes id='XIMOF'>

                本文介绍了PHP ColdFusion9 AES 加密 - 不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                PHP 和 ColdFusion9 中的 AES 加密产生不同的结果.有人可以帮帮我吗?

                AES Encryption in PHP and ColdFusion9 is producing different results. Could somebody please help me?

                以下 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=

                gives me G+tdEOfQTtVCQGxW3N5uzkqN207OyfIPxS6zf2xrKKY=

                下面的 ColdFusion 代码

                While the below ColdFusion Code

                <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/T3ee6xVq9VGFbyCacJznkHEqC/RDRhL+4nw=

                gives me KLt55n5/T3ee6xVq9VGFbyCacJznkHEqC/RDRhL+4nw=

                知道我哪里错了吗?提前致谢.

                Any idea where I am wrong? Thanks in advance.

                推荐答案

                不幸的是,ColdFusion 和 PHP 实现之间在使用的明文填充样式方面存在轻微的不兼容.AES 要求明文块大小可被 128 整除.为了实现这一点,PHP 将使用 NULL 字符填充明文输入获得适当的块大小.ColdFusion 可以使用 Java 支持的各种 填充技术.不幸的是,ColdFusion 和 Java 都支持 NULL 填充模式,这使得互操作性更加困难.ColdFusion 的字符串处理不支持 NULL 字符,因此您需要在 PHP 中实现 PKCS5Padding 架构 而是让它们正确地互操作.

                Unfortunately there is a slight incompatibility between the ColdFusion and PHP implementations regarding the plaintext padding style used. AES requires a plaintext block size divisible by 128. To achieve this, PHP will pad the plaintext input with NULL characters to get the proper block size. ColdFusion can use a variety of padding techniques that are supported by Java. Unfortunately, ColdFusion nor Java support a NULL padding schema which makes interoperability more difficult. ColdFusion's string handling does not support NULL characters, so you will need to implement a PKCS5Padding schema within PHP instead to get them to inter-operate properly.

                此外,正如评论中提到的,ColdFusion 将期望密钥是 base64 编码的,因此您需要密钥设置如下所示:

                Also, as mentioned in the comments, ColdFusion will expect the key to be base64 encoded, so you'd need the key setting to look like:

                <cfset theKey = toBase64("12345678123456781234567812345678") />
                

                此外,默认情况下 Java(以及扩展的 ColdFusion)仅支持最大 128 位的密钥大小.在这里,您使用的是 256 位密钥,这将 需要 Java 无限强度扩展(对于那些试图测试代码并得到非法密钥大小错误).

                Further, Java by default (and ColdFusion by extension) only supports key sizes up to 128 bits. Here you're using a 256 bit key which would require the Java Unlimited Strength extension (for those trying to test the code and getting an illegal key size error).

                生成的 PHP 代码如下所示:

                The resulting PHP code looks like:

                // 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 代码如下所示:

                The resulting ColdFusion code looks like:

                <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编码字符串:

                Both output the same base64 encoded string:

                G+tdEOfQTtVCQGxW3N5uzlu0mGabRKNxuIdAXArQE80=
                

                这篇关于PHP ColdFusion9 AES 加密 - 不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <tbody id='Zt5H5'></tbody>

                    <small id='Zt5H5'></small><noframes id='Zt5H5'>

                    <i id='Zt5H5'><tr id='Zt5H5'><dt id='Zt5H5'><q id='Zt5H5'><span id='Zt5H5'><b id='Zt5H5'><form id='Zt5H5'><ins id='Zt5H5'></ins><ul id='Zt5H5'></ul><sub id='Zt5H5'></sub></form><legend id='Zt5H5'></legend><bdo id='Zt5H5'><pre id='Zt5H5'><center id='Zt5H5'></center></pre></bdo></b><th id='Zt5H5'></th></span></q></dt></tr></i><div id='Zt5H5'><tfoot id='Zt5H5'></tfoot><dl id='Zt5H5'><fieldset id='Zt5H5'></fieldset></dl></div>
                  • <tfoot id='Zt5H5'></tfoot>
                  • <legend id='Zt5H5'><style id='Zt5H5'><dir id='Zt5H5'><q id='Zt5H5'></q></dir></style></legend>
                        <bdo id='Zt5H5'></bdo><ul id='Zt5H5'></ul>