Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple JavaScript encryption & decryption without using key

I would like to know if it is possible to encrypt and decrypt a text, using pure JavaScript. I don't want to use a key. It may be an entry lever solution. But I simply want to encode a text "my-name-1" into some text format and want to retrieve the text from it. Is this possible, without using any js libraries?

like image 899
Alfred Avatar asked May 17 '13 09:05

Alfred


1 Answers

Without a key (or some secret for that matter), you wont get any kind of encryption.

What you mean is something like a different encoding. So maybe Base64 is something for you.

var baseString = 'my-name-1';

var encodedString = window.btoa( baseString ); // returns "bXktbmFtZS0x"

var decodedString = window.atob( encodedString );  // returns "my-name-1"

This is supported in all major browsers. IE support just in IE10+.

References:

  • Base64
  • window.btoa()
  • window.atob()
like image 56
Sirko Avatar answered Oct 23 '22 01:10

Sirko