gshow8 shac
gshow8 shac

Reputation: 421

Node.js encrypt a string into alphanumerical

I am currently using NPM module crypto-js to encrypt strings. The codes are the following:

CryptoJS.AES.encrypt("hello", "secrete_key").toString()

This yields to an encrypted string of: U2FsdGVkX1/G276++MaPasgSVxcPUgP72A1wMaB8aAM=

Notice that in the encrypted string, there are special symbols such as / + and =. These generated special symbols cause a problem in my app. Is there a way that I can encrypt a string into only alphanumericals? I like the way crypto-js is doing, since it is simple. I only need to feed in the string, and a password hash. If crypto-js cannot, what other modules can achieve this in a simple and straight-forward way?

Upvotes: 1

Views: 1338

Answers (1)

aminits
aminits

Reputation: 184

CryptoJS also includes utilities for encoding/decoding so you can convert your encrypted string to a URL-safe format. See the CryptoJS docs for more info.

For example:

var CryptoJS = require("crypto-js");

let encrypted = CryptoJS.AES.encrypt("hello", "secrete_key").toString()
var encoded = CryptoJS.enc.Base64.parse(encrypted).toString(CryptoJS.enc.Hex);

console.log(encrypted)
console.log(encoded)

var decoded = CryptoJS.enc.Hex.parse(encoded).toString(CryptoJS.enc.Base64);
var decrypted = CryptoJS.AES.decrypt(decoded, "secrete_key").toString(CryptoJS.enc.Utf8);

console.log(decoded)
console.log(decrypted)

Upvotes: 4

Related Questions