【发布时间】:2021-05-12 23:54:11
【问题描述】:
我有 2 个变量:text 和 password。 text 是我想加密的一些信息,password 是我想用来加密文本的密码。我了解如何在 Node.js 中加密文本,但我所需要的与我目前所见的不同。
我的目标是让函数使用密码处理文本,并生成与原始文本在长度上合理接近的内容。
这是我已经想出的:
class Encryptor {
constructor(encryptionKey) {
this.algorithm = "aes256";
this.key = encryptionKey
}
encrypt(text) {
var cipher = crypto.createCipher(this.algorithm, this.key);
var encrypted = cipher.update(text, 'utf8', 'base64') + cipher.final('base64');
return encrypted;
}
decrypt(encrypted) {
var decipher = crypto.createDecipher(this.algorithm, this.key);
var decrypted = decipher.update(encrypted, 'base64', 'utf8') + decipher.final('utf8');
return decrypted;
}
}
我是这样使用的:
let text = "I am some secret info"
let password = "password"
let encryptor = new Encryptor(password);
let encrypted = encryptor.encrypt(text);
console.log(text.length, encrypted.length)
这给了我一个大约是原始长度 2 倍的加密文本。但是,当与magic... 之类的文本一起使用时,它的作用远不止于此。
这让我想到了我的问题:有没有办法用密码加密文本并生成长度接近原始文本的加密文本?
谢谢!
【问题讨论】:
标签: node.js encryption cryptography