.Net下文件加密和解密
用XML文件记录配置信息时,有时候不希望别人看到配置信息的内容.怎么才能实现呢.这里介绍几种常见的加密和解密算法.这些算法Framework已经封装好了.我们不用理会具体的实现,只需要会用就行.下面给出一个源程序供大家参考.
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace coder
{
/// <summary>
/// SymmCrypto 的摘要说明。
/// SymmCrypto类实现.NET框架下的加密和解密服务。
/// 原作者: Frank Fang : fangfrank@hotmail.com
///改进者:ligang : nkligang@163.com
/// </summary>
public class SymmCrypto
{
public enum SymmProvEnum : int
{
DES, RC2, Rijndael
}
{
/// <summary>
/// SymmCrypto 的摘要说明。
/// SymmCrypto类实现.NET框架下的加密和解密服务。
/// 原作者: Frank Fang : fangfrank@hotmail.com
///改进者:ligang : nkligang@163.com
/// </summary>
public class SymmCrypto
{
public enum SymmProvEnum : int
{
DES, RC2, Rijndael
}
private SymmetricAlgorithm mobjCryptoService;
/// <remarks>
/// 使用.Net SymmetricAlgorithm 类的构造器.
/// </remarks>
public SymmCrypto(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.RC2:
mobjCryptoService = new RC2CryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}
/// 使用.Net SymmetricAlgorithm 类的构造器.
/// </remarks>
public SymmCrypto(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.RC2:
mobjCryptoService = new RC2CryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}