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 static void EncryptDecryptWithAES() { var des = new AESEncryption(); var key = des.GenerateRandomNumber(32); var iv = des.GenerateRandomNumber(16); const string original = "Text to encrypt"; var encrypted = des.Encrypt(Encoding.UTF8.GetBytes(original), key, iv); var decrypted = des.Decrypt(encrypted, key, iv); var decryptedMessage = Encoding.UTF8.GetString(decrypted); Console.WriteLine("AES Encryption Demonstration in .NET"); Console.WriteLine("------------------------------------"); Console.WriteLine(); Console.WriteLine("Original Text = " + original); Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encrypted)); Console.WriteLine("Decrypted Text = " + decryptedMessage); }
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); }