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
1-QSJlFRNb-6MhZe4zra6tbTTriX_IHbZ7X3nNoFfKtlkA3DrbY-Z-S4i
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;
console.log(gc.getVersion())
shortcut to library namespace
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
check
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)
console.log(`${crypted}\n${decrypted}\n${secret}`);
encrypting and decrypting text
6768109653ac032e9c91d40a55caae1f6555a279401f1a02947b8fe1b8425459ifWclGYVRkBF00inAWvWkw==5b7f6745f2eb5bc1c9869904ab2f3c94d686426cb213cccea0caf80196497a49
some text
my secret passphrase
result
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
sc.setSecret(secret)
original = 'some other text'
crypted = sc.encrypt(original);
decrypted = sc.decrypt(crypted);
check (original, decrypted)
console.log(`${crypted}\n${decrypted}\n${secret}`);
use a random pass phrase
c37569775697910da9ccf92ba3e3450d9876fdc85060310d699f9d603930bd228idFdtzsrJbtRTpTIsMjqw==18b1cec9e8699636915c4ce642327e488f005dfc4c0d835701d024caf0f4694b
some other text
48c16e7e4db7adc0cb72e1d8af4dbd3e4b950e24c34698e09c4c5a8c3e0c4b6a
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)
console.log(`${crypted}\n${JSON.stringify(decrypted)}\n${secret}`);
crypting a number
980934885120fc95c955708fca3ab140d4aa968297e5be49cd56014137dd1f74 yF69VkiLWQiDNJYkmZrZ4YgpBt/M 2DRunf4WD0y3g=b93839dac298535d6b7a487bff38bc0809073bebb3d6052618e8739e08555cf4
3.141592653589793
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)
console.log(`${crypted}\n${JSON.stringify(decrypted)}\n${secret}`);
crypting an object
527d1f5f9f2b0f1da29f6bc05ea9474947f64b4c921d76df4e0e58a3700759ddhImqv5CYKyr7t7LdQI1NpYh0wTlXYhvvrBYOCLUBuLzvi0eGdaYHgdzDvPLG4O749hacJ5/H /GeudkDqWMcEiQGikb0fgrQ8wEwRDwFc s=fa4a2982fbc7cdd2b9322daada553eb75ad10007dc0af3367b807753ca4683e3
{"text":"an object to crypt","a":[1,2,3],"b":true,"c":{"a":"something"}}
48c16e7e4db7adc0cb72e1d8af4dbd3e4b950e24c34698e09c4c5a8c3e0c4b6a
an object
Links
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.
tester
github
ide
library
github
ide
1-QSJlFRNb-6MhZe4zra6tbTTriX_IHbZ7X3nNoFfKtlkA3DrbY-Z-S4i
Happy crypting.