示例#1
0
        internal static string Decrypt_File_Contents(string InputEncryptedFilePath, bool ReWriteDecryptedFile = true)
        {
            string       plaintext = null;
            CryptoStream csDecrypt = null;

            try
            {
                using (Aes AES = Aes.Create())
                {
                    AES.KeySize   = AES256KeySize;
                    AES.BlockSize = 128;
                    AES.Padding   = PaddingMode.PKCS7;

                    var key = new Rfc2898DeriveBytes(CONVERT_To_UTF8_Bytes(GET_Password()), CONVERT_To_UTF8_Bytes(SALT), 50000);
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV  = key.GetBytes(AES.BlockSize / 8);

                    ICryptoTransform decryptor = AES.CreateDecryptor(AES.Key, AES.IV);

                    using (MemoryStream msDecrypt = new MemoryStream(File.ReadAllBytes(InputEncryptedFilePath)))
                    {
                        using (csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                try
                                {
                                    plaintext = srDecrypt.ReadToEnd();
                                }
                                catch (Exception e)
                                {
                                    File_Operation.DELETE_File(InputEncryptedFilePath);
                                    File_Operation.WRITE_Default_Critical_Files();
                                }
                            }
                            csDecrypt = null;
                        }
                    }
                }
                if (ReWriteDecryptedFile)
                {
                    File_Operation.Turnicate_File(InputEncryptedFilePath);
                }
            }
            finally
            {
                if (csDecrypt != null)
                {
                    csDecrypt.Dispose();
                }
            }
            return(plaintext);
        }