Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 Encoder via crypto-js

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);
like image 825
Saeed Heidarizarei Avatar asked Jan 30 '18 15:01

Saeed Heidarizarei


People also ask

Is b64 URL safe?

Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. By consisting only of ASCII characters, base64 strings are generally url-safe, and that's why they can be used to encode data in Data URLs.

How do I encode a b64?

Encode to Base64 formatSimply enter your data then push the encode button. To encode binaries (like images, documents, etc.) use the file upload form a little further down on this page. Destination character set. Destination newline separator.

What is CryptoJS in JS?

CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.


1 Answers

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;
}
like image 89
Saeed Heidarizarei Avatar answered Oct 06 '22 23:10

Saeed Heidarizarei