我想将数字编码为字符。
代码:
const CryptoJS = require('crypto-js');
function msg() {
return '7543275'; // I want to encrypt this number to character
}
const msgLocal = msg();
// Encrypt
const ciphertext = CryptoJS.AES.encrypt(msgLocal, 'password');
// Decrypt
const bytes = CryptoJS.AES.decrypt(ciphertext.toString(), 'password');
const plaintext = bytes.toString(CryptoJS.enc.Utf8);
console.log(plaintext);
答案 0 :(得分:4)
解决。
const CryptoJS = require('crypto-js');
// OUTPUT
console.log(encrypt()); // 'NzUzMjI1NDE='
console.log(decrypt()); // '75322541'
function encrypt() {
// INIT
const myString = '75322541'; // Utf8-encoded string
// PROCESS
const encryptedWord = CryptoJS.enc.Utf8.parse(myString); // encryptedWord Array object
const encrypted = CryptoJS.enc.Base64.stringify(encryptedWord); // string: 'NzUzMjI1NDE='
return encrypted;
}
function decrypt() {
// INIT
const encrypted = 'NzUzMjI1NDE='; // Base64 encrypted string
// PROCESS
const encryptedWord = CryptoJS.enc.Base64.parse(encrypted); // encryptedWord via Base64.parse()
const decrypted = CryptoJS.enc.Utf8.stringify(encryptedWord); // decrypted encryptedWord via Utf8.stringify() '75322541'
return decrypted;
}