加密使用Angularjs客户端本地存储数据 [英] Encrypting the client side local storage data using Angularjs

查看:500
本文介绍了加密使用Angularjs客户端本地存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端中的数据存储的localStorage。出于安全原因,我想对数据进行加密。有什么办法使用Angularjs加密/解密的客户数据(而不是服务器数据)?

  $ scope.accountObj = {
        isErrorMsg:假的,
        isReadonly:假的,
        createAccountErr:假的
    };


解决方案

您可以使用cryptojs库加密/解密数据。首先,你应该产生一些关键的加密过程中使用:

  VAR SecretKey的='你的秘密钥匙;

然后,你需要的方法来存储和索赔数据:

 商店:功能(键,值){
    VAR的EncryptedData = CryptoJS.AES.encrypt(angular.toJson(值),SecretKey的)的ToString();
    window.localStorage.setItem(键,的EncryptedData);
},得到:功能(键){
    VAR的EncryptedData = window.localStorage.getItem(键);    如果(!_。ISNULL(的EncryptedData))
        返回angular.fromJson(CryptoJS.AES.decrypt(encryptedValue,SecretKey的)的ToString(CryptoJS.enc.Utf8));    返回null;
}

这里唯一的问题是,密钥存储在客户端,它是一种打破这种加密的逻辑。

I have a client side data storing in the localStorage. For security reasons i want to encrypt the data. Is there any way to encrypt/decrypt the client data(not server data) using Angularjs?

$scope.accountObj = {
        isErrorMsg:false,
        isReadonly:false,
        createAccountErr:false        
    }; 

解决方案

You could use cryptojs library for encrypting/decrypting your data. First you should generate some key to use in encryption process:

 var secretKey = 'your-secret-key';

Then you need method to store and claim data:

store : function (key, value) {
    var encryptedData = CryptoJS.AES.encrypt(angular.toJson(value), secretKey).toString();
    window.localStorage.setItem(key, encryptedData);
},

get : function (key) {
    var encryptedData = window.localStorage.getItem(key);

    if (!_.isNull(encryptedData))
        return angular.fromJson(CryptoJS.AES.decrypt(encryptedValue, secretKey).toString(CryptoJS.enc.Utf8));

    return null;
}

The only problem here is that secret key is stored on the client side and it's kind of breaking logics of such encryptions.

这篇关于加密使用Angularjs客户端本地存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆