0

我已经生成了公钥和私钥并将其存储到本地存储中以供进一步使用,但是当我尝试使用存储在本地存储中的私钥解密时,我得到了一些奇怪的错误。

    var Bits = 1024;
    var privateKey = cryptico.generateRSAKey(PassPhrase, Bits);
    var publicKey = cryptico.publicKeyString(privateKey);

    storageService.set('clientPrivate', privateKey);
    storageService.set('clientPublic', publicKey);*/

    var privateKey = cryptico.generateRSAKey(PassPhrase, Bits);
    var publicKey = cryptico.publicKeyString(privateKey);

    var PlainText = "Matt, I need you to help me with my Starcraft strategy.";
    var encrypted = cryptico.encrypt(PlainText, publicKey);

    console.log('Public key: ' + publicKey);
    console.log('Private key: ' + privateKey);
    console.log('Encrypted: ' + encrypted.chiper);

    var dec = cryptico.decrypt(encrypted.cipher, privateKey);
    console.log(dec.plaintext);

如果我为每次使用创建新密钥,那么它可以工作,但当我将它们存储到本地存储时则不行。

我发现在将私钥保存到本地存储之前:

console.log(privateKey);

输出如下所示:

RSAKey {}

当我显示来自本地存储的数据时,它像这样:

Object {}

我认为这可能是问题所在,但不知道如何解决。

用于从本地存储中获取数据的编辑代码:

angular.module('cashregisterApp')
.service('storageService', function () {
    // AngularJS will instantiate a singleton by calling "new" on this function
    return {
        get: function(key, callback) {
            // get value for that key
            chrome.storage.local.get(key, function(value) {
                var ret = [];
                if (key === null) {
                    ret = value;
                } else {
                    for (var a = 0; a < key.length; a++) {
                        if (typeof value[key[a]] !== 'undefined') {
                            // Add specfic value to array for this key
                            ret[key[a]] = value[key[a]];
                        } else {
                            // Set to null because it is not defined
                            ret[key[a]] = null;
                        }
                    }
                }
                callback(ret);
            });
        },
        set: function(key, value, callback) {
            // set key and value
            var keyValue = {};
            keyValue[key] = value;
            // check callback
            if (typeof callback === 'undefined') {
                callback = function(){};
            }
            chrome.storage.local.set(keyValue, function() { callback(); });
        },
        remove: function(key, callback) {
            // remove only one key value pair
            chrome.storage.local.remove(key, function() {
                callback();
            });
        },
        clear: function(callback) {
            // remove everything
            chrome.storage.local.clear(function() {
                callback();
            });
        }
    };

});

4

0 回答 0