-
Notifications
You must be signed in to change notification settings - Fork 1
AES
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();
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();
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();
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();
NOTE: All encryption functions will accept the key and IV in either base64 string or byte array formats.
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);
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);
NOTE: All decryption functions will accept the key, IV and encrypted data in either base64 string or byte array formats.
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);
}