Quick intro guide: Cryptography

Robin has lots of cryptography options. This just scratches the surface of what it can do.
Here, I’m using the default AES encryption with a 128-bit block size, and a 256-bit key size.

Text encoding errors can break your code: If I try the code for this guide with the default, i.e.

Encoding:Cryptography.EncryptionEncoding.Unicode

it will throw an error on my system. Normally I use UTF8, so that’s what I’ve coded in here. Your system may well be different.

Incorrect block size can break your code. AES supports 128-bit block size, so I’ve just used that.

Key size may or may not break your code. Here, as you can see below, a key shorter than 256 bits will still work. A key greater than 256 bits will throw an error.

Here’s a code screenshot:

The code itself:

set MyKey to "I am Godzilla!!! Are you Rodan??"
#set MyKey to "I am Godzilla!!! Are you Rodan?? I certainly hope not." # Key is too long!
#set MyKey to "Howard Hughes" # Here, a key shorter than 256 bytes will be padded.
set MyText to '''Multiline Text.

Check this agains your text before encryption.

It should be identical.'''

# Check your encoding, and make sure your block size matches one the algorithm supports.
# In this code, the default AES algorithm supports a 128-bit block size.
# See e.g. https://en.wikipedia.org/wiki/Block_size_(cryptography)

Cryptography.EncryptText    Encoding:Cryptography.EncryptionEncoding.UTF8 \
                            Algorithm:Cryptography.SymmetricEncryptionAlgorithm.AES \
                            TextToEncrypt:  MyText\
                            EncryptionKey:  MyKey\
                            CipherMode:Cryptography.CipherMode.CBC \
                            PaddingMode:Cryptography.PaddingMode.PKCS7 \
                            BlockSize:  128\
                            KeySize:  256\
                            EncryptedText=> EncryptedText

# Message box with encrypted text

Display.ShowMessage         Title:'Encrypted Text         ' \
                            Message:EncryptedText \
                            Icon:Icon.None \
                            Buttons:Buttons.OK \
                            DefaultButton:DefaultButton.Button1 \
                            IsTopMost:False \
                            ButtonPressed=> ButtonPressed

Cryptography.DecryptText    Encoding:Cryptography.EncryptionEncoding.UTF8 \
                            Algorithm:Cryptography.SymmetricEncryptionAlgorithm.AES \
                            TextToDecrypt:  EncryptedText\
                            DecryptionKey:  MyKey\
                            CipherMode:Cryptography.CipherMode.CBC \
                            Padding:Cryptography.PaddingMode.PKCS7 \
                            BlockSize:  128\
                            KeySize:  256\
                            DecryptedText=> DecryptedText

#Message box with decrypted text

Display.ShowMessage         Title:'Decrypted Text         ' \
                            Message:DecryptedText \
                            Icon:Icon.None \
                            Buttons:Buttons.OK \
                            DefaultButton:DefaultButton.Button1 \
                            IsTopMost:False \
                            ButtonPressed=> ButtonPressed

and a couple of screenshots showing encrypted and decrypted text.

encrypted

decrypted

Best regards,
burque505

4 Likes

Excellent quick intro to the Cryptography module @burque505!
Great job!

Best regards,
J.


James Papadimitriou
Technical Evangelist