public EncryptedPacket EncryptDataWithIntegrity(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); // create an HMAC using the session key and store an HMAC of the encrypted data in the packet using (HMACSHA256 hmac = new HMACSHA256(sessionKey)) { EP.Hmac = hmac.ComputeHash(EP.EncryptedData); } return(EP); }
public EncryptedPacket EncryptDataWithSignature(byte[] originalMessage, RsaWithRsaParameterKey rsaParams, DigitalSignatures DS) { // 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); // Sender generates hash mac using our session key using (HMACSHA256 hmac = new HMACSHA256(sessionKey)) { EP.Hmac = hmac.ComputeHash(EP.EncryptedData); } //Sender signs the message with a digital signature EP.Signature = DS.SignData(EP.Hmac); return(EP); }
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); }