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.
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.
Initializing an instance
To encrypt and decrypt data you’ll need an instance of the crypter object and a pass code.
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.
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.
Crypting a number
It’s not just text that can be encrypted and decrypted – so can numbers, booleans and plain objects
Crypting 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.
id – 1P9xp6HOnd6RqxQnBYYYjc9sDatGZ2E3PNVBkTfvtvSGRdtzrPEvsDYwZ