打字稿:使用加密模块加密
Typescript: Encryption using crypto module
我正在尝试使用内置加密模块来加密密码。我之前一直在使用 createCipher,现在已弃用。所以我想知道这样的方式是否仍然是一件好事。
旧代码:
hashPassword(pass: string) {
const cipher = crypto.createCipher('aes256', 'a pass');
let encrypted = cipher.update(pass, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
我很感激任何建议。
检查这个...
https://www.w3schools.com/nodejs/ref_crypto.asp
var crypto = require("crypto");
var mykey = crypto.createCipher("aes-128-cbc", "mypassword");
var mystr = mykey.update("abc", "utf8", "hex");
mystr += mykey.final("hex");
console.log(mystr);
但这是一个不使用 KEY 和 IV 的弱方法...
使用来自 Jyotirmoy Upadhyay 的类似内容[=ga=]
const crypto = require("crypto");
const ENC = "bf3c199c2470cb477d907b1e0917c17b";
const IV = "5183666c72eec9e4";
const ALGO = "aes-256-cbc";
const encrypt = (text) => {
let cipher = crypto.createCipheriv(ALGO, ENC, IV);
let encrypted = cipher.update(text, "utf8", "base64");
encrypted += cipher.final("base64");
return encrypted;
};
const decrypt = (text) => {
let decipher = crypto.createDecipheriv(ALGO, ENC, IV);
let decrypted = decipher.update(text, "base64", "utf8");
return decrypted + decipher.final("utf8");
};
const encrypted_key = encrypt("HelloWorld");
const decrypted_key = decrypt(encrypted_key);
console.log(encrypted_key);
console.log(decrypted_key);
我正在尝试使用内置加密模块来加密密码。我之前一直在使用 createCipher,现在已弃用。所以我想知道这样的方式是否仍然是一件好事。
旧代码:
hashPassword(pass: string) {
const cipher = crypto.createCipher('aes256', 'a pass');
let encrypted = cipher.update(pass, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
我很感激任何建议。
检查这个... https://www.w3schools.com/nodejs/ref_crypto.asp
var crypto = require("crypto");
var mykey = crypto.createCipher("aes-128-cbc", "mypassword");
var mystr = mykey.update("abc", "utf8", "hex");
mystr += mykey.final("hex");
console.log(mystr);
但这是一个不使用 KEY 和 IV 的弱方法...
使用来自 Jyotirmoy Upadhyay 的类似内容[=ga=]
const crypto = require("crypto");
const ENC = "bf3c199c2470cb477d907b1e0917c17b";
const IV = "5183666c72eec9e4";
const ALGO = "aes-256-cbc";
const encrypt = (text) => {
let cipher = crypto.createCipheriv(ALGO, ENC, IV);
let encrypted = cipher.update(text, "utf8", "base64");
encrypted += cipher.final("base64");
return encrypted;
};
const decrypt = (text) => {
let decipher = crypto.createDecipheriv(ALGO, ENC, IV);
let decrypted = decipher.update(text, "base64", "utf8");
return decrypted + decipher.final("utf8");
};
const encrypted_key = encrypt("HelloWorld");
const decrypted_key = decrypt(encrypted_key);
console.log(encrypted_key);
console.log(decrypted_key);