中央論壇 - CENTER BBS
標題:
JS Encrypt/Decrypt AES (ECB mode為例) 對PHP成功加解密範例
[打印本頁]
作者:
死神
時間:
2015-5-25 20:22
標題:
JS Encrypt/Decrypt AES (ECB mode為例) 對PHP成功加解密範例
首先要先參考
Cryptojs
,但會雷,故只好自己來。
以下範例為AES ECB-mode:(已測試對PHP加解密,使用MCRYPT_RIJNDAEL_128)
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-ecb-min.js"></script>
<script>
// Key
var src_key = '22ca8686bfa31a2ae5f55a7f60009e14';
var key = CryptoJS.enc.Utf8.parse(src_key);
// Source String
var src_string = '{"msg":"Message中文"}';
// Encryption
var encrypted = CryptoJS.AES.encrypt(src_string, key, { mode: CryptoJS.mode.ECB });
encrypted = encrypted.toString();
// Decryption
var decrypted = encrypted;
decrypted = CryptoJS.AES.decrypt(decrypted, key, { mode: CryptoJS.mode.ECB });
decrypted = decrypted.toString(CryptoJS.enc.Utf8);
// Info Template
document.write('Source String: '+src_string+'<br/>');
document.write('Source Key: '+src_key+'<br/>');
document.write('JS Key: '+key+' (After CryptoJS.enc.Utf8.parse)<br/>');
document.write('JS Encryption String: '+encrypted+'<br/>');
document.write('JS Decryption String: '+decrypted+'<br/>');
</script>
複製代碼
注意到官方文件的問題了嗎?
歡迎光臨 中央論壇 - CENTER BBS (https://www.centerbbs.com/)
Powered by Discuz! X3