【问题标题】:Php equivalent of as3crypto aes256-cbc相当于 as3crypto aes256-cbc 的 PHP
【发布时间】:2013-11-30 23:51:51
【问题描述】:

有人知道 as3crypto lib 中的 aes256-cbc 密码的 php 等效项是什么吗? 我需要在 as3 和 php 中得到相同的结果,因为我的应用需要 as3 php 数据交换。

这是我的 as3 类:

    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import com.hurlant.crypto.symmetric.ICipher;

    import com.hurlant.crypto.symmetric.IVMode;
    import com.hurlant.crypto.symmetric.IMode;
    import com.hurlant.crypto.symmetric.NullPad;
    import com.hurlant.crypto.symmetric.PKCS5;
    import com.hurlant.crypto.symmetric.IPad;
    import com.hurlant.util.Base64;
    import com.hurlant.util.Hex;
    import com.hurlant.crypto.Crypto;

    public class CryptoCode extends Sprite
    {
            private var type:String='aes256-cbc';
            private var key:ByteArray;

            public function CryptoCode()
            {
                init();
            }

            private function init():void
            {
                    key = Hex.toArray(Hex.fromString('secret'));// can only be 8 characters long

                    //trace(encrypt('rower'));
                    //trace(decrypt(encrypt('TEST TEST'));
            }

            public function encrypt(txt:String = ''):String
            {
                    var data:ByteArray = Hex.toArray(Hex.fromString(txt));

                    var pad:IPad = new PKCS5;
                    var mode:ICipher = Crypto.getCipher(type, key, pad);
                    pad.setBlockSize(mode.getBlockSize());
                    mode.encrypt(data);
                    return Base64.encodeByteArray(data);
            }

            public function decrypt(txt:String = ''):String
            {
                    var data:ByteArray = Base64.decodeToByteArray(txt);
                    var pad:IPad = new PKCS5;
                    var mode:ICipher = Crypto.getCipher(type, key, pad);
                    pad.setBlockSize(mode.getBlockSize());
                    mode.decrypt(data);
                    return Hex.toString(Hex.fromArray(data));
            }
    }

和php类

class Crypt {
    var $key = NULL;
    var $iv = NULL;
    var $iv_size = NULL;

    function Crypt()
    {
            $this->init();
    }

    function init($key = "")
    {
            $this->key = ($key != "") ? $key : "";

            $this->algorithm = MCRYPT_DES;
            $this->mode = MCRYPT_MODE_ECB;

            $this->iv_size = mcrypt_get_iv_size($this->algorithm, $this->mode);
            $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
    }

    function encrypt($data)
    {
            $size = mcrypt_get_block_size($this->algorithm, $this->mode);
            $data = $this->pkcs5_pad($data, $size);
            return base64_encode(mcrypt_encrypt($this->algorithm, $this->key, $data, $this->mode, $this->iv));
    }

    function decrypt($data)
    {
            return $this->pkcs5_unpad(rtrim(mcrypt_decrypt($this->algorithm, $this->key, base64_decode($data), $this->mode, $this->iv)));
    }

    function pkcs5_pad($text, $blocksize)
    {
            $pad = $blocksize - (strlen($text) % $blocksize);
            return $text . str_repeat(chr($pad), $pad);
    }

    function pkcs5_unpad($text)
    {
            $pad = ord($text{strlen($text)-1});
            if ($pad > strlen($text)) return false;
            if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
            return substr($text, 0, -1 * $pad);
    }

}

它与 simple-des-ecb 一起正常工作 - Php 和 Flash 输出相同的字符串,但 aes256-cbc 给出不同的字符串。

我遵循了这个示例 Flash Encryption PHP Decryption,但我需要 aes265-cbc 而不是 simple-des-ecb。

谁能帮帮我?

【问题讨论】:

  • 请也向我们展示您的 PHP 代码。
  • 我已经添加了我使用的 php 类。

标签: php actionscript-3 aes


【解决方案1】:

快速的互联网搜索提供了这个金块:

$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
                             $plaintext, MCRYPT_MODE_CBC, $iv);

这直接来自mcrypt_encrypt 代码示例,我前段时间重写了。

请注意,MCRYPT_RIJNDAEL_128 与 AES 相同。您需要提供 256 位(32 字节)的密钥才能将其用作 AES-256。 MCRYPT_RIJNDAEL_128 中的 128 是 块大小,AES-256 中的 256 是 密钥大小

请注意,PHP 版本的 - 到现在 6 年未维护 - mcrypt 库不提供开箱即用的 PKCS#7 填充。因此,请检查以下 stackoverflow 块:

How to add/remove PKCS7 padding from an AES encrypted string?

【讨论】:

  • 请指出答案中是否缺少任何内容;如果它包含您需要的信息,请点击左侧的 V 标记接受它。
【解决方案2】:

如果你想要一个使用 AES-256 和 CBC + hashMac 的良好 php 版本(基于 mash 的消息验证代码,请随时查看我的小代码片段(作为 yiiframework 插件实现,但只需查看 aes256.php 类信息)。

它将让您使用 AES-256 和私钥加密/解密字符串,避免一些攻击 https://github.com/lucbonnin/aes256_yii_extension

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2014-03-18
    • 1970-01-01
    • 2013-08-02
    • 2012-01-11
    相关资源
    最近更新 更多