private void button_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (sender == btnAssignKey)
         {
             rsaParams.AssignNewKey();
         }
         else if (sender == btnEncrypt)
         {
             var encryptBlock = HybridEncryptionAddedIntegrity.EncryptData(txtMessage.Text, rsaParams);
             txtEncryptedSessionKey.Text = Convert.ToBase64String(encryptBlock.EncryptedSessionKey);
             txtEncryptedData.Text       = Convert.ToBase64String(encryptBlock.EncryptedData);
             txtIv.Text   = Convert.ToBase64String(encryptBlock.Iv);
             txtHmac.Text = Convert.ToBase64String(encryptBlock.Hmac);
         }
         else if (sender == btnDecrypt)
         {
             var encryptBlock = new EncryptedPacket
             {
                 EncryptedData       = Convert.FromBase64String(txtEncryptedData.Text),
                 EncryptedSessionKey = Convert.FromBase64String(txtEncryptedSessionKey.Text),
                 Iv   = Convert.FromBase64String(txtIv.Text),
                 Hmac = Convert.FromBase64String(txtHmac.Text)
             };
             var decrypted = HybridEncryptionAddedIntegrity.DecryptData(encryptBlock, rsaParams);
             txtResult.Text = decrypted;
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show("Error\n" + exc.Message);
     }
 }
Exemple #2
0
        public static string DecryptData(EncryptedPacket encryptedPacket, RSAWithRSAParameterKey rsaParams)
        {
            var aes = new AesEncryption();
            // Decrypt AES key with RSA and then decrypt data with AES.
            var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey);

            using (var hmac = new HMACSHA256(decryptedSessionKey))
            {
                var hmacToCheck = hmac.ComputeHash(encryptedPacket.EncryptedData);
                if (!CompareArrays.Compare(encryptedPacket.Hmac, hmacToCheck))
                {
                    throw new CryptographicException("HMAC for decryption does not match encrypted packet.");
                }
            }

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

            return(Encoding.UTF8.GetString(decryptedData));
        }
Exemple #3
0
        public static EncryptedPacket EncryptData(string original, RSAWithRSAParameterKey rsaParams)
        {
            var aes             = new AesEncryption();
            var sessionKey      = RandomCryptography.Random.GenerateRandomNumber(32);
            var encryptedPacket = new EncryptedPacket
            {
                Iv = RandomCryptography.Random.GenerateRandomNumber(16)
            };

            // Encrypt data with AES and AES key with RSA.
            encryptedPacket.EncryptedData       = aes.Encrypt(Encoding.UTF8.GetBytes(original), sessionKey, encryptedPacket.Iv);
            encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);

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

            return(encryptedPacket);
        }