Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
158
rated 0 times [  165] [ 7]  / answers: 1 / hits: 31476  / 6 Years ago, tue, january 30, 2018, 12:00:00

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);

More From » encryption

 Answers
94

Solved.



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;
}

[#55308] Saturday, January 27, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kadinl

Total Points: 321
Total Questions: 117
Total Answers: 103

Location: Nepal
Member since Mon, Jan 4, 2021
3 Years ago
;