JavaScript string encryption and decryption?

I am interested in creating a small application for personal use that will encrypt and decrypt information on the client side using JavaScript. The encrypted information will be stored in the database on the server, but not in the decrypted version.

It doesn't have to be super-duper safe, but I would like to use the current continuous algorithm.

Ideally, I could do something like

var gibberish = encrypt(string, salt, key); 

to generate a coded string and something like

 var sensical = decrypt(gibberish, key); 

to decode it later.

So far I have seen this: http://bitwiseshiftleft.imtqy.com/sjcl/

Any other libraries I should look at?

+121
javascript encryption
Aug 16 '13 at 17:40
source share
7 answers

  var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase"); //U2FsdGVkX18ZUVvShFSES21qHsQEqZXMxQ9zgHy+bu0= var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase"); //4d657373616765 document.getElementById("demo1").innerHTML = encrypted; document.getElementById("demo2").innerHTML = decrypted; document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8); 
 Full working sample actually is: <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script> <br><br> <label>encrypted</label> <div id="demo1"></div> <br> <label>decrypted</label> <div id="demo2"></div> <br> <label>Actual Message</label> <div id="demo3"></div> 
+139
Oct 11 '14 at 17:46
source share
— -

What about CryptoJS ?

This is a reliable crypto library, with lots of functionality. It implements hashes, HMAC, PBKDF2 and ciphers. In this case, ciphers are what you need. Check out the quick start on the project home page.

You could do something like with AES:

 <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <script> var encryptedAES = CryptoJS.AES.encrypt("Message", "My Secret Passphrase"); var decryptedBytes = CryptoJS.AES.decrypt(encryptedAES, "My Secret Passphrase"); var plaintext = decryptedBytes.toString(CryptoJS.enc.Utf8); </script> 

As for security, at the time of writing my article, the AES algorithm is considered continuous

Edit:

It seems that the online address does not work, and you can use the downloaded files for encryption at the link below and put the corresponding files in the root folder of the application.

https://code.google.com/archive/p/crypto-js/downloads

or used another CDN, for example https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/aes-min.js

+54
May 14 '14 at 12:48
source share

I created a simple text cipher / decoder utilities. There are no dependencies with any external library.

These are functions

 let cipher = salt => { let textToChars = text => text.split('').map(c => c.charCodeAt(0)) let byteHex = n => ("0" + Number(n).toString(16)).substr(-2) let applySaltToChar = code => textToChars(salt).reduce((a,b) => a ^ b, code) return text => text.split('') .map(textToChars) .map(applySaltToChar) .map(byteHex) .join('') } let decipher = salt => { let textToChars = text => text.split('').map(c => c.charCodeAt(0)) let saltChars = textToChars(salt) let applySaltToChar = code => textToChars(salt).reduce((a,b) => a ^ b, code) return encoded => encoded.match(/.{1,2}/g) .map(hex => parseInt(hex, 16)) .map(applySaltToChar) .map(charCode => String.fromCharCode(charCode)) .join('') } 

And you can use them as follows:

 // To create a cipher let myCipher = cipher('mySecretSalt') //Then cipher any text: myCipher('the secret string') // --> "7c606d287b6d6b7a6d7c287b7c7a61666f" //To decipher, you need to create a decipher and use it: let myDecipher = decipher('mySecretSalt') myDecipher("7c606d287b6d6b7a6d7c287b7c7a61666f") // --> 'the secret string' 
+20
Jan 03 '19 at 16:46
source share

Modern browsers now support the crypto.subtle , which provides its own encryption and decryption functions (no less asynchronously!) Using one of the following methods: AES-CBC, AES-CTR, AES-GCM or RSA-OAEP.

https://www.w3.org/TR/WebCryptoAPI/#dfn-Crypto

+15
Sep 10 '18 at 21:12
source share

CryptoJS is no longer supported. If you want to continue using it, you can switch to this URL:

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>

+2
May 04 '18 at 19:26
source share

Use SimpleCrypto

Using encrypt () and decrypt ()

To use SimpleCrypto, first create an instance of SimpleCrypto with a secret key (password). The private key parameter MUST be specified when instantiating SimpleCrypto.

To encrypt and decrypt data, simply use the encrypt () and decrypt () functions from the instance. This will use the AES-CBC encryption algorithm.

 var _secretKey = "some-unique-key"; var simpleCrypto = new SimpleCrypto(_secretKey); var plainText = "Hello World!"; var chiperText = simpleCrypto.encrypt(plainText); console.log("Encryption process..."); console.log("Plain Text : " + plainText); console.log("Cipher Text : " + cipherText); var decipherText = simpleCrypto.decrypt(cipherText); console.log("... and then decryption..."); console.log("Decipher Text : " + decipherText); console.log("... done."); 
+1
May 13 '19 at 11:16
source share

Simple functions

 function Encrypt(value) { var result="";  for(i=0;i<value.length;i++)  { if(i<value.length-1) { result+=value.charCodeAt(i)+10; result+="-"; } else { result+=value.charCodeAt(i)+10; }  }  return result; } function Decrypt(value) { var result=""; var array = value.split("-");  for(i=0;i<array.length;i++)  {    result+=String.fromCharCode(array[i]-10);  }  return result; } 
0
Jun 05 '19 at 10:29
source share



All Articles