AES/CryptoJS/PHP 加密解密

作者: JONE 分类: PHP/MySQL 发布时间: 2019-01-05 13:18

原文链接 https://blog.csdn.net/tangpanqing/article/details/71117822

AES加密解密 接口开发很重要的环节

PHP代码

        $text = 'After all, tomorrow is another day.';
        $key = pack('H*', 'e10adc3949ba59abbe56e057f20f883e'); //md5('123456')
        $iv = pack('H*', '1234567890abcdef1234567890abcdef');
        
        $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
        $encrypted = base64_encode($encrypted);
        var_dump($encrypted);
        
        $dncrypted = base64_decode($encrypted);
        $dncrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $dncrypted, MCRYPT_MODE_CBC, $iv);
        $dncrypted = trim($dncrypted);
        var_dump($dncrypted);

JS代码

        var text = "After all, tomorrow is another day.";
        var key = CryptoJS.enc.Hex.parse("e10adc3949ba59abbe56e057f20f883e"); //md5('123456')
        var iv = CryptoJS.enc.Hex.parse("1234567890abcdef1234567890abcdef");
     
        var opinion = {iv:iv, padding:CryptoJS.pad.ZeroPadding};
        
        var encrypted = CryptoJS.AES.encrypt(text, key, opinion);
        encrypted = encrypted.toString();
        console.log(encrypted);
        
        var decrypted = CryptoJS.AES.decrypt(encrypted, key, opinion);
        decrypted = decrypted.toString(CryptoJS.enc.Utf8);
        console.log(decrypted);

记得引用CryptoJS

<script type="text/javascript" src="http://cdn.bootcss.com/crypto-js/3.1.9/crypto-js.js"></script>
 <script type="text/javascript" src="http://cdn.bootcss.com/crypto-js/3.1.9/aes.js"></script>

发表评论

电子邮件地址不会被公开。 必填项已用*标注