我正在使用 localStorage 存储数据并且我想加密密码,然后当密码在 localStorage 中加密时,我现在想解密它以便能够登录。我尝试加密工作但解密不工作,错误消息是: Uncaught SyntaxError: Unexpected end of input
我正在使用 CryptoJS v3.1.2 code.google.com/p/crypto-js
这里的代码:
var JsonFormatter = {
stringify: function (cipherParams) {
var jsonObj = {ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)};
if (cipherParams.iv) {jsonObj.iv = cipherParams.iv.toString();}
if (cipherParams.salt) {jsonObj.s = cipherParams.salt.toString();}
return JSON.stringify(jsonObj);
},
parse: function (jsonStr) {
var jsonObj = JSON.parse(jsonStr);
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
});
if (jsonObj.iv) {cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)}
if (jsonObj.s) {cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)}
return cipherParams;
}
};
加密.js
function btnSubmit(){
var p = document.getElementById("setpass").value;
if(p.length >= 5){
var password = localStorage["Password"];
var encoded = CryptoJS.AES.encrypt(JSON.stringify(p), password, {format: JsonFormatter}).ciphertext.toString();
localStorage.setItem("Password", encoded);
jAlert('Password is set', 'URIBlocker');
window.location.reload();
}
else
{
//Notification
}
}
解密.js
function login() {
var usepass = document.getElementById('usepass').value;
var password = localStorage["Password"];
var encrypted = localStorage.getItem("Password");
var decrypted = CryptoJS.AES.decrypt(encrypted, password, {formatter: JsonFormatter}).toString();
var secret = JSON.parse(decrypted);
if (usepass === secret)
{
$('.bd_class').show();
$('.go_class').hide();
}
else
{
$('.bd_class').hide();
$('go_class').show();
}
}
出了点问题,我不知道错误是 Uncaught SyntaxError: Unexpected end of input