前端 crypto-js AES 加解密

AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。严格地说,AES和Rijndael加密法并不完全一样(虽然在实际应用中二者可以互换),因为Rijndael加密法可以支持更大范围的区块和密钥长度:AES的区块长度固定为128比特,密钥长度则可以是128,192或256比特;而Rijndael使用的密钥和区块长度可以是32位的整数倍,以128位为下限,256比特为上限。包括AES-ECBAES-CBCAES-CTRAES-OFBAES-CFB

JavaScript加密库Crypto-JShttps://github.com/brix/crypto-js

CryptoJS (crypto.js) 为 JavaScript 提供了各种各样的加密算法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>

<script src="./crypto-js/src/core.js"></script>
<script src="./crypto-js/src/evpkdf.js"></script>
<script src="./crypto-js/src/cipher-core.js"></script>
<script src="./crypto-js/src/md5.js"></script>
<script src="./crypto-js/src/aes.js"></script>
<script src="./crypto-js/src/tripledes.js"></script>
<script src="./crypto-js/src/enc-base64.js"></script>
<script src="./crypto-js/src/enc-utf16.js"></script>
<script src="./crypto-js/src/format-hex.js"></script>
<script src="./crypto-js/src/hmac.js"></script>
<script src="./crypto-js/src/lib-typedarrays.js"></script>
<script src="./crypto-js/src/mode-cfb.js"></script>
<script src="./crypto-js/src/mode-ctr.js"></script>
<script src="./crypto-js/src/mode-ctr-gladman.js"></script>
<script src="./crypto-js/src/mode-ecb.js"></script>
<script src="./crypto-js/src/mode-ofb.js"></script>
<script src="./crypto-js/src/pad-ansix923.js"></script>
<script src="./crypto-js/src/pad-iso10126.js"></script>
<script src="./crypto-js/src/pad-iso97971.js"></script>
<script src="./crypto-js/src/pad-nopadding.js"></script>
<script src="./crypto-js/src/pad-zeropadding.js"></script>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<button id="md5">MD5</button>
<button id="aes">AES</button>

<script>

$(function() {
$("#md5").click(function(){
console.log("测试");
//MD5
var md5Str = CryptoJS.MD5("key").toString(CryptoJS.enc.Hex).toUpperCase();
console.log("MD5后得到的字符串:%s", md5Str)
return false;
});

$("#aes").click(function() {
var encrypted = getAES('AppBlog.CN')
console.log("AES加密得到的字符串:%s", encrypted)
var decrypted = getDeAes(encrypted)
console.log("AES解密得到的字符串:%s", decrypted)

/********************/
var key = '12345678';
console.log('密钥:', key);
key = CryptoJS.enc.Hex.parse(key)
var src = "AppBlog.CN";
console.log('原字符串:', src);
var enc = CryptoJS.AES.encrypt(src ,key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})

var encode = enc.ciphertext.toString()
console.log("加密:", encode);

var decode = CryptoJS.AES.decrypt(CryptoJS.format.Hex.parse(encode), key, {
//iv:iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
console.log('解密:', CryptoJS.enc.Utf8.stringify(decode));

return false;
});

function getAesString(data, key, iv) { //加密
var key = CryptoJS.enc.Utf8.parse(key);
var iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(data, key,
{
iv: iv, //CBC模式需要指定偏移量
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
console.log(encrypted.ciphertext.toString())
return encrypted.ciphertext.toString()
//return CryptoJS.enc.Base64.stringify(encrypted.ciphertext); //返回Base64格式的密文
}

function getDeAesString(encrypted, key, iv) { //解密
var key = CryptoJS.enc.Utf8.parse(key);
var iv = CryptoJS.enc.Utf8.parse(iv);
var decrypted = CryptoJS.AES.decrypt(CryptoJS.format.Hex.parse(encrypted), key,
//var decrypted = CryptoJS.AES.decrypt(CryptoJS.enc.Base64.parse(encrypted), key,
{
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}

function getAES(data) { //加密
var key = '12345678'; //密钥(数位为8的整数倍)
var iv = '1234567812345678';
var encrypted = getAesString(data, key, iv); //密文
var encryptedStr = CryptoJS.enc.Utf8.parse(encrypted);
return encrypted;
}

function getDeAes(data) { //解密
var key = '12345678'; //密钥(数位为8的整数倍)
var iv = '1234567812345678';
var decryptedStr = getDeAesString(data, key, iv);
return decryptedStr;
}
});
</script>
</body>
</html>

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :