public static string DecryptAES(this string cipherText, string key)
 {
     cipherText = cipherText.Replace(" ", "+");
     byte[] cipherBytes = Convert.FromBase64String(cipherText);
     using (Aes encryptor = new AesManaged())
     {
         Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
         encryptor.Key     = pdb.GetBytes(32);
         encryptor.IV      = pdb.GetBytes(16);
         encryptor.Padding = PaddingMode.PKCS7;
         encryptor.Mode    = CipherMode.CBC;
         using (MemoryStream ms = new MemoryStream())
         {
             using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
             {
                 cs.Write(cipherBytes, 0, cipherBytes.Length);
                 cs.Close();
             }
             cipherText = Encoding.Unicode.GetString(ms.ToArray());
         }
     }
     return(cipherText);
 }
Esempio n. 2
0
 private static byte[] AesDec(byte[] sharedKey, byte[] cipherText)
 {
     byte[] array;
     using (Aes aesManaged = new AesManaged())
     {
         aesManaged.Key     = sharedKey;
         aesManaged.Mode    = CipherMode.ECB;
         aesManaged.Padding = PaddingMode.None;
         using (MemoryStream memoryStream = new MemoryStream())
         {
             using (ICryptoTransform cryptoTransform = aesManaged.CreateDecryptor(aesManaged.Key, aesManaged.IV))
             {
                 using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
                 {
                     cryptoStream.Write(cipherText, 0, (int)cipherText.Length);
                     cryptoStream.FlushFinalBlock();
                     array = memoryStream.ToArray();
                 }
             }
         }
     }
     return(array);
 }
Esempio n. 3
0
        private static byte[] AesDec(byte[] sharedKey, byte[] cipherText)
        {
            using (Aes aes = new AesManaged())
            {
                aes.Key     = sharedKey;
                aes.Mode    = CipherMode.ECB;
                aes.Padding = PaddingMode.None;

                using (MemoryStream ms = new MemoryStream())
                {
                    using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
                    {
                        using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
                        {
                            cs.Write(cipherText, 0, cipherText.Length);
                            cs.FlushFinalBlock();

                            return(ms.ToArray());
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public static string Decrypt(byte[] encrypted, string key)
        {
            using (AesManaged aes = new AesManaged())
            {
                byte[] KeyBytes = Encoding.ASCII.GetBytes(AdjustKeyLength(key));

                aes.Key     = KeyBytes;
                aes.IV      = KeyBytes;
                aes.Padding = PaddingMode.Zeros;
                ICryptoTransform decryptor = aes.CreateDecryptor(KeyBytes, KeyBytes);

                using (MemoryStream ms = new MemoryStream(encrypted))
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                        {
                            return(sr.ReadLine());
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        private Stream Decrypt(Stream data, byte[] encryptionKey, int hmacLength)
        {
            var initializationVector = new byte[BLOCK_SIZE];

            data.Seek(HEADER_SIZE + 2 * SALT_SIZE, SeekOrigin.Begin);
            data.Read(initializationVector, 0, BLOCK_SIZE);

            var decryptedData = new MemoryStream();
            var encryptedData = new SubStream(data, data.Position, data.Length - data.Position - hmacLength);

            logger.Debug("Decrypting data...");

            using (var algorithm = new AesManaged {
                KeySize = KEY_SIZE * 8, BlockSize = BLOCK_SIZE * 8, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7
            })
                using (var decryptor = algorithm.CreateDecryptor(encryptionKey, initializationVector))
                    using (var cryptoStream = new CryptoStream(encryptedData, decryptor, CryptoStreamMode.Read))
                    {
                        cryptoStream.CopyTo(decryptedData);
                    }

            return(decryptedData);
        }
Esempio n. 6
0
        public static string Decrypt(byte[] cipherText, string key)
        {
            string plaintext = null;

            byte[] keyBytes = GetKeyBytes(key);

            using (AesManaged aes = new AesManaged())
            {
                ICryptoTransform decryptor = aes.CreateDecryptor(keyBytes, IV);
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader reader = new StreamReader(cs))
                        {
                            plaintext = reader.ReadToEnd();
                        }
                    }
                }
            }

            return(plaintext);
        }
        public static string Decrypt(string encryptedX2String)
        {
            var hashKey   = GetHashKey();
            var decryptor = new AesManaged
            {
                Key = hashKey,
                IV  = hashKey
            };

            var encryptedData = Enumerable.Range(0, encryptedX2String.Length).
                                Where(x => x % 2 == 0).
                                Select(x => Convert.ToByte(encryptedX2String.Substring(x, 2), 16)).ToArray();

            using var decryptionStream = new MemoryStream();
            using var decrypt          = new CryptoStream(decryptionStream, decryptor.CreateDecryptor(), CryptoStreamMode.Write);
            decrypt.Write(encryptedData, 0, encryptedData.Length);
            decrypt.Flush();
            decrypt.Close();

            var decryptedData = decryptionStream.ToArray();

            return(Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length));
        }
Esempio n. 8
0
        public static string Decrypt <T>(string text, string password, string salt)
            where T : SymmetricAlgorithm, new()
        {
            DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));

            SymmetricAlgorithm algorithm = new AesManaged();

            byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
            byte[] rgbIV  = rgb.GetBytes(algorithm.BlockSize >> 3);

            ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);

            using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
            {
                using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
                    {
                        return(reader.ReadToEnd());
                    }
                }
            }
        }
Esempio n. 9
0
        public static String NewCryto(String data, String key)
        {
            string iv      = "dZMjkk8gFDzKHlsx";
            var    ivBytes = Encoding.UTF8.GetBytes(iv);

            var keyBytes = new Byte[16];

            Array.Copy(Encoding.UTF8.GetBytes(key), keyBytes, key.Length);
            var aes = new AesManaged {
                Mode = CipherMode.CBC, Key = keyBytes, IV = ivBytes
            };

            Byte[]           uncryptedBytes = Convert.FromBase64String(data);
            ICryptoTransform transform      = aes.CreateDecryptor();

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
                    cryptoStream.Write(uncryptedBytes, 0, uncryptedBytes.Length);
                var tmp = Encoding.UTF8.GetString(memoryStream.ToArray());
                return(tmp.Substring(0, 3 + tmp.IndexOf("}]}", 0)));
            }
        }
Esempio n. 10
0
        public byte[] Decrypt(EncryptedMessage em)
        {
            using (var aes = new AesManaged())
            {
                aes.Key = em.Key;
                aes.IV  = em.IV;

                using (var decryptor = aes.CreateDecryptor())
                {
                    using (var msInput = new MemoryStream(em.CipherBytes))
                    {
                        using (var cryptoStream = new CryptoStream(msInput, decryptor, CryptoStreamMode.Read))
                        {
                            using (var msOutput = new MemoryStream())
                            {
                                cryptoStream.CopyTo(msOutput);
                                return(msOutput.ToArray());
                            }
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        public static Stream DecryptStream(Stream inStream, string password)
        {
            using (var am = new AesManaged())
            {
                am.BlockSize = 128;
                am.KeySize   = 128;
                am.Mode      = CipherMode.CBC;
                am.Padding   = PaddingMode.PKCS7;

                byte[] salt = new byte[16];
                inStream.Read(salt, 0, 16);  // !!!

                byte[] IV = new byte[16];
                inStream.Read(IV, 0, 16);

                var deriveBytes = new Rfc2898DeriveBytes(password, salt);
                am.Key = deriveBytes.GetBytes(16);
                am.IV  = IV;

                var crypter = am.CreateDecryptor(am.Key, am.IV);
                return(new CryptoStream(inStream, crypter, CryptoStreamMode.Read));
            }
        }
Esempio n. 12
0
 public static void AesDecrypt(this byte[] data, byte[] key, byte[] iv)
 {
     if (data == null || key == null || iv == null)
     {
         throw new ArgumentNullException();
     }
     if (data.Length % 16 != 0 || key.Length != 32 || iv.Length != 16)
     {
         throw new ArgumentException();
     }
     byte[] buffer;
     using (AesManaged aes = new AesManaged())
     {
         aes.Padding = PaddingMode.None;
         using (ICryptoTransform decryptor = aes.CreateDecryptor(key, iv))
         {
             buffer = decryptor.TransformFinalBlock(data, 0, data.Length);
         }
     }
     Buffer.BlockCopy(buffer, 0, data, 0, data.Length);
     Array.Clear(iv, 0, iv.Length);
     Array.Clear(buffer, 0, buffer.Length);
 }
Esempio n. 13
0
        public string Decrypt(string base64Input)
        {
            byte[]             encryptBytes = Convert.FromBase64String(base64Input);
            byte[]             saltBytes    = Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings["Encryption_Salt"]);
            AesManaged         aes          = new AesManaged();
            Rfc2898DeriveBytes rfc          = new Rfc2898DeriveBytes(ConfigurationManager.AppSettings["Encryption_Password"], saltBytes);

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize   = aes.LegalKeySizes[0].MaxSize;
            aes.Key       = rfc.GetBytes(aes.KeySize / 8);
            aes.IV        = rfc.GetBytes(aes.BlockSize / 8);
            ICryptoTransform decryptTrans  = aes.CreateDecryptor();
            MemoryStream     decryptStream = new MemoryStream();
            CryptoStream     decryptor     = new CryptoStream(decryptStream, decryptTrans, CryptoStreamMode.Write);

            decryptor.Write(encryptBytes, 0, encryptBytes.Length);
            decryptor.Flush();
            decryptor.Close();
            byte[] decryptBytes    = decryptStream.ToArray();
            string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);

            return(decryptedString);
        }
Esempio n. 14
0
        public static string Decrypt(string input)
        {
            string s        = "saltValue";
            string password = "******";

            byte[]             array              = Convert.FromBase64String(input.Replace("%2B", "+"));
            byte[]             bytes              = Encoding.UTF8.GetBytes(s);
            AesManaged         aesManaged         = new AesManaged();
            Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, bytes);

            aesManaged.BlockSize = aesManaged.LegalBlockSizes[0].MaxSize;
            aesManaged.KeySize   = aesManaged.LegalKeySizes[0].MaxSize;
            aesManaged.Key       = rfc2898DeriveBytes.GetBytes(aesManaged.KeySize / 8);
            aesManaged.IV        = rfc2898DeriveBytes.GetBytes(aesManaged.BlockSize / 8);
            ICryptoTransform transform    = aesManaged.CreateDecryptor();
            MemoryStream     memoryStream = new MemoryStream();
            CryptoStream     cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);

            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.Close();
            byte[] array2 = memoryStream.ToArray();
            return(Encoding.UTF8.GetString(array2, 0, array2.Length));
        }
Esempio n. 15
0
        internal static string Decrypt(this byte[] data, SettingsModel settingsModel)
        {
            var saltBytes = Encoding.UTF8.GetBytes(settingsModel.InitVector);

            var key = new Rfc2898DeriveBytes(PassPhrase, saltBytes, 32768);

            using (Aes aes = new AesManaged())
            {
                aes.KeySize = 256;
                aes.Key     = key.GetBytes(aes.KeySize / 8);
                aes.IV      = key.GetBytes(aes.BlockSize / 8);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.Close();
                    }
                    return(Encoding.UTF8.GetString(ms.ToArray()));
                }
            }
        }
Esempio n. 16
0
        private static byte[] Decrypt(this byte[] param)
        {
            if (param.Length == default(int))
            {
                return(new byte[] { });
            }

            using (var ms = new MemoryStream())
            {
                using (var aes = new AesManaged())
                {
                    aes.Key = Extensions.PDB.GetBytes(aes.KeySize / 8);
                    aes.IV  = Extensions.PDB.GetBytes(aes.BlockSize / 8);

                    using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(param, default(int), param.Length);
                    }

                    return(ms.ToArray());
                }
            }
        }
Esempio n. 17
0
        public static int AesCbcCfb128Decrypt(byte[] @out, byte[] @in, int size, byte[] key, byte[] iv)
        {
            var cipher = new AesManaged
            {
                Mode      = CipherMode.CBC,
                KeySize   = 128,
                Key       = key,
                IV        = iv,
                Padding   = PaddingMode.None,
                BlockSize = 128,
            };
            var tmp = new byte[size];

            using (var ct_stream = new MemoryStream(@in))
                using (var pt_stream = new MemoryStream(tmp))
                    using (var dec = cipher.CreateDecryptor(key, iv))
                        using (var s = new CryptoStream(ct_stream, dec, CryptoStreamMode.Read))
                        {
                            s.CopyTo(pt_stream);
                        }
            Buffer.BlockCopy(tmp, 0, @out, 0, tmp.Length);
            return(0);
        }
Esempio n. 18
0
        public string Decrypt(string data, string password)
        {
            string result = null;

            byte[] dataToBeDecrypted = Convert.FromBase64String(data);

            using (AesManaged aesAlg = new AesManaged())
            {
                var key = GenerateKey(password);

                aesAlg.Key = key;
                aesAlg.IV  = dataToBeDecrypted.Skip(0).Take(16).ToArray();

                var dataToDecrypt = dataToBeDecrypted.Skip(16).ToArray();

                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                var decryptedBytes         = decryptor.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);

                result = Encoding.UTF8.GetString(decryptedBytes);
            }

            return(result);
        }
Esempio n. 19
0
        /// <summary>
        /// Method to decrypt the encrypted Configuration parameters
        /// </summary>
        /// <param name="Encryptedstring">Encrypted SPO User name or SPO Password</param>
        /// <param name="Key">Encrypted Key</param>
        /// <param name="Iv">Encrypted Iv</param>
        /// <returns>Decrypted string</returns>
        public static string Decrypt(string Encryptedstring, string Key, string Iv)
        {
            //Algorithm used for decryption
            AesManaged tdes = new AesManaged();

            byte[] bytestoDecrypt = null;
            try
            {
                bytestoDecrypt = Convert.FromBase64String(Encryptedstring);
                tdes.KeySize   = KeyBitSize;
                tdes.Mode      = CipherMode.ECB;
                tdes.Padding   = PaddingMode.PKCS7;

                ICryptoTransform cryptoTransform = tdes.CreateDecryptor(Convert.FromBase64String(Key), Convert.FromBase64String(Iv));
                byte[]           resultArray     = cryptoTransform.TransformFinalBlock(bytestoDecrypt, 0, bytestoDecrypt.Length);

                return(Encoding.ASCII.GetString(resultArray));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 20
0
        private static byte[] Decrypt(byte[] iv, byte[] dat)
        {
            using (var ms = new MemoryStream(dat.Length))
            {
                ms.Write(dat, 0, dat.Length);
                ms.Position = 0;

                using (var aes = new AesManaged
                {
                    Key = Key,
                    IV = iv,
                    Mode = CipherMode.CBC,
                    Padding = PaddingMode.None
                })
                    using (var decryptor = aes.CreateDecryptor())
                        using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                        {
                            var decrypted = new byte[dat.Length];
                            cs.Read(decrypted, 0, decrypted.Length);
                            return(decrypted);
                        }
            }
        }
Esempio n. 21
0
        /// <summary>
        /// ファイルを復号化する
        /// </summary>
        /// <param name="ifs"></param>
        /// <param name="ofs"></param>
        /// <param name="password"></param>
        public static void Decrypt(Stream ifs, Stream ofs, string password)
        {
            var keyBytes = FileEncryptor.GetKeyBytes(password);

            using var aes = new AesManaged()
                  {
                      BlockSize = 128,
                      KeySize   = 128,
                      Mode      = CipherMode.CBC,
                      Padding   = PaddingMode.PKCS7,
                      Key       = keyBytes,
                  };

            var ivBytes = new byte[aes.IV.Length];

            ifs.Read(ivBytes, 0, ivBytes.Length);
            aes.IV = ivBytes;

            /* Create AES Decryptor */
            using var encrypt = aes.CreateDecryptor();
            using var cs      = new CryptoStream(ofs, encrypt, CryptoStreamMode.Write, true);
            ifs.CopyTo(cs, 32 * 1024);
        }
Esempio n. 22
0
        private void DecodeSubtitle(int subtitleId, string ivBase64, string dataBase64, ref CrunchySubtitleInfo si)
        {
            byte[]     key  = GenerateKey(subtitleId).Concat(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).ToArray();
            byte[]     iv   = Convert.FromBase64String(ivBase64);
            byte[]     data = Convert.FromBase64String(dataBase64);
            AesManaged mng  = new AesManaged();

            mng.Mode    = CipherMode.CBC;
            mng.Padding = PaddingMode.None;
            ICryptoTransform tr = mng.CreateDecryptor(key, iv);

            byte[]       kk     = tr.TransformFinalBlock(data, 0, data.Length);
            MemoryStream ms     = new MemoryStream();
            ZlibStream   stream = new ZlibStream(new MemoryStream(kk), Ionic.Zlib.CompressionMode.Decompress);

            stream.CopyTo(ms);
            ms.Position = 0;
            XmlSerializer  serializer = new XmlSerializer(typeof(SubtitleScript));
            SubtitleScript script     = (SubtitleScript)serializer.Deserialize(ms);

            si.Title = script.Title;
            si.Ass   = script.ToAss();
        }
        public static byte[] Decrypt(byte[] strData)
        {
            PasswordDeriveBytes passbytes =
                new PasswordDeriveBytes(strPermutation,
                                        new byte[] { bytePermutation1,
                                                     bytePermutation2,
                                                     bytePermutation3,
                                                     bytePermutation4 });

            MemoryStream memstream = new MemoryStream();
            Aes          aes       = new AesManaged();

            aes.Key = passbytes.GetBytes(aes.KeySize / 8);
            aes.IV  = passbytes.GetBytes(aes.BlockSize / 8);

            CryptoStream cryptostream = new CryptoStream(memstream,
                                                         aes.CreateDecryptor(), CryptoStreamMode.Write);

            cryptostream.Write(strData, 0, strData.Length);

            cryptostream.Close();
            return(memstream.ToArray());
        }
        public static byte[] AESDecryptBytes(byte[] cryptBytes, byte[] passBytes, byte[] saltBytes)
        {
            byte[] clearBytes = null;
            var    key        = new Rfc2898DeriveBytes(passBytes, saltBytes, 32768);

            using (Aes aes = new AesManaged())
            {
                aes.KeySize = 256;
                aes.Key     = key.GetBytes(aes.KeySize / 8);
                aes.IV      = key.GetBytes(aes.BlockSize / 8);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cryptBytes, 0, cryptBytes.Length);
                        cs.Close();
                    }
                    clearBytes = ms.ToArray();
                }
            }
            return(clearBytes);
        }
Esempio n. 25
0
        private static byte[] DecryptByteArray(byte[] key, byte[] secret)
        {
            byte[] iv = new byte[16];
            byte[] encryptedContent = new byte[secret.Length - 16];

            Buffer.BlockCopy(secret, 0, iv, 0, iv.Length);
            Buffer.BlockCopy(secret, iv.Length, encryptedContent, 0, encryptedContent.Length);

            using (MemoryStream ms = new MemoryStream())
                using (AesManaged cryptor = new AesManaged())
                {
                    cryptor.Mode      = CipherMode.CBC;
                    cryptor.Padding   = PaddingMode.None;
                    cryptor.KeySize   = 256;
                    cryptor.BlockSize = 128;

                    using (CryptoStream cs = new CryptoStream(ms, cryptor.CreateDecryptor(key, iv), CryptoStreamMode.Write))
                    {
                        cs.Write(encryptedContent, 0, encryptedContent.Length);
                    }
                    return(ms.ToArray());
                }
        }
Esempio n. 26
0
        public static byte[] DecryptBytesFromBytes_Aes(byte[] cryptBytes, byte[] key, byte[] iv)
        {
            byte[] clearBytes = null;

            using (Aes aesAlg = new AesManaged())
            {
                aesAlg.Padding = PaddingMode.PKCS7;

                aesAlg.Key = key;
                aesAlg.IV  = iv;

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aesAlg.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cryptBytes, 0, cryptBytes.Length);
                        cs.Close();
                    }
                    clearBytes = ms.ToArray();
                }
            }
            return(clearBytes);
        }
Esempio n. 27
0
        // Расшифровка AES шифрования
        public static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
        {
            string plaintext = null;

            // Создаём объект
            using (AesManaged aes = new AesManaged())
            {
                // Создаём дешифровщик
                ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
                // Создаём поток куда пишем
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    // Создаём CryptoStream для дешифровки
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        // Считываем результат дешифровки
                        using (StreamReader reader = new StreamReader(cs))
                            plaintext = reader.ReadToEnd();
                    }
                }
            }
            return(plaintext);
        }
        public byte[] Düzelt(byte[] Girdi, byte[] Parola)
        {
            try
            {
                Aes aes = new AesManaged();
                aes.IV = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

                PasswordDeriveBytes pdb = new PasswordDeriveBytes(Parola, aes.IV);
                MemoryStream        ms  = new MemoryStream();

                aes.Mode    = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
                aes.Key     = pdb.GetBytes(aes.KeySize / 8);
                aes.IV      = pdb.GetBytes(aes.BlockSize / 8);

                CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(Girdi, 0, Girdi.Length);
                cs.Close();
                return(ms.ToArray());
            }
            catch (Exception) { }
            return(null);
        }
Esempio n. 29
0
        private static string DecryptInternal(byte[] cipherText, byte[] Key, byte[] IV)
        {
            string plaintext = null;

            // Create AesManaged
            using (AesManaged aes = new AesManaged())
            {
                // Create a decryptor
                ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
                // Create the streams used for decryption.
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    // Create crypto stream
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        // Read crypto stream
                        using (StreamReader reader = new StreamReader(cs))
                            plaintext = reader.ReadToEnd();
                    }
                }
            }
            return(plaintext);
        }
 /// <summary>
 /// Decrypt a string with a given Key and Salt.
 /// </summary>
 /// <param name="input">The Current String to Decrypt</param>
 /// <param name="Key">The Encryption Key</param>
 /// <param name="Salt">Needed for decryption</param>
 /// <returns>
 /// Decrypted String
 /// </returns>
 public static string Decrypt(this string input, string Key, string Salt)
 {
     // Locate a memory to write the bytes in.
     using (MemoryStream MS = new MemoryStream())
     {
         // Advanced Encryption Standard Encryption Algorithm, it’s so powerful.
         AesManaged aes = new AesManaged();
         // Generate Encrypt Key, with a given key and pseudo byte array
         using (PasswordDeriveBytes KeyGenerator = new PasswordDeriveBytes(Key, Encoding.UTF8.GetBytes(Salt)))
         {
             aes.Key = KeyGenerator.GetBytes(aes.KeySize / 8);
             aes.IV  = KeyGenerator.GetBytes(aes.BlockSize / 8);
         }
         // Define a stream to link the data with Cryptographic transformation
         using (CryptoStream cs = new CryptoStream(MS, aes.CreateDecryptor(), CryptoStreamMode.Write))
         {
             // Convert from the Base64 text byte[] represention to it's byte[] again.
             byte[] InputeByte = input.ConvertBase64ToByteArray();
             cs.Write(InputeByte, 0, InputeByte.Length);
         }
         return(MS.ToArray().ConvertByteArrayToUTF8String());
     }
 }