如何使用 crypto.js 创建 AES-256 十六进制字符串?
How to create an AES-256 hex string using crypto.js?
我想将 json 编码为 AES-256,然后将其转换为十六进制字符串。
然而,结果还是空的。我该如何解决这个问题?
- 环境:node.js
import CryptoJS from "crypto-js";
const STORE_KEY = "12345678912345678912345678912345"
EncryptHex(JSON.stringify(params), "AES");
const EncryptHex = (string, chip) => {
let result = "";
try {
if (chip === "AES") {
result = CryptoJS.AES.encrypt(string, STORE_KEY).toString(
CryptoJS.enc.Hex
);
console.log("@@@@@@");
console.log(result); // this is empty
console.log("@@@@@@");
} else {
result = CryptoJS.HmacSHA256(string, STORE_KEY).toString(
CryptoJS.enc.Hex
);
}
return result;
} catch (error) {
throw error;
}
};
以及以后如何进行解密?
这样做:
const crypto = require('crypto')
const STORE_KEY = ''// your key
function aes256 (string) {
let result = ''
const decipher = crypto.createDecipher('aes-256-cbc', STORE_KEY)
result = decipher.update(string, 'hex', 'utf8')
result += decipher.final('utf8')
return result
}
aes256('lalala')
和 SHA256:
let string = '' // your data
let result = crypto.createHmac('SHA256', STORE_KEY).update(string).digest('hex')
谢谢
我在@Topaco 的帮助下解决了这个问题。
const key = CryptoJS.enc.Utf8.parse(STORE_KEY);
const iv = CryptoJS.enc.Utf8.parse(STORE_IV);
result = CryptoJS.AES.encrypt(string, key, { iv: key });
result = result.ciphertext.toString();
我想将 json 编码为 AES-256,然后将其转换为十六进制字符串。
然而,结果还是空的。我该如何解决这个问题?
- 环境:node.js
import CryptoJS from "crypto-js";
const STORE_KEY = "12345678912345678912345678912345"
EncryptHex(JSON.stringify(params), "AES");
const EncryptHex = (string, chip) => {
let result = "";
try {
if (chip === "AES") {
result = CryptoJS.AES.encrypt(string, STORE_KEY).toString(
CryptoJS.enc.Hex
);
console.log("@@@@@@");
console.log(result); // this is empty
console.log("@@@@@@");
} else {
result = CryptoJS.HmacSHA256(string, STORE_KEY).toString(
CryptoJS.enc.Hex
);
}
return result;
} catch (error) {
throw error;
}
};
以及以后如何进行解密?
这样做:
const crypto = require('crypto')
const STORE_KEY = ''// your key
function aes256 (string) {
let result = ''
const decipher = crypto.createDecipher('aes-256-cbc', STORE_KEY)
result = decipher.update(string, 'hex', 'utf8')
result += decipher.final('utf8')
return result
}
aes256('lalala')
和 SHA256:
let string = '' // your data
let result = crypto.createHmac('SHA256', STORE_KEY).update(string).digest('hex')
谢谢
我在@Topaco 的帮助下解决了这个问题。
const key = CryptoJS.enc.Utf8.parse(STORE_KEY);
const iv = CryptoJS.enc.Utf8.parse(STORE_IV);
result = CryptoJS.AES.encrypt(string, key, { iv: key });
result = result.ciphertext.toString();