function getKey()
{
var key = localStorage.PBK;
if (!key)
{
var password = prompt("Enter data encryption key. Please do not use your password!");
if (!password) { return key; }
var p = {};
p.iter = 1000;
p.salt = [0xD1F6D8FF, 0x482648A7];
key = sjcl.misc.cachedPbkdf2(password, p).key.slice(0, 4);
localStorage.PBK = key;
}
return key;
}
function encryptElements()
{
var key = getKey();
if (!key || !key.length) return;
$(".cryptable").each(
function (idx)
{
if ($(this).val() && !$(this).val().match(/\{iv\:".*",salt\:".*",ct\:".*"\}/))
{
try
{
$(this).val(sjcl.encrypt(key, $(this).val()));
}
catch (e)
{
error("Cannot encrypt: " + e);
return false;
}
}
}
);
return true;
}
function decryptElements()
{
var key = getKey();
if (!key || !key.length) return;
$(".cryptable").each(
function (idx)
{
if ($(this).val() && $(this).val().match(/\{iv\:".*",salt\:".*",ct\:".*"\}/))
{
try
{
$(this).val(sjcl.decrypt(key, $(this).val()));
}
catch (e)
{
// could not decrypt, data will be shown as is.
}
}
}
);
}
It is not totally safe because anyone who has an access to the local machine can obtain the key from the local storage, but it satisfies my needs: as a provider of a service, I will not be able to see the user data, even myself.
No comments:
Post a Comment