Exemplo n.º 1
0
        /// <summary>
        /// Decrypts a byte array using the specified encryption algorithm,
        /// key, and initialization vector.
        /// </summary>
        /// <param name="input">The encrypted array,</param>
        /// <param name="algorithm">The symmetric algorithm name.</param>
        /// <param name="key">The encryption key.</param>
        /// <param name="IV">The initialization vector.</param>
        /// <returns>The decrypted output.</returns>
        public static byte[] Decrypt(byte[] input, string algorithm, byte[] key, byte[] IV)
        {
            if (input.Length == 0)
            {
                return(new byte[0]);
            }

            using (BlockDecryptor decryptor = new BlockDecryptor(algorithm, key, IV))
                return(decryptor.Decrypt(input));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Decrypts a byte array with eight bytes of cryptographic salt using
        /// the specified encryption algorithm, key, and initialization vector.
        /// </summary>
        /// <param name="input">The encrypted array,</param>
        /// <param name="algorithm">The symmetric algorithm name.</param>
        /// <param name="key">The encryption key.</param>
        /// <param name="IV">The initialization vector.</param>
        /// <returns>The decrypted output.</returns>
        /// <remarks>
        /// <note>
        /// This method returns an zero length result if the input
        /// array has zero length.
        /// </note>
        /// </remarks>
        public static byte[] DecryptWithSalt8(byte[] input, string algorithm, byte[] key, byte[] IV)
        {
            if (input.Length == 0)
            {
                return(new byte[0]);
            }
            else if (input.Length < 8)
            {
                throw new ArgumentException("Block does not contain 8 bytes of salt.");
            }

            using (BlockDecryptor decryptor = new BlockDecryptor(algorithm, key, IV))
                return(Helper.Extract(decryptor.Decrypt(input), 8));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Decrypts data encrypted using <see cref="Encrypt(SymmetricKey,byte[],int)" />.
        /// </summary>
        /// <param name="symmetricKey">The symmetric algorithm arguments.</param>
        /// <param name="cipherText">The encrypted data.</param>
        /// <returns>The decrypted result.</returns>
        public static byte[] Decrypt(SymmetricKey symmetricKey, byte[] cipherText)
        {
            EnhancedMemoryStream input     = new EnhancedMemoryStream(cipherText);
            EnhancedMemoryStream ms        = new EnhancedMemoryStream(cipherText.Length);
            BlockDecryptor       decryptor = null;

            try
            {
                // Read the header fields

                if (input.ReadInt32() != Magic)
                {
                    throw new CryptographicException(BadFormatMsg);
                }

                if (input.ReadInt32() != 0)
                {
                    throw new CryptographicException("Unsupported secure data format version.");
                }

                decryptor = new BlockDecryptor(symmetricKey);

                // Decrypt the contents

                ms.WriteBytesNoLen(decryptor.Decrypt(input.ReadBytes32()));
                ms.Position = 0;

                if (ms.ReadInt32() != Magic)
                {
                    throw new CryptographicException("Secure data content is corrupt.");
                }

                ms.Position += 8;   // Skip over the salt

                return(ms.ReadBytes32());
            }
            finally
            {
                if (decryptor != null)
                {
                    decryptor.Dispose();
                }

                input.Close();
                ms.Close();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Decrypts a byte array encrypted using <see cref="Encrypt(string ,byte[],string,int,int,out SymmetricKey)" />.
        /// </summary>
        /// <param name="rsaKey">The decrypting RSA key as XML or as a secure key container name.</param>
        /// <param name="cipherText">The encrypted data.</param>
        /// <param name="symmetricKey">Returns as the symmetric encryption algorithm arguments.</param>
        /// <returns>The decrypted data.</returns>
        /// <exception cref="CryptographicException">Thrown is the encrypted data block is incorrectly formatted.</exception>
        /// <remarks>
        /// Note that applications should take some care to ensure that the <paramref name="symmetricKey" />
        /// value return is disposed so that the symmetric encryption key will be cleared.
        /// </remarks>
        public static byte[] Decrypt(string rsaKey, byte[] cipherText, out SymmetricKey symmetricKey)
        {
            EnhancedMemoryStream input     = new EnhancedMemoryStream(cipherText);
            EnhancedMemoryStream ms        = new EnhancedMemoryStream(cipherText.Length);
            BlockDecryptor       decryptor = null;

            byte[] symKey;
            byte[] symIV;
            string algorithm;

            try
            {
                // Read the header fields

                if (input.ReadInt32() != Magic)
                {
                    throw new CryptographicException(BadFormatMsg);
                }

                if (input.ReadInt32() != 0)
                {
                    throw new CryptographicException("Unsupported secure data format version.");
                }

                // Decrypt the encryption info

                ms.WriteBytesNoLen(AsymmetricCrypto.Decrypt(CryptoAlgorithm.RSA, rsaKey, input.ReadBytes16()));
                ms.Position = 0;

                algorithm    = ms.ReadString16();
                symKey       = ms.ReadBytes16();
                symIV        = ms.ReadBytes16();
                symmetricKey = new SymmetricKey(algorithm, symKey, symIV);
                decryptor    = new BlockDecryptor(algorithm, symKey, symIV);

                // Decrypt the contents

                ms.SetLength(0);
                ms.WriteBytesNoLen(decryptor.Decrypt(input.ReadBytes32()));
                ms.Position = 0;

                if (ms.ReadInt32() != Magic)
                {
                    throw new CryptographicException("Secure data content is corrupt.");
                }

                ms.Position += 8;   // Skip over the salt

                return(ms.ReadBytes32());
            }
            finally
            {
                if (decryptor != null)
                {
                    decryptor.Dispose();
                }

                input.Close();
                ms.Close();
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Decrypts a byte array with eight bytes of cryptographic salt using
 /// the specified encryption algorithm, key, and initialization vector
 /// and then converts it to a string using UTF-8 encoding.
 /// </summary>
 /// <param name="input">The encrypted string,</param>
 /// <param name="algorithm">The symmetric algorithm name.</param>
 /// <param name="key">The encryption key.</param>
 /// <param name="IV">The initialization vector.</param>
 /// <returns>The decrypted string.</returns>
 public static string DecryptStringWithSalt8(byte[] input, string algorithm, byte[] key, byte[] IV)
 {
     using (BlockDecryptor decryptor = new BlockDecryptor(algorithm, key, IV))
         return(Helper.FromUTF8(Helper.Extract(decryptor.Decrypt(input), 8)));
 }