2025年3月13日 星期四 甲辰(龙)年 月十二 夜 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > .net

C# 加密与解密

时间:12-12来源:作者:点击数:20

c#中,在System.Security.Cryptography命名空间中有一些常用的关于加密算法的类。

1. DES类

DES是一个抽象类,DESCryptoServiceProvider 类是它的一个实现类。

DES的主要属性有两个:密钥 (Key) 和初始化向量 (IV) 。主要方法:加密方法CreateEncryptor()和解密方法CreateDecryptor()。

MSDN上的一个加密文件的例子

  • private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
  • {
  • //Create the file streams to handle the input and output files.
  • FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  • FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  • fout.SetLength(0);
  • //Create variables to help with read and write.
  • byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
  • long rdlen = 0; //This is the total number of bytes written.
  • long totlen = fin.Length; //This is the total length of the input file.
  • int len; //This is the number of bytes to be written at a time.
  • DES des = new DESCryptoServiceProvider();
  • CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
  • Console.WriteLine("Encrypting...");
  • //Read from the input file, then encrypt and write to the output file.
  • while(rdlen < totlen)
  • {
  • len = fin.Read(bin, 0, 100);
  • encStream.Write(bin, 0, len);
  • rdlen = rdlen + len;
  • Console.WriteLine("{0} bytes processed", rdlen);
  • }
  • encStream.Close();
  • fout.Close();
  • fin.Close();
  • }

可以用同样的方式处理解密;使用 CreateDecryptor 而不是 CreateEncryptor必须使用加密该文件所用的同一密钥 (Key) 和初始化向量 (IV) 对文件进行解密。如果Key或IV为null,会自动调用GenerateKeyGetHashCode创建随机密钥和向量。

我自己写的一个例子:

  • // 加密
  • public static byte[] Encrypt(String str, byte[] key, byte[] iv)
  • {
  • byte[] data = Encoding.ASCII.GetBytes(str.ToCharArray());
  • byte[] result;
  • DES des = new DESCryptoServiceProvider();
  • des.Key = key;
  • des.IV = iv;
  • result = des.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
  • return result;
  • }
  • // 解密
  • public static String Decrypt(byte[] data, byte[] key, byte[] iv)
  • {
  • byte[] result;
  • DES des = new DESCryptoServiceProvider();
  • result = des.CreateDecryptor(key, iv).TransformFinalBlock(data, 0, data.Length);
  • String s = Encoding.ASCII.GetString(result);
  • return s;
  • }

2. MD5

参见http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.md5.aspx

3. 对XML元素的加密和解密

可以使用EncryptedXml类的EncryptData()和DecryptData()等方法。

4. 对配置文件的加密和解密

可以使用SectionInformation类的ProtectSection()和UnprotectSection()方法。

  • String configFileName = "文件名";
  • System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(configFileName);
  • System.Configuration.ConnectionStringsSection section = config.GetSection("connectionStrings") as System.Configuration.ConnectionStringsSection;
  • System.Configuration.SectionInformation info = section.SectionInformation;
  • //加密
  • if (!info.IsProtected)
  • {
  • info.ProtectSection("DataProtectionConfigurationProvider");
  • //加密提供程序的名字可以由ProtectedConfiguration类得到,有两个。
  • }
  • //解密
  • if (info.IsProtected)
  • {
  • info.UnprotectSection();
  • }

另参见:http://msdn.microsoft.com/zh-cn/library/system.configuration.sectioninformation.protectsection(VS.80).aspx

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐