by Cody123100
Implementation of AES-128 encryption
This library implements the AES-128 encryption/decryption algorithm to encrypt and decrypt 128 bit long blocks of text. To encrypt and decrypt larger blocks of text it utilizes AES-128 in conjunction with cipher block chaining.

This is the initial implementation and as such may be prone to bugs. Ensure you can encrypt AND decrypt your data before you get rid of the unencrypted data.

AES stands for Advanced Encryption Standard and it is the standard by which the US government encrypts and decrypts their data. This flavor of AES utilizes 128 bit long keys (16 characters). AES's primary features are its speed and security. While this version is neither optimized nor as fast as non-DM implementations, this library is a suitable alternative to offloading encryption to a dll.

V0.2 - Added error reporting feature. Read the error reporting section of readme file for more details. Currently error reporting only reports if the user entered a key or initialization vector that was too short.

V1.0 - Added a hexadecimal mode. The default mode is still for ASCII characters. Read the input modes section of the readme file for more details. Added a test vectors function to the demo under hexadecimal mode to validate encryption correctness.
Your library fails to properly encrypt every AES-128 test vector I've tried. Did you actually test it before you released it? 55-aes-test-vectors
I didn't test it against those test vectors, no. The library takes in ASCII characters for the key, plain-text, and IV not hexadecimal values so I'm curious how you tested them. Because DM doesn't play nice with all ASCII characters it wouldn't be easy to test with test vectors containing characters like null, which DM doesn't store in variables at all as far as I recall.

What I did instead was a few test cases against this online AES encryption oracle which passes with the test vectors for ECB mode to verify that individual blocks are encrypted correctly using a snippet like this:

var/AES/aes = new()
var/data = "ThisIsThePlainTe"
var/key = "ThisIsTheKeyTest"
data = aes.text2mat(data)
key = aes.text2int(key)
world << "Encrypting"
data = aes.EncBlock(data, key)
world << "Encryption Complete"
var/out = ""
for(var/i = 1 to length(data))
for(var/j = 1; j <= 4; j++)
out += "[data[i][j]] "
out += "\n"
world << "Cipher-text:\n[out]"
cipher_text = data
world << "Decrypting"
data = aes.DecBlock(cipher_text,key)
world << "Decryption Complete"
world << "Plain-text: [aes.mat2text(data)]"

Note the cipher-text is treated as a 4x4 matrix of integers because of the aforementioned ASCII problem so I had to convert the integers into hex to verify against the online calculator.

This gave me confidence that my encryption of individual blocks was adequate because it produced the same results as an oracle that does satisfy those test vectors. I was mainly concerned with verifying that individual blocks are encrypted correctly so I did indeed skimp on the testing of CBC mode. I'm considering an update that's hexadecimal friendly which would make it possible to test against those test-vectors directly. Your complaint is extremely valid though, and I'll add a disclaimer to the hub and make it invisible until I do validate against those test vectors.
In response to Koil
Koil wrote:
Your library fails to properly encrypt every AES-128 test vector I've tried. Did you actually test it before you released it? 55-aes-test-vectors

I added a hexadecimal mode to the library and in that mode I added a Test Vectors function to the demo that executes all of the CBC test vectors from It all checked out. Note that the default mode is still the ASCII characters mode so to get the test vectors function you need to change the input mode.