AES 256文件加密c#

时间:2013-07-13 05:41:04

标签: c# asp.net aes

我似乎无法找到使用AES 256加密在c#中加密文件的一个很好的简洁示例

有没有人有一些示例代码?

1 个答案:

答案 0 :(得分:4)

以上是上述问题的答案

 UnicodeEncoding ue = new UnicodeEncoding();

                byte[] key = ue.GetBytes(password);
                string cryptFile = outputFile;
                using (FileStream fileCrypt = new FileStream(cryptFile, FileMode.Create))
                {
                    using (AesManaged encrypt = new AesManaged())
                    {
                        using (CryptoStream cs = new CryptoStream(fileCrypt, encrypt.CreateEncryptor(key, key), CryptoStreamMode.Write))
                        {
                            using (FileStream fileInput = new FileStream(inputFile, FileMode.Open))
                            {
                                encrypt.KeySize = 256;
                                encrypt.BlockSize = 128;
                                int data;
                                while ((data = fileInput.ReadByte()) != -1)
                                    cs.WriteByte((byte)data);
                            }
                        }
                    }
                }