I am supporting CandidateX

CandidateX is a startup that focuses on creating inclusion-focused hiring solutions, designed to increase access to job opportunities for underestimated talent. Check them out if you have a few minutes to spare. They need visibility!

Encryption and decryption can be a bit cheesy to come to grips with. The most common library is probably cryptojs which has endless options. Since most people simply want to encrypt some data and decrypt it again, I figured it would be useful to publish an Apps Script library which uses cryptojs underneath but only presents limited and simple, but useful options.

Simple Crypto

Underneath I’m currently using a port of simple-crypto-js which in turn is based on crypto-js. Crypto is a pretty significant piece of code, so the library, bmSimpleCrypto is pretty big, so con’t be surprised at a relatively long load time.

How to use

You can clone testBmSimpleCrypto for some examples of how to use it, but I’ll go through it in the following sections.

Shortcut to library

After including a reference to the library


let’s start by making a shortcut to the library namespace, and showing the version of the encrpter I’m using.

  const gc = bmSimpleCrypto.GasCrypt;
shortcut to library namespace
simple-crypto 2.5.0

Initializing an instance

To encrypt and decrypt data you’ll need an instance of the crypter object and a pass code.

  // use a given passphrase and some text
let secret = 'my secret passphrase';
let sc = gc.newCrypto(secret);
create instance


For testing assurance there’s an example of a function to compare before and after. It’ll be referenced in the code following and you can get a copy (if you need it) in testBmSimpleCrypto

Encrypting and decrypting text

It’s simple – just pass the text you need encrypted and the pass phrase. You’ll get back an encrypted version that can be reversed using the same pass phrase.

  let original = 'some text'
let crypted = sc.encrypt(original);
let decrypted = sc.decrypt(crypted);
check (original, decrypted)
encrypting and decrypting text
some text
my secret passphrase

Generating a random pass phrase

It’s recommended to use a random pass phrase rather than one you might think of. Simple-crypto can generate one for you, which you can then use to set (or reset) your crypter secret.

  // use a random secret - better
secret = gc.randomString()
// we can make a new instance, or reset the secret on an existing instance
original = 'some other text'
crypted = sc.encrypt(original);
decrypted = sc.decrypt(crypted);
check (original, decrypted)
use a random pass phrase
some other text
using a random passphrase

Crypting a number

It’s not just text that can be encrypted and decrypted – so can numbers, booleans and plain objects

  // crypting a number
original = Math.PI
crypted = sc.encrypt(original);
decrypted = sc.decrypt(crypted);
check (original, decrypted)
crypting a number
980934885120fc95c955708fca3ab140d4aa968297e5be49cd56014137dd1f74 yF69VkiLWQiDNJYkmZrZ4YgpBt/M 2DRunf4WD0y3g=b93839dac298535d6b7a487bff38bc0809073bebb3d6052618e8739e08555cf4
48c16e7e4db7adc0cb72e1d8af4dbd3e4b950e24c34698e09c4c5a8c3e0c4b6a console.log(`${crypted}\n${JSON.stringify(decrypted)}\n${secret}`);
a number

Crypting an object

  // crypting an object
original = {text: 'an object to crypt', a:[1,2,3], b: true, c: { a: 'something'}}
crypted = sc.encrypt(original);
decrypted = sc.decrypt(crypted);
check (original, decrypted)
crypting an object
527d1f5f9f2b0f1da29f6bc05ea9474947f64b4c921d76df4e0e58a3700759ddhImqv5CYKyr7t7LdQI1NpYh0wTlXYhvvrBYOCLUBuLzvi0eGdaYHgdzDvPLG4O749hacJ5/H /GeudkDqWMcEiQGikb0fgrQ8wEwRDwFc s=fa4a2982fbc7cdd2b9322daada553eb75ad10007dc0af3367b807753ca4683e3
{"text":"an object to crypt","a":[1,2,3],"b":true,"c":{"a":"something"}}
an object


I won’t reproduce the library or the full tester code here – but you can find it on github or the IDE using the links below.








Happy crypting.