通过crypto-js的base64编码器 [英] base64 Encoder via crypto-js
本文介绍了通过crypto-js的base64编码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将数字编码为字符.
- 如何在输出中编码为base64?
代码:
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);
推荐答案
已解决.
const CryptoJS = require('crypto-js');
// OUTPUT
console.log(encode()); // 'NzUzMjI1NDE='
console.log(decode()); // '75322541'
function encode() {
// INIT
const myString = '75322541'; // Utf8-encoded string
// PROCESS
const encodedWord = CryptoJS.enc.Utf8.parse(myString); // encodedWord Array object
const encoded = CryptoJS.enc.Base64.stringify(encodedWord); // string: 'NzUzMjI1NDE='
return encoded;
}
function decode() {
// INIT
const encoded = 'NzUzMjI1NDE='; // Base64 encoded string
// PROCESS
const encodedWord = CryptoJS.enc.Base64.parse(encoded); // encodedWord via Base64.parse()
const decoded = CryptoJS.enc.Utf8.stringify(encodedWord); // decode encodedWord via Utf8.stringify() '75322541'
return decoded;
}
这篇关于通过crypto-js的base64编码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文