private void RunEncryptionUsingAES() { Console.WriteLine("Encryption Using AES started"); Console.WriteLine(); CryptographyExample cryptographyExample = new CryptographyExample(); // here we are creating a key that is 32 bytes in length or 256 bits, which is the max size of an AES key byte[] key = cryptographyExample.GenerateRandomNumber(32); // AES using a 16 byte Initialization Vector byte[] initializationVector = cryptographyExample.GenerateRandomNumber(16); const string originalMessage = "Text To Encrypt"; Console.WriteLine(String.Format("Message before encryption: {0}", originalMessage)); byte[] encryptedMessage = cryptographyExample.EncryptUsingAES(Encoding.UTF8.GetBytes(originalMessage), key, initializationVector); Console.WriteLine(String.Format("Message after encryption: {0}", Encoding.UTF8.GetString(encryptedMessage))); byte[] decryptedMessage = cryptographyExample.DecryptUsingAES(encryptedMessage, key, initializationVector); Console.WriteLine(String.Format("Message after decryption: {0}", Encoding.UTF8.GetString(decryptedMessage))); Console.WriteLine(); Console.WriteLine("Encryption Using AES ended"); }
public EncryptedPacket EncryptData(byte[] originalMessage, RsaWithRsaParameterKey rsaParams) { // Sender generates AES session key byte[] sessionKey = _cryptographyExample.GenerateRandomNumber(32); // Sender generates Initialization Vector byte[] initializationVector = _cryptographyExample.GenerateRandomNumber(16); // Sender stores that IV in the packet object EncryptedPacket EP = new EncryptedPacket { IV = initializationVector }; // Sender encrypts data using AES EP.EncryptedData = _cryptographyExample.EncryptUsingAES(originalMessage, sessionKey, EP.IV); //Sender encrypts the session key with RSA EP.EncryptedSessionKey = rsaParams.EncryptData(sessionKey); return(EP); }