我似乎无法找到使用AES 256加密在c#中加密文件的一个很好的简洁示例
有没有人有一些示例代码?
答案 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);
}
}
}
}