我发现有人编写了一个使用CryptoJS加密客户端数据的演示页面,其中包括:
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<br>text:<input id='text' type='text' value='0.1'/>
<br>password:<input id='pass' type='text' value='cool'/>
<br><button onclick="encrypt()">encrypt</button>
<br><button onclick="decrypt()">decrypt</button>
<br>encrypted:<br><input id='result'></input>
<br>decrypted:<br><span id="decode"></span>
<script>
function encrypt(){
var encrypted = CryptoJS.AES.encrypt(document.getElementById("text").value, document.getElementById("pass").value);
document.getElementById("result").value = encrypted;
}
function decrypt(){
var decrypted = CryptoJS.AES.decrypt(document.getElementById("result").value, document.getElementById("pass").value);
document.getElementById("decode").innerHTML = decrypted;
}
</script>
</html>
它只有一个加密功能,所以我添加了我认为的解密功能,但结果却是完全不同的文本。
我的整个测试页:
Hi I'm Lucas
在此页面上,如果我输入passwd
作为明文(无引号),密码为U2FsdGVkX1/S3TobCOdyJ7k7eaBhdFKRpJkDBTpV1D0=
,我会收到一串字符:48692049276d204c75636173
如果我继续点击解密按钮,我没有收到任何错误,但我得到 var i;
$("body").on("click",".createnew", function(){
$(".inputsl").append("<div class='inputt'><input type='text' name='myanswer[]' id='createmyanswer'"+i+" class='myinput' placeholder='Write answer!'></div>");
i++;//no needed if you dont want to use id attribute
});
的结果。
显然,我做错了什么,并希望在这里提供帮助:)
答案 0 :(得分:1)
从this开始,以下情况似乎有效:
document.getElementById("decode").innerHTML = decrypted.toString(CryptoJS.enc.Utf8)