当前位置: 首页 > 工具软件 > Crypto-JS > 使用案例 >

crypto-js -- 密码加密

陆弘新
2023-12-01

特别感谢

官方文档

  • https://github.com/brix/crypto-js/blob/master/docs/QuickStartGuide.wiki#AES

安装

在文件根目录下

npm install crypto-js

使用

引入以下这个文件

  • crypto-js/crypto-js.js

示例

#js端
function secret(string, code, operation) {
        code = CryptoJS.MD5('contentDocuments').toString();
        code2 = CryptoJS.MD5('contentWindowHig').toString();
        var iv = CryptoJS.enc.Utf8.parse(code.substring(0, 16));
        var key = CryptoJS.enc.Utf8.parse(code2.substring(0,16));
        if (operation) {
            return CryptoJS.AES.decrypt(string, key, {iv: iv, padding: CryptoJS.pad.Pkcs7}).toString(CryptoJS.enc.Utf8);
        }
        return CryptoJS.AES.encrypt(string, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        }).toString();
    }
# php端
public static function secret($string,$code,$operation=false){
        $code = md5('contentDocuments');
        $code2 = md5('contentWindowHig');
        $iv = substr($code,0,16);
        $key = substr($code2,0,16);
        if($operation){
            return openssl_decrypt(base64_decode($string),"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv);
        }
        return base64_encode(openssl_encrypt($string,"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv));
    }
 类似资料: