public byte[] DecryptData(EncryptedPacket encryptedpacket, RSAWithRSAParameterKey rsaParams) { // Decrypt AES Key with RSA var decryptedSessionKey = rsaParams.DecryptData(encryptedpacket.EncryptedSessionKey); // Decrypt our data with AES using the decrypted session key var decryptedData = _aes.Decrypt(encryptedpacket.EncryptedData, decryptedSessionKey, encryptedpacket.Iv); return(decryptedData); }
public byte[] DecryptData(EncryptedPacketIntegrityCheck encryptedpacket, RSAWithRSAParameterKey rsaParams) { // Decrypt AES Key with RSA var decryptedSessionKey = rsaParams.DecryptData(encryptedpacket.EncryptedSessionKey); using (var hmac = new HMACSHA256(decryptedSessionKey)) { var hmacToCheck = hmac.ComputeHash(encryptedpacket.EncryptedData); if (!Comparer(encryptedpacket.Hmac, hmacToCheck)) { throw new CryptographicException("HMAC for decryption does not match"); } } // Decrypt our data with AES using the decrypted session key var decryptedData = _aes.Decrypt(encryptedpacket.EncryptedData, decryptedSessionKey, encryptedpacket.Iv); return(decryptedData); }
public static void EncryptDecryptWithRSAWithRSAParameterKey() { var rsaParams = new RSAWithRSAParameterKey(); const string original = "Text to encrypt"; rsaParams.AssignNewKey(); var encryptedRsaParams = rsaParams.EncryptData(Encoding.UTF8.GetBytes(original)); var decryptedRsaParams = rsaParams.DecryptData(encryptedRsaParams); Console.WriteLine("RSA Encryption Demonstration in .NET"); Console.WriteLine("------------------------------------"); Console.WriteLine(); Console.WriteLine("In Memory Key"); Console.WriteLine(); Console.WriteLine("Original Text = " + original); Console.WriteLine(); Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encryptedRsaParams)); Console.WriteLine(); Console.WriteLine("Decrypted Text = " + Encoding.Default.GetString(decryptedRsaParams)); Console.WriteLine(); Console.WriteLine(); }