我目前正在尝试使用 SJCL,但在加密/解密方面遇到了问题。由于缺乏好的例子,我想出了你在下面看到的,但它不起作用。谁能指出我做错了什么?提前致谢。
<html>
<!-- sjcl made with: ./configure --with-all -compress=none && make -->
<script type="text/javascript" src="sjcl.js"></script>
<body>
<script>
var p = {
"iv": "PnWtrKCP2DKcLyNC18RKAw==",
"ts": 128,
"mode": "gcm",
"adata": "You can read me",
"cipher": "aes",
"key": "QiJysyALRxUESl18XKl0FcpXQJvFB2Z3Q3A61tdNNM0=" // pbkdf2 key
};
var prp = new sjcl.cipher[p.cipher](sjcl.codec.base64.toBits(p.key));
var plain = "My plaintext";
var ct = sjcl.codec.base64.fromBits(sjcl.mode[p.mode].encrypt(prp, sjcl.codec.bytes.toBits(plain), p.iv, p.adata, p.ts));
var pt = sjcl.codec.base64.fromBits(sjcl.mode[p.mode].decrypt(prp, sjcl.codec.base64.toBits(ct), p.iv, p.adata, p.ts));
document.writeln("ct: " + ct + "<br>");
document.writeln("pt: " + pt + "<br>");
</script>
<hr><pre>
Results in:
encrypted: 5Z2QQ9s6gfORlr6qLvlwjO/J+/TbfSbOs79c4w==
decrypted: AAAAAAAAAAAAAAAA
</pre></body></html>