欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javascript使用CryptoJS库实现AES加密

程序员文章站 2022-06-14 21:41:53
...
使用了第三方组件 CryptoJS v3.1.2
<script src="aes.js"></script>
<script src="pad-zeropadding.js"></script>
<script>
    var data = "Test String啊啊啊啊啊啊啊啊啊啊啊啊啊啊";
	var key  = CryptoJS.enc.Utf8.parse('1234567812345678');
	var iv   = CryptoJS.enc.Utf8.parse('1234567812345678');
	//var key = "1234567812345678";
    //var iv  = "1234567812345678";
    var encrypted = 
	CryptoJS.AES.encrypt(data, key, { iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});
	//或者不指定padding
    //var encrypted = 
	//CryptoJS.AES.encrypt(data, key, { iv: iv,mode:CryptoJS.mode.CBC});
    document.write("encode:"+encrypted);
	//var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding});
	var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,mode:CryptoJS.mode.CBC});
	alert(decrypted.toString(CryptoJS.enc.Utf8));
</script>
</script>