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); }
private static void RunDigitalSignature() { Console.WriteLine("Digital Signatures started"); Console.WriteLine(); Console.WriteLine(String.Format("Message before encryption: {0}", _signatureMessage)); byte[] messageToSign = Encoding.UTF8.GetBytes(_signatureMessage); byte[] hashedDocument; using (SHA256 sha256 = SHA256.Create()) { hashedDocument = sha256.ComputeHash(messageToSign); } DigitalSignatures DS = new DigitalSignatures(); DS.AssignNewKey(); byte[] signature = DS.SignData(hashedDocument); Console.WriteLine(String.Format("Digital Signature: {0}", Encoding.UTF8.GetString(signature))); bool isSignatureVerified = DS.VerifySignature(hashedDocument, signature); Console.WriteLine(String.Format("Verify Signature Results: {0}", isSignatureVerified.ToString())); Console.WriteLine(); Console.WriteLine("Digital Signatures ended"); }