using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace SqlResetPwd
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while (true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Escape)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.Write("IN FileName:"); string inFileName = Console.ReadLine();
Console.Write("OUT FileName:"); string outFileName = Console.ReadLine();
Console.Write("PassWord:"); string password = Console.ReadLine();
Console.Write("Choice 0:Encrypt other:Dcrypt:"); string Choice = Console.ReadLine();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create the password key
byte[] saltValueBytes = Encoding.ASCII.GetBytes("This is my sa1t");
Rfc2898DeriveBytes passwordKey = new Rfc2898DeriveBytes(password, saltValueBytes);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create the algorithm and specify the key and IV
RijndaelManaged alg = new RijndaelManaged();
alg.Key = passwordKey.GetBytes(alg.KeySize / 8);
alg.IV = passwordKey.GetBytes(alg.BlockSize / 8);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Choice == "0")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
encrypt#region encrypt
// Create the password key
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Read the unencrypted file into fileData
FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
byte[] fileData = new byte[inFile.Length];
inFile.Read(fileData, 0, (int)inFile.Length);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create the ICryptoTransform and CryptoStream object
ICryptoTransform encryptor = alg.CreateEncryptor();
FileStream outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
CryptoStream encryptStream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Write the contents to the CryptoStream
encryptStream.Write(fileData, 0, fileData.Length);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Close the file handles
encryptStream.Close();
inFile.Close();
outFile.Close();
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
dencrypt#region dencrypt
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Read the encrypted file into fileData
ICryptoTransform decryptor = alg.CreateDecryptor();
FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
CryptoStream decryptStream = new CryptoStream(inFile, decryptor, CryptoStreamMode.Read);
byte[] fileData = new byte[inFile.Length];
decryptStream.Read(fileData, 0, (int)inFile.Length);
// Write the contents of the unencrypted file
FileStream outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
outFile.Write(fileData, 0, fileData.Length);
// Close the file handles
decryptStream.Close();
inFile.Close();
outFile.Close();
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
从中不难看出对称加密解密的步骤:
选择要采用的加解密算法的类
创建相应的KEY,IV
创建要读出或写入的文件流
利用SymmetricAlgorithm.CreateEncryptor() CreateDecryptor()方法创建ICryptoTransform对象
利用ICryptoTransform对象和创建的文件流创建CryptoStream对象
写入或读出加解密的文件流