Example #1
0
        /// <summary>
        /// Decrypts authenticated ciphertext using the Rijndael cipher in CBC mode with a password derived
        /// HMAC SHA-512 salt.
        /// </summary>
        /// <param name="etmCiphertext">The EtM ciphertext to decrypt.</param>
        /// <param name="password">The password to decrypt the EtM ciphertext with.</param>
        /// <param name="keySize">The size of the cipher key used to create the EtM ciphertext.</param>
        /// <returns>The plaintext.</returns>
        public static new string Decrypt(byte[] etmCiphertext, string password, KeySize keySize)
        {
            // Generate AE keys
            var keyRing = AeKeyRing.Generate(password);

            // Extract the ciphertext and MAC from the EtM ciphertext
            var mac        = new byte[keyRing.MacKey.Length];
            var ciphertext = new byte[etmCiphertext.Length - mac.Length];

            using (var ms = new MemoryStream(etmCiphertext))
            {
                // Extract the ciphertext
                ms.Read(ciphertext, 0, ciphertext.Length);

                // Extract the MAC
                ms.Read(mac, 0, mac.Length);
            }

            // Calculate the MAC from the ciphertext
            var newMac = CalculateMac(ciphertext, keyRing.MacKey);

            // Authenticate ciphertext
            if (!mac.SequenceEqual(newMac))
            {
                throw new Exception("Authentication failed!");
            }

            // Decrypt the ciphertext
            return(Rijndael.Decrypt(ciphertext, keyRing.CipherKey, keySize));
        }
Example #2
0
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            string ToDecrypt = "";

            try
            {
                try { ToDecrypt = FilePath + lbFiles.SelectedItem.ToString(); }
                catch (Exception) { MessageBox.Show("You must select an item to encrypt or decrypt!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
                if (string.IsNullOrWhiteSpace(txtPassword.Text))
                {
                    MessageBox.Show("You must enter a password to decrypt or encrypt files!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return;
                }
                string Password = txtPassword.Text;
                if (Path.GetExtension(ToDecrypt).ToLower() == ".locked")
                {
                }
                else
                {
                    MessageBox.Show("File is already decrypted!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return;
                }
                string Output = FilePath + Path.GetFileNameWithoutExtension(ToDecrypt);
                Rijndael.Decrypt(ToDecrypt, Output, txtPassword.Text, KS);
                File.Delete(ToDecrypt);
                WriteToConsole("Decrypted file successfully");
            } catch { File.Delete(FilePath + Path.GetFileNameWithoutExtension(ToDecrypt)); }
        }