public byte[] DecryptData(EncryptedPacket encryptedPacket, RsaWithRsaParameterKey rsaParams, DigitalSignature digitalSignature)
        {
            //Decrypt AES Key with RSA.
            var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey);

            using (var hmac = new HMACSHA256(decryptedSessionKey))
            {
                var hmacToCheck = hmac.ComputeHash(encryptedPacket.EncryptedData);

                if (!Compare(encryptedPacket.Hmac, hmacToCheck))
                {
                    throw new CryptographicException(
                              "HMAC for decryption does not match encrypted package");
                }

                if (!digitalSignature.VerifySignature(encryptedPacket.Hmac, encryptedPacket.Signature))
                {
                    throw new CryptographicException(
                              "Digital Signature can not be verified");
                }
            }

            var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData, decryptedSessionKey, encryptedPacket.Iv);

            return(decryptedData);
        }
        public EncryptedPacket EncryptData(byte[] original, RsaWithRsaParameterKey rsaParams, DigitalSignature digitalSignature)
        {
            var sessionKey = _aes.GenerateRandomNumber(32);

            var encryptedPacket = new EncryptedPacket {
                Iv = _aes.GenerateRandomNumber(16)
            };

            encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv);

            encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);

            using (var hmac = new HMACSHA256(sessionKey))
            {
                encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData);
            }

            encryptedPacket.Signature = digitalSignature.SignData(encryptedPacket.Hmac);

            return(encryptedPacket);
        }
        static void Main()
        {
            const string original = "Petras";

            //Testavimas

            var hybrid = new HybridEncryption();

            var rsaParams = new RsaWithRsaParameterKey();

            rsaParams.AssignNewKey();

            var digitalSignature = new DigitalSignature();

            digitalSignature.AssignNewKey();

            Console.WriteLine("Hybrid Encryption");
            Console.WriteLine();

            try
            {
                var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original),
                                                        rsaParams, digitalSignature);

                var decrypted = hybrid.DecryptData(encryptedBlock, rsaParams, digitalSignature);

                Console.WriteLine("Original Message = " + original);
                Console.WriteLine();
                Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrypted));
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine("Error : " + ex.Message);
            }

            Console.ReadKey();
        }