public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams) { // Generate our session key var sessionKey = _aes.GenerateRandomNumber(32); // Create the encrypted packet and generate the IV var encryptedPacket = new EncryptedPacket { IV = _aes.GenerateRandomNumber(16) }; // Encrypt our data with AES encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.IV); // Encrypt the session key with RSA encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey); return(encryptedPacket); }
/// <summary> /// This is an example of what a sender would do to securely transmit data /// using a hybrid encryption solution, (combining symmetric (AES) encryption /// with asymmetric encryption (RSA)). /// </summary> /// <param name="data">Data to be encrypted</param> /// <param name="publicKey">The public key, used to encrypt the session key used to encrypt the data.</param> /// <returns>Encrypted Packet of data that can be securely transferred</returns> public EncryptedPacket EncryptData(byte[] data, RSAWithRSAParameterKey publicKey) { var encryptedPacket = new EncryptedPacket(); // Generate our unique 256 bits session key var sessionKey = _aes.GenerateRandomNumbers(32); // Generate the 128 bit Initialization Vector encryptedPacket.Iv = _aes.GenerateRandomNumbers(16); // Encrypt data using AES (symmetric encryption) session key and IV encryptedPacket.EncryptedData = _aes.Encrypt(data, sessionKey, encryptedPacket.Iv); // Encrypt the session key with the public RSA key encryptedPacket.EncryptedSessionKey = publicKey.EncryptData(sessionKey); // Generate a HMAC using the unique session key using (var hmac = new HMACSHA256(sessionKey)) { encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData); } return(encryptedPacket); }