javascript使用CryptoJS库实现AES加密


使用了第三方组件 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>

你可能感兴趣的:(javascript使用CryptoJS库实现AES加密)