最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

encrypting an object in javascript - Stack Overflow

programmeradmin8浏览0评论
var Obj = {};
Obj.ID = e.row.ID;
Obj.firstName = e.row.firstName;
Obj.lastName = e.row.lastName;

This is my object, and i save this object in a file. now before saving into file, i want to encrypt it and save and while reading i want to decrypt and read.

var newFile = FileSystemPath;
newFile.write(JSON.stringify(object));
  1. Should i encrypt the object before stringifying it or after it.
  2. What are the ways to encrypt an object in javascript. Any examples would be great.
var Obj = {};
Obj.ID = e.row.ID;
Obj.firstName = e.row.firstName;
Obj.lastName = e.row.lastName;

This is my object, and i save this object in a file. now before saving into file, i want to encrypt it and save and while reading i want to decrypt and read.

var newFile = FileSystemPath;
newFile.write(JSON.stringify(object));
  1. Should i encrypt the object before stringifying it or after it.
  2. What are the ways to encrypt an object in javascript. Any examples would be great.
Share Improve this question asked Sep 30, 2011 at 7:00 John CooperJohn Cooper 7,64133 gold badges83 silver badges102 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

You can't really encrypt objects, but you can encrypt strings, so you should probably first do a object serialization (JSON.stringify) and then encrypt it with a symmetric encryption algorithm so you would be able to decode the object later.
I can't really provide a good example, because javascript will always have serious security problems (being a client-side programming language), and even if you try a rather complex algorithm (such as AES) it will still be vulnerable, because the user can just see your source code, thus see your encription/decription algorithms.
If you just want to alter the string a bit so it can't be deciphered on the first look, you can simply use some built-in javascript methods (such as encodeURI/decodeURI) or you can do some character replacements or even use salts.

Here's a sample demo of how you can "encrypt" an object :

function encrypt(o, salt) {
    o = JSON.stringify(o).split('');
    for(var i = 0, l = o.length; i < l; i++)
        if(o[i] == '{')
            o[i] = '}';
        else if(o[i] == '}')
            o[i] = '{';
    return encodeURI(salt + o.join(''));
}

 function decrypt(o, salt) {
    o = decodeURI(o);
    if(salt && o.indexOf(salt) != 0)
        throw new Error('object cannot be decrypted');
    o = o.substring(salt.length).split('');
    for(var i = 0, l = o.length; i < l; i++)
        if(o[i] == '{')
            o[i] = '}';
        else if(o[i] == '}')
            o[i] = '{';
    return JSON.parse(o.join(''));
}

var obj = {
    key : 'value',
    3 : 1
};
var salt = "some string here";
var encrypted = encrypt(obj, salt);
var decrypted = decrypt(encripted, salt);

Of course, this is just an example and you should modify it in order to encrypt more complex objects, where you need to encrypt functions, or where the object has circular references.

发布评论

评论列表(0)

  1. 暂无评论