public byte[] DecryptDataWithSignature(EncryptedPacket EP, RsaWithRsaParameterKey rsaParams, DigitalSignatures DS) { // Receiver decrypts AES session key with RSA byte[] decryptedSessionKey = rsaParams.DecryptData(EP.EncryptedSessionKey); // Receiver compares using (HMACSHA256 hmac = new HMACSHA256(decryptedSessionKey)) { byte[] hmacToCheck = hmac.ComputeHash(EP.EncryptedData); if (!CompareHashes(EP.Hmac, hmacToCheck)) { throw new CryptographicException("HMAC for decryption does not match encrypted packet HMAC"); } if (!DS.VerifySignature(EP.Hmac, EP.Signature)) { throw new CryptographicException("Digital Signature cannot be verified"); } } // Receiver decrypts the data wuth AES using the decrypted session key byte[] decryptedData = _cryptographyExample.DecryptUsingAES(EP.EncryptedData, decryptedSessionKey, EP.IV); return(decryptedData); }
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"); }