示例#1
0
        public static byte[] Decrypt(byte[] key, byte[] initializationVector, byte[] cipher)
        {
            var decryptor = _cryptoProvider.CreateDecryptor(key, initializationVector);

            using (var stream = new MemoryStream(cipher))
                using (var cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
                    return(cryptoStream.ReadAll());
        }
示例#2
0
 public static byte[] EncryptAes256(byte[] plaintext, byte[] key, PaddingMode padding = PaddingMode.None)
 {
     using (var aes = CreateAes256Cbc(key, padding))
         using (var encryptor = aes.CreateEncryptor())
             using (var stream = new MemoryStream(plaintext, false))
                 using (var cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Read))
                     return(cryptoStream.ReadAll(256));
 }
示例#3
0
        /// <summary>
        /// Decrypts using AES/CBC/PKCS7 with an input byte array and key, using the random IV prepended using AES/ECB/None
        /// </summary>
        static byte[] SymmetricDecrypt(byte[] input, byte[] key, out byte[] iv)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            DebugLog.Assert(key.Length == 32, "CryptoHelper", "SymmetricDecrypt used with non 32 byte key!");

            using (var aes = Aes.Create())
            {
                aes.BlockSize = 128;
                aes.KeySize   = 256;

                // first 16 bytes of input is the ECB encrypted IV
                byte[] cryptedIv = new byte[16];
                iv = new byte[cryptedIv.Length];
                Array.Copy(input, 0, cryptedIv, 0, cryptedIv.Length);

                // the rest is ciphertext
                byte[] cipherText = new byte[input.Length - cryptedIv.Length];
                Array.Copy(input, cryptedIv.Length, cipherText, 0, cipherText.Length);

                // decrypt the IV using ECB
                aes.Mode    = CipherMode.ECB;
                aes.Padding = PaddingMode.None;

                using (var aesTransform = aes.CreateDecryptor(key, null))
                {
                    iv = aesTransform.TransformFinalBlock(cryptedIv, 0, cryptedIv.Length);
                }

                // decrypt the remaining ciphertext in cbc with the decrypted IV
                aes.Mode    = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                using (var aesTransform = aes.CreateDecryptor(key, iv))
                    using (var ms = new MemoryStream(cipherText))
                        using (var cs = new CryptoStream(ms, aesTransform, CryptoStreamMode.Read))
                        {
                            // plaintext is never longer than ciphertext
                            byte[] plaintext = new byte[cipherText.Length];

                            int len = cs.ReadAll(plaintext);

                            byte[] output = new byte[len];
                            Array.Copy(plaintext, 0, output, 0, len);

                            return(output);
                        }
            }
        }
示例#4
0
    //Decryption
    public static byte[] Decrypt(byte[] data, byte[] keySeed, byte[] ivSeed)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }
        if (data.Length == 0)
        {
            return(new byte[0]);
        }

        if (keySeed == null)
        {
            throw new ArgumentNullException("keySeed");
        }
        if (keySeed.Length == 0)
        {
            throw new ArgumentException("keySeed length is zero!", "keySeed");
        }

        if (ivSeed == null)
        {
            throw new ArgumentNullException("IvSeed");
        }
        if (ivSeed.Length == 0)
        {
            throw new ArgumentException("IvSeed length is zero!", "IV");
        }

        byte[] Key = DeriveKeyFromSeed(keySeed);
        byte[] IV  = DeriveIVFromSeed(ivSeed);

        byte[] decrypted;
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV  = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            using (MemoryStream msDecrypt = new MemoryStream(data))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    decrypted = csDecrypt.ReadAll();
                }
            }
        }

        return(decrypted);
    }
示例#5
0
 public static byte[] Decrypt(Stream cipher, byte[] key, byte[] iv)
 {
     if (cipher == null || cipher.Length <= 0)
     {
         throw new ArgumentNullException(nameof(cipher));
     }
     if (key == null || key.Length <= 0)
     {
         throw new ArgumentNullException(nameof(key));
     }
     if (iv == null || iv.Length <= 0)
     {
         throw new ArgumentNullException(nameof(iv));
     }
     using (CryptoStream csDecrypt = DecryptStream(cipher, key, iv))
     {
         return(csDecrypt.ReadAll());
     }
 }
示例#6
0
        /// <summary>
        /// Decrypts an input byte array using AES/CBC/PKCS7 with a given key and IV
        /// </summary>
        public static byte[] AESDecrypt(byte[] input, byte[] key, byte[] iv)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (iv == null)
            {
                throw new ArgumentNullException(nameof(iv));
            }

            using (var aes = Aes.Create())
            {
                aes.BlockSize = 128;
                aes.KeySize   = 128;

                aes.Mode    = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                byte[] plainText = new byte[input.Length];
                int    outLen    = 0;

                using (var aesTransform = aes.CreateDecryptor(key, iv))
                    using (var ms = new MemoryStream(input))
                        using (var cs = new CryptoStream(ms, aesTransform, CryptoStreamMode.Read))
                        {
                            outLen = cs.ReadAll(plainText);
                        }

                byte[] output = new byte[outLen];
                Array.Copy(plainText, 0, output, 0, output.Length);

                return(output);
            }
        }