示例#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));
        }