Example #1
0
 public static byte[] Decrypt(this SymmetricAlgorithm @this, byte[] buffer)
 {
     using (var decryptor = @this.CreateDecryptor())
         using (var ms = new MemoryStream(buffer))
             using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                 return(cs.ReadToEnd());
 }
Example #2
0
 public byte[] Decrypt(byte[] Data)
 {
     if (!HasPrivate)
     {
         throw new Exception("Can't decrypt without private key!");
     }
     using (RSACryptoServiceProvider R = new RSACryptoServiceProvider())
     {
         R.PersistKeyInCsp = false;
         R.ImportParameters(Params);
         using (RijndaelManaged RM = new RijndaelManaged())
         {
             using (MemoryStream MS = new MemoryStream(Data, false))
             {
                 using (BinaryReader BR = new BinaryReader(MS))
                 {
                     RM.IV  = R.Decrypt(BR.ReadBytes(BR.ReadInt32()), true);
                     RM.Key = R.Decrypt(BR.ReadBytes(BR.ReadInt32()), true);
                     using (RijndaelManagedTransform Dec = (RijndaelManagedTransform)RM.CreateDecryptor())
                     {
                         using (CryptoStream CS = new CryptoStream(MS, Dec, CryptoStreamMode.Read))
                         {
                             return(CS.ReadToEnd());
                         }
                     }
                 }
             }
         }
     }
 }
Example #3
0
        public static byte[] DecryptData(SymmetricAlgorithms symmetricAlgorithm, byte[] inputBytes, byte[] key)
        {
            byte[] decrypted;

            using (SymmetricAlgorithm algorithm = GetSymmetricAlgorithm(symmetricAlgorithm))
            {
                byte[] salt          = new byte[PBKDF2_SaltSizeBytes];
                byte[] iv            = new byte[algorithm.IV.Length];
                byte[] encryptedData = new byte[inputBytes.Length - salt.Length - iv.Length];

                int maxKeySize = GetLegalKeySizes(algorithm).Max();
                Buffer.BlockCopy(inputBytes, 0, salt, 0, salt.Length);
                Buffer.BlockCopy(inputBytes, salt.Length, iv, 0, iv.Length);
                Buffer.BlockCopy(inputBytes, salt.Length + iv.Length, encryptedData, 0, encryptedData.Length);

                algorithm.IV = iv;
                using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(key, salt, PBKDF2_Iterations))
                {
                    algorithm.Key = pbkdf2.GetBytes(maxKeySize);
                }

                using (ICryptoTransform cryptoTransform = algorithm.CreateDecryptor())
                {
                    using (MemoryStream encryptedStream = new MemoryStream(encryptedData))
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Read))
                        {
                            decrypted = cryptoStream.ReadToEnd();
                        }
                    }
                }
            }

            return(decrypted);
        }
Example #4
0
        /// <summary>
        /// Decrypts the specified private key.
        /// </summary>
        /// <param name="armoredData">The armored data.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">armoredData</exception>
        /// <exception cref="System.ArgumentException">armoredData cannot be empty</exception>
        public byte[] Decrypt(byte[] armoredData)
        {
            if (armoredData == null)
            {
                throw new ArgumentNullException("armoredData");
            }
            if (armoredData.Length <= 0)
            {
                throw new ArgumentException("armoredData cannot be empty");
            }

            var key = Key;
            var iv  = IV;

            using (var crypto = new RijndaelManaged()
            {
                BlockSize = BlockSize, KeySize = KeySize
            })
            {
                crypto.Key = key;
                crypto.IV  = iv;

                using (var decryptor = crypto.CreateDecryptor(crypto.Key, crypto.IV))
                {
                    using (var memoryStream = new MemoryStream(armoredData))
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                        {
                            var data = cryptoStream.ReadToEnd();
                            return(data);
                        }
                    }
                }
            }
        }
Example #5
0
 public static byte[] Decrypt(this SymmetricAlgorithm @this, Stream stream)
 {
     using (var decryptor = @this.CreateDecryptor())
         using (var cs = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
             return(cs.ReadToEnd());
 }