admin管理员组文章数量:1180552
I want to Encode number to character.
- How Can I encode to base64 in output?
Code:
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);
I want to Encode number to character.
- How Can I encode to base64 in output?
Code:
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);
Share
Improve this question
edited Jan 31, 2018 at 0:39
Saeed Heidarizarei
asked Jan 30, 2018 at 15:00
Saeed HeidarizareiSaeed Heidarizarei
8,91622 gold badges66 silver badges111 bronze badges
1 Answer
Reset to default 33Solved.
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;
}
本文标签: javascriptbase64 Encoder via cryptojsStack Overflow
版权声明:本文标题:javascript - base64 Encoder via crypto-js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738178522a2067371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论