【发布时间】:2020-01-31 18:02:09
【问题描述】:
我尝试在 cookieStorage 中加密和解密一个字符串,但是当我执行我的代码时,我收到这样的消息,比如这个方法已经过时并且它不起作用
我的代码:
const crypto = requite('crypto');
app.get("/crypto", (req, res) => {
var word = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlMzBiZjE0YmQ4OTNmMmE5Y2Q1Y2I5MSIsInJvbGVzIjoiVVNFUl9ST0xFIiwiZW1haWwiOiJjYW1lcmF0ZXN0ODExQGdtYWlsLmNvbSIsImlhdCI6MTU4MDMzNjc1MCwiZXhwIjoxNTgwMzk2NzUwfQ.2bbqkoX7qhyX7lyLjBtlGPe08-oHGjO83nNIPxAzHv8";
var algorithm = "aes-256-ct";
var password = "3zTvzr3p67VC61jmV54rIYu1545x4TlY";
var hw = encrypt(word, algorithm, password);
console.log("encrypt: ", hw);
console.log("decrypt: ", decrypt(hw, algorithm, password));
});
function encrypt(text, algorithm, password) {
var cipher = crypto.createCipher(algorithm, password);
var crypted = cipher.update(text, "utf8", "hex");
crypted += cipher.final("hex");
return crypted;
}
function decrypt(text, algorithm, password) {
var decipher = crypto.createDecipher(algorithm, password);
var dec = decipher.update(text, "hex", "utf8");
dec += decipher.final("utf8");
return dec;
}
【问题讨论】:
-
嗨。一种方法已经过时了,但是……哪一种?
-
尝试查看 API:nodejs.org/api/crypto.html
-
此方法:createCipher 和 createDecipher
-
有待阅读的错误消息或警告,通常会提供有关如何解决问题的非常具体的信息。
-
我放了两个console.log,但它没有显示,我有一个警告(createCipher 和 createDecipher)不推荐使用它
标签: javascript node.js string cryptojs