Skip to content
David Whitehead edited this page May 4, 2019 · 1 revision

Generation Functions

CreateAESStringKey

This function generates the 256 bit AES CBC key and returns it in base64 string format.

Syntax

string CreateAESStringKey()

Normal Example

// Generates the AES key for the example
string key = AES.CreateAESStringKey();

CreateAESByteKey

This function generates the 256 bit AES CBC key and returns it in a byte array format.

Syntax

byte[] CreateAESByteKey()

Normal Example

// Generates the AES key for the example
byte[] key= AES.CreateAESByteKey();

CreateAESStringIV

This function generates the IV for the AES CBC algorithm and returns it in a base64 string format.

Syntax

string CreateAESStringIV()

Normal Example

// Generates a AES CBC IV for the example
string iv = AES.CreateAESStringIV();

CreateAESByteIV

This function generates an IV for the AES CBC algorithm and returns it in a byte array format.

Syntax

byte[] CreateAESByteIV()

Normal Example

// Generates a AES CBC IV for the example
byte[] iv = AES.CreateAESByteIV();

Encryption Functions

NOTE: All encryption functions will accept the key and IV in either base64 string or byte array formats.

EncryptToString

This function encrypts a specified object into a base64 encrypted string.

Syntax

string EncryptToString(object data, object key, object IV)

Example Usage

AES AES = new AES();

string exampleString = "this is a test string.";

//Generates a key and IV for the example
string key = AES.CreateAESStringKey();
string IV = AES.CreateAESStringIV();

//Encrypts the string
string encryptedString = AES.EncryptToString(exampleString, key, IV);

EncryptToBytes

This function encrypts a specified object into a byte array.

Syntax

byte[] EncryptToBytes(object data, object key, object IV)

Example Usage

AES AES = new AES();

string exampleString = "this is a test string.";

//Generates a key and IV for the example
string key = AES.CreateAESStringKey();
string IV = AES.CreateAESStringIV();

//Encrypts the string into bytes
byte[] encryptedBytes = AES.EncryptToBytes(exampleString, key, IV);

Decryption Functions

NOTE: All decryption functions will accept the key, IV and encrypted data in either base64 string or byte array formats.

DecryptToType

This function decrypts the supplied encryption string or byte array into a specified object type.

Note: The type for the decryption MUST be the original type the data was before it was encrypted.

Syntax

T DecryptToType<T>(object data, object key, object IV)

Example Usage

AES AES = new AES();

string exampleString = "this is a test string.";

//Generates a key and IV for the example
string key = AES.CreateAESStringKey();
string IV = AES.CreateAESStringIV();

//Encrypts the string
string encryptedString = AES.EncryptToString(exampleString, key, IV);

//Decrypts the string base64 encrypted string back into a string format
string decryptedString = AES.DecryptToType<string>(encryptedString, key, IV);

Object Example

//NOTE: for a class object to be encrypted it has to have the Serializable flag on it as thats how we process the data before encryption
[Serializable]
public class TestUser
{
	public string Name { get; set; }
	public int Age { get; set; }
}

private static bool ObjectEncryption()
{
        AES AES = new AES();

        //Generates a key and IV for the example
        string key = AES.CreateAESStringKey();
        string IV = AES.CreateAESStringIV();

	//Creates an object
	TestUser user = new TestUser()
	{
		Name = "David",
		Age = 99
	};

	//Encrypts the user object into a string
	string encryptedString = AES.EncryptToString(user, key, IV);

	//Decrypts the it back into a user object
	TestUser decryptedObject = AES.DecryptToType<TestUser>(encryptedString, key, IV);
}