Skip to content
Advertisement

Encrypt and decrypt a string using simple Javascript without using any external library

I want to create a function to encrypt a string which will shorten the string into alphanumeric character and also create a function decrypt which will get back the encrypted string.

Here is what I coded by taking reference online.

JavaScript

Currently the output from the sting=”awesome” is

JavaScript

I want similar encryption but must be only in alphanumeric values and not symbols.

Advertisement

Answer

I don’t know what is your goal (is it make the string shorter, but binary or encrypted and in ascii range), so if it’s the later, than you could use base64 encoding:

JavaScript

Or if you trully want alphanumerical, than you can simply convert it into HEX:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement