AES Encryption
Info
This how-to assumes you are familiar with setting up a project either manually or via VoltScript Dependency Management. If you are not, follow the Intro to VoltScript Tutorials.
Introduction
Advanced Encryption Standard (AES) is a standard encryption algorithm using symmetric keys. This means it uses the same keys for both encryption and decryption. VoltScript provides both 128-bit and 256-bit cryptographic keys. It also provides the ability to generate the initialization vector, a randomized starting point for the encryption.
VoltScript dependencies
Incorporating HashVSE is straightforward. You just need to add the following JSON object to the vsesDependencies
element in your atlas.json
.
"HashVSE": {
"library": "25278",
"version": "1.0.1",
"module": "25882/20850",
"repository":"volt-mx-marketplace"
}
You'll need to add to your repositories object in the atlas.json of your project:
{
"id": "volt-mx-marketplace",
"type": "marketplace",
"url": "https://community.demo-hclvoltmx.com/marketplace/asset"
}
To use the extension in your script, enter UseVSE "*HashVSE"
.
Encrypting files
Initialization vector and keys
For AES encryption, you will need an initialization vector and either a 128-bit or 256-bit key, stored as a byte array. The following code shows how to initialize all:
Sub initializeForAES(iv as Variant, key128 as Variant, key256 as Variant)
Dim cu as New CryptoUtilities()
iv = cu.createAESIV()
key128 = cu.createAES128Key()
key256 = cu.createAES256Key()
End Sub
Encrypting a file
The following code will encrypt the file, returning True
if the file could be successfully encrypted:
Function encryptFile(iv as Variant, key as Variant, sourceFile as String, targetFile as String)
Dim cu as New CryptoUtilities()
Call cu.fileEncryptAES(sourceFile, targetFile, iv, key, True)
End Function
The same function is used for encrypting with a 128-bit key or a 256-bit key. You just pass the relevant key.
Decrypting a file
Decrypting a file will require the same initialization vector and key used to encrypt it, both as byte arrays. This will probably not be shared as a byte array, but a string. HashUtilities provides an option to convert the string to a byte array, as shown in the Hash how-to.
Decrypting the file with the keys, as byte arrays, can be done with the following code:
Function decryptFile(sourceFile as String, targetFile as String, iv as Variant, key as Variant)
Dim cu as New CryptoUtilities()
Call cu.fileDecryptAES(sourceFile, targetFile, iv, key, True)
End Function
The complete implementations of the code snippets are available on GitHub.