使用加密时出现问题。我很确定我的问题是在saveAccounts函数的第75行。我相信在这一点上,帐户var将包含一个在getAccounts函数中分配的空[](因为'存储'中的'accounts'没有'getItemSync'。我感觉这个问题与我试图加密的数据的格式,但我无法解决这个问题。现在已经麻烦了一段时间。天。我的代码下面是完整错误报告。
console.log('starting password manager');
var crypto = require("crypto-js");
var storage = require('node-persist');
storage.initSync();
var argv = require('yargs')
.command('create', 'Create a new account', function(yargs){
yargs.options({
name: {
demand: true,
alias: 'n',
description: "Account name (eg: Github, SnapChat).",
type: 'string'
},
username: {
demand: true,
alias: 'u',
description: "Please provide a username for your account.",
type: 'string'
},
password: {
demand: true,
alias: 'p',
description: "Please provide a password for your account.",
type: 'string'
},
masterPassword: {
demand: true,
alias: 'm',
description: "Please provide a MASTER password.",
type: 'string'
}
}).help('help');
})
.command('get', 'Get an existing account', function(yargs){
yargs.options({
name: {
demand: true,
alias: 'n',
description: "Account name (eg: Github, SnapChat).",
type: 'string'
},
masterPassword: {
demand: true,
alias: 'm',
description: "Please provide a MASTER password.",
type: 'string'
}
}).help('help');
})
.help('help')
.argv;
var command = argv._[0];
function getAccounts (masterPassword){
console.log("function getAccounts has been run");
var encryptedAccount = storage.getItemSync('accounts');
var accounts = [];
// decrypt
if (typeof encryptedAccount !== 'undefined') {
var bytes = crypto.AES.decrypt(encryptedAccount, masterPassword);
accounts = JSON.parse(bytes.toString(crypto.enc.Utf8));
}
// return accoutns array
return accounts;
}
function saveAccounts (accounts, masterPassword){
console.log("function saveAccounts has been run");
// encrypt accounts
var encryptedAccounts = crypto.AES.encrypt(JSON.stringify(accounts), masterPassword);
// setItemSync
storage.setItemSync('accounts', encryptedAccounts.toString());
// return accounts
return accounts;
}
function createAccount(account, masterPassword){
var accounts = getAccounts(masterPassword);
accounts.push(account);
console.log("back to function createAccount");
saveAccounts(account, masterPassword);
}
function getAccount(accountName, masterPassword){
var accounts = getAccounts(masterPassword);
var matchedAccount;
accounts.forEach(function(account){
if (account.name === accountName) {
matchedAccount = account;
}
});
return matchedAccount;
}
if (command === "create") {
var createdAccount = createAccount({
name: argv.name,
username: argv.username,
password: argv.password
}, argv.masterPassword);
console.log('Account created!');
console.log(createdAccount);
} else if (command === "get") {
if (masterPassword !== argv.m || typeof masterPassword === undefined) {
console.log("Your password was incorrect.");
} else {
var accountReturned = getAccount(argv.name, argv.masterPassword);
if(typeof(accountReturned) === undefined){
console.log("This account doesn't exist.");
} else {
console.log("Your account info:");
console.log(accountReturned);
}
}
}
完整的错误代码如下所示
starting password manager
function getAccounts has been run
back to function createAccount
function saveAccounts has been run
/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/aes.js:96
var keyWords = key.words;
^
TypeError: Cannot read property 'words' of undefined
at Object.C_algo.AES.BlockCipher.extend._doReset (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/aes.js:96:32)
at Object.CryptoJS.lib.Cipher.C_lib.Cipher.BufferedBlockAlgorithm.extend.reset (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/cipher-core.js:119:19)
at Object.CryptoJS.lib.Cipher.C_lib.BlockCipher.Cipher.extend.reset (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/cipher-core.js:457:27)
at Object.CryptoJS.lib.Cipher.C_lib.Cipher.BufferedBlockAlgorithm.extend.init (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/cipher-core.js:104:19)
at Object.subtype.init (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/core.js:68:46)
at Object.subtype.init (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/core.js:68:46)
at Object.subtype.init (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/core.js:68:46)
at Object.C_lib.Base.create (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/core.js:95:32)
at Object.CryptoJS.lib.Cipher.C_lib.Cipher.BufferedBlockAlgorithm.extend.createEncryptor (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/cipher-core.js:63:26)
at Object.CryptoJS.lib.Cipher.C_lib.SerializableCipher.Base.extend.encrypt (/media/david/08053ee9-7733-4986-97be-f5cac7a80746/david/Projects/Node-Password-Manager/node_modules/crypto-js/cipher-core.js:669:37)
答案 0 :(得分:0)
您应该将typeof与字符串进行比较 - ' undefined'。 See typeof docs
else if (command === "get") {
if (masterPassword !== argv.m || typeof masterPassword === 'undefined') {
console.log("Your password was incorrect.");
} else {
var accountReturned = getAccount(argv.name, argv.masterPassword);
if(typeof(accountReturned) === 'undefined'){
console.log("This account doesn't exist.");
} else {
console.log("Your account info:");
console.log(accountReturned);
}
}
我正在研究同样的Udemy课程。祝你好运!
答案 1 :(得分:0)
我有同样的问题,在尝试不同的事情后,我发现错误是要加密的对象类型。 ---> $crypto.encrypt("string","string")
。就那么简单。当您尝试使用对象而不是字符串进行解密时,您会收到该错误。