cryptoJS 安装与使用
程序员文章站
2024-03-04 12:22:35
...
在线文档
安装
npm install crypto-js
#安装之后看到node_modules目录直接放到public目录下
使用
#主要引入以下这个文件
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));
}
自己封装
class AesSecurity
{
/**
* method 为AES-128-CBC时
* @var string传入要加密的明文
* 传入一个16字节的key
* 传入一个16字节的初始偏移向量IV
*/
private static $method = 'AES-128-CBC';
private static $key = 'contentWindowHig';
private static $options = OPENSSL_RAW_DATA;
private static $iv = 'contentDocuments';
public static function getKey()
{
return self::$key;
}
public function __construct()
{
self::$key = md5(self::$key,true);
}
public static function setMethod($method){
self::$method = $method;
}
public static function setKey($key){
self::$key = $key;
}
/**
* @param $options 可取值 OPENSSL_ZERO_PADDING OPENSSL_RAW_DATA
*/
public static function setOptions($options){
self::$options = $options;
}
public static function _encrypt($input){
$key = substr(md5(self::$key),0,16);
$iv = substr(md5(self::$iv),0,16);
$data = base64_encode(openssl_encrypt($input,"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv));
return $data;
}
/**
* @param $input
* @return bool|string
* todo rtrim
*/
public static function _decrypt($input){
$key = substr(md5(self::$key),0,16);
$iv = substr(md5(self::$iv),0,16);
$data = openssl_decrypt(base64_decode($input),"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv);
return $data;
}
}
#js端
<script type="text/javascript" >
var key_base = 'contentWindowHig';
var iv_base = 'contentDocuments'
var key_hash = CryptoJS.MD5(key_base).toString();
var iv_hash = CryptoJS.MD5(iv_base).toString();
var key = CryptoJS.enc.Utf8.parse(key_hash.substr(0,16));
var iv = CryptoJS.enc.Utf8.parse(iv_hash.substr(0,16));
function _decrypt(string) {
var data = CryptoJS.AES.decrypt(string, key, {iv: iv, padding: CryptoJS.pad.Pkcs7}).toString(CryptoJS.enc.Utf8);
return data;
}
function _encrypt(string) {
var data = CryptoJS.AES.encrypt(string, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
return data;
}
var a = _encrypt('msgg');
var b = _decrypt(a);
var c= _decrypt('evD5EE8QGSWpuqzhDFaamw==')
console.log(a);
console.log(b);
console.log(c);
</script>
上一篇: ActiveMQ安装与使用
下一篇: 肿瘤全外显子--记录