/// <summary>
    ///   <para>Decrypts encrypted binary data, using specified symmetric algorithm.</para>
    /// </summary>
    /// <param name="self">Symmetric algorithm which have been used for encryption previously and should be used for decryption now.</param>
    /// <param name="source">Binary data to be decrypted.</param>
    /// <returns>Decrypted binary data.</returns>
    /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="source"/> is a <c>null</c> reference.</exception>
    /// <seealso cref="Decrypt(SymmetricAlgorithm, Stream, bool)"/>
    public static byte[] Decrypt(this SymmetricAlgorithm self, byte[] source)
    {
      Assertion.NotNull(self);
      Assertion.NotNull(source);

      return self.CreateDecryptor().TransformFinalBlock(source, 0, source.Length);
    }
 public static byte[] Decrypt(this SymmetricAlgorithm algorithm, byte[] inputBuffer)
 {
     using (var decryptor = algorithm.CreateDecryptor())
     {
         return decryptor.TransformFinalBlock(inputBuffer);
     }
 }
Example #3
0
 internal static byte[] Decrypt(this SymmetricAlgorithm alg, byte[] cipher, int blockSizeMultipler = 1)
 {
     using (ICryptoTransform decryptor = alg.CreateDecryptor())
     {
         return decryptor.Transform(cipher, blockSizeMultipler);
     }
 }
 public static Byte[] Decrypt(this AesManaged aes, Byte[] source, Byte[] key, Byte[] iv)
 {
     var bytesKey = ResizeBytesArray(key, aes.Key.Length);
     var bytesIv = ResizeBytesArray(iv, aes.IV.Length);
     using (var msIn = new MemoryStream(source))
     using (var cryptStreem = new CryptoStream(msIn, aes.CreateDecryptor(bytesKey, bytesIv), CryptoStreamMode.Read))
     {
         MemoryStream m = new MemoryStream();
         cryptStreem.CopyTo(m);
         return m.ToArray();
     }
 }
Example #5
0
        public static string DecryptString(this SymmetricAlgorithm algorithm, string ciphertext)
        {
            if (string.IsNullOrEmpty(ciphertext))
                throw new ArgumentNullException("ciphertext");

            var decryptor = algorithm.CreateDecryptor();

            var bytes = Convert.FromBase64String(ciphertext);
            var ms = new MemoryStream(bytes);
            var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

            using (var reader = new StreamReader(cs))
            {
                return reader.ReadToEnd();
            }
        }
        /// <summary>
        /// Dencryptors the specified algorithm.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <param name="data">The data.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        public static byte[] Dencryptor(this SymmetricAlgorithm algorithm, byte[] data, int offset, int count)
        {
            MemoryStream tmp = new MemoryStream(data);
            tmp.Position = 0;
            System.Security.Cryptography.CryptoStream cryptoStream = new CryptoStream(tmp, algorithm.CreateDecryptor(), CryptoStreamMode.Read);
            MemoryStream ms = new MemoryStream();

            byte[] buffer = new byte[1024];
            int readCount = -1;
            do
            {
                readCount = cryptoStream.Read(buffer, 0, 1024);
                ms.Write(buffer, 0, readCount);
            }
            while (readCount != 0);
            return ms.ToArray();
        }
        public static byte[] Decrypt(this SymmetricAlgorithm algorithm, byte[] input)
        {
            // Create memory stream to which from read decrypted data.
            using (var memoryStream = new MemoryStream(input))
            {
                //// Ensure that crypto operations are thread-safe.
                //lock (algorithm)
                //{
                using (var transform = algorithm.CreateDecryptor())
                {
                    var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);

                    // Read output data to crypto stream.
                    byte[] output = new byte[input.Length];
                    int outputLength = cryptoStream.Read(output, 0, output.Length);

                    Array.Resize(ref output, outputLength);

                    // Return decrypted data.
                    return output;
                }
                //}
            }
        }
        /// <summary>
        /// Decrypts input stream onto output stream for the given parameters.
        /// </summary>
        /// <param name="algorithm"><see cref="SymmetricAlgorithm"/> to use for decryption.</param>
        /// <param name="source">Source stream that contains data to decrypt.</param>
        /// <param name="destination">Destination stream used to hold decrypted data.</param>
        /// <param name="key">The secret key to use for the symmetric algorithm.</param>
        /// <param name="iv">The initialization vector to use for the symmetric algorithm.</param>
        public static void Decrypt(this SymmetricAlgorithm algorithm, Stream source, Stream destination, byte[] key, byte[] iv)
        {
            byte[] buffer = new byte[Standard.BufferSize];
            CryptoStream decodeStream = new CryptoStream(destination, algorithm.CreateDecryptor(key, iv), CryptoStreamMode.Write);
            int read;

            // Decrypts data onto output stream.
            read = source.Read(buffer, 0, Standard.BufferSize);

            while (read > 0)
            {
                decodeStream.Write(buffer, 0, read);
                read = source.Read(buffer, 0, Standard.BufferSize);
            }

            decodeStream.FlushFinalBlock();
        }
Example #9
0
File: Cipher.cs Project: avs009/gsf
        /// <summary>Decrypts input stream onto output stream for the given parameters.</summary>
        public static void Decrypt(this SymmetricAlgorithm algorithm, Stream inStream, Stream outStream, byte[] key, byte[] IV)
        {
            // This is the root decryption function. Eventually, all the symmetric algorithm based decryption
            // functions perform their actual decryption here.
            byte[] rgbKey = algorithm.GetLegalKey(key);
            byte[] rgbIV = algorithm.GetLegalIV(IV);
            byte[] buffer = new byte[BufferSize];
            CryptoStream decodeStream = new CryptoStream(outStream, algorithm.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            int read;

            // Decrypts data onto output stream.
            read = inStream.Read(buffer, 0, BufferSize);

            while (read > 0)
            {
                decodeStream.Write(buffer, 0, read);
                read = inStream.Read(buffer, 0, BufferSize);
            }

            decodeStream.FlushFinalBlock();
        }
Example #10
0
 public static ICryptoTransform CreateDecryptor(this SymmetricAlgorithm symmetricAlgorithm, CryptoKey cryptoKey, CryptoIV cryptoIV)
 {
     return symmetricAlgorithm.CreateDecryptor(cryptoKey.Value, cryptoIV.Value);
 }