/**////////////////////////////////////////////////////////////////Author: stardicky ////E-mail: stardicky@hotmail.com ////QQNumber: 9531511 ////CompanyName: Ezone International ////Class: HBS-0308 ////title: 利用DotNET密码系统保证数据安全 ///**////////////////////////////////////////////////////////////////注:利用DotNET密码系统之一的DES对称加密算法保证数据安全 ///**//////////////////////////////////////////////////////////////using System;using System.IO;using System.Text;using System.Security.Cryptography;namespace EzoneInternationalSecurityCryptography...{ class EzoneSecurityCryptographyDemo ...{ [STAThread] public static void Main(string[] args) ...{ //加密数据(从内存到文件) EzoneEncryptorDemo(); //解密数据(从文件到内存) EzoneDecryptorDemo(); } /**//// <summary> /// 加密 /// </summary> public static void EzoneEncryptorDemo() ...{ //创建一个文件对象,文件的模式是创建新文件,文件的访问权限是可写! FileStream fs=new FileStream("EzoneDemo.txt",FileMode.Create,FileAccess.Write); Console.WriteLine("请输入你想要进行加密的字符串:"); //输入你想要进行加密的字符串 string YourInput=Console.ReadLine(); //将字符串转换成字节 byte[] YourInputStorage=System.Text.Encoding.UTF8.GetBytes(YourInput); //创建一个DES算法的加密类 DESCryptoServiceProvider MyServiceProvider=new DESCryptoServiceProvider(); //从DES算法的加密类对象的CreateEncryptor方法,创建一个加密转换接口对象 //第一个参数的含义是:对称算法的机密密钥(长度为64位,也就是8个字节) // 可以人工输入,也可以随机生成方法是:MyServiceProvider.GenerateKey(); //第二个参数的含义是:对称算法的初始化向量(长度为64位,也就是8个字节) // 可以人工输入,也可以随机生成方法是:MyServiceProvider.GenerateIV(); ICryptoTransform MyTransform=MyServiceProvider.CreateEncryptor(new byte[]...{100,110,120,130,100,110,120,130},new byte[]...{100,110,120,130,100,110,120,130}); //CryptoStream对象的作用是将数据流连接到加密转换的流 CryptoStream MyCryptoStream=new CryptoStream(fs,MyTransform,CryptoStreamMode.Write); //将字节数组中的数据写入到加密流中 MyCryptoStream.Write(YourInputStorage,0,YourInputStorage.Length); //关闭加密流对象 MyCryptoStream.Close(); } /**//// <summary> /// 解密 /// </summary> public static void EzoneDecryptorDemo() ...{ FileStream fs=new FileStream("EzoneDemo.txt",FileMode.Open,FileAccess.Read); DESCryptoServiceProvider MyServiceProvider=new DESCryptoServiceProvider(); //从DES算法的加密类对象的CreateEncryptor方法,创建一个解密转换接口对象 //[对称算法的机密密钥]必须是加密时候的[对称算法的机密密钥] //[对称算法的初始化向量]必须是加密时候的[对称算法的初始化向量] //如果不一样,则会抛出一个异常。 ICryptoTransform MyTransform=MyServiceProvider.CreateDecryptor(new byte[]...{100,110,120,130,100,110,120,130},new byte[]...{100,110,120,130,100,110,120,130}); CryptoStream MyCryptoStream=new CryptoStream(fs,MyTransform,CryptoStreamMode.Read); byte[] YourInputStorage=new byte[1000]; int len=MyCryptoStream.Read(YourInputStorage,0,YourInputStorage.Length); Console.WriteLine("你刚才输入的字符串是:"); Console.WriteLine(System.Text.Encoding.UTF8.GetString(YourInputStorage,0,len)); } }} 直接将字符串加密呢 //加密public string DesEncrypt(string strText, string strEncrKey) ...{ byte[] byKey=null; byte[] IV= ...{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; try ...{ byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ; cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch(System.Exception error) ...{ MessageBox.Show(error.Message); return "error:" +error.Message+" "; } } //解密函数 public string DesDecrypt(string strText,string sDecrKey) ...{ byte[] byKey = null; byte[] IV= ...{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; byte[] inputByteArray = new Byte[strText.Length]; try ...{ byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch(System.Exception error) ...{ MessageBox.Show(error.Message); return "error:"+error.Message+" "; } } //或者下面的另一个程序代码:using System;using System.Security.Cryptography;using System.IO;using System.Text;public class EncryptStringDES ...{ public static void Main(String[] args) ...{ if (args.Length < 1) ...{ Console.WriteLine("Usage: des_demo ", args[0]); return; } // 使用UTF8函数加密输入参数 UTF8Encoding utf8Encoding = new UTF8Encoding(); byte[] inputByteArray = utf8Encoding.GetBytes(args[0].ToCharArray()); // 方式一:调用默认的DES实现方法DES_CSP. DES des = DES.Create(); // 方式二:直接使用DES_CSP()实现DES的实体 //DES_CSP DES = new DES_CSP(); // 初始化DES加密的密钥和一个随机的、8比特的初始化向量(IV) Byte[] key = ...{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; Byte[] IV = ...{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}; des.Key = key; des.IV = IV; // 建立加密流 SymmetricStreamEncryptor sse = des.CreateEncryptor(); // 使用CryptoMemoryStream方法获取加密过程的输出 CryptoMemoryStream cms = new CryptoMemoryStream(); // 将SymmetricStreamEncryptor流中的加密数据输出到CryptoMemoryStream中 sse.SetSink(cms); // 加密完毕,将结果输出到控制台 sse.Write(inputByteArray); sse.CloseStream(); // 获取加密数据 byte[] encryptedData = cms.Data; // 输出加密后结果 Console.WriteLine("加密结果:"); for (int i = 0; i < encryptedData.Length; i++) ...{ Console.Write("{0:X2} ",encryptedData[i]); } Console.WriteLine(); //上面演示了如何进行加密,下面演示如何进行解密 SymmetricStreamDecryptor ssd = des.CreateDecryptor(); cms = new CryptoMemoryStream(); ssd.SetSink(cms); ssd.Write(encryptedData); ssd.CloseStream(); byte[] decryptedData = cms.Data; char[] decryptedCharArray = utf8Encoding.GetChars(decryptedData); Console.WriteLine("解密后数据:"); Console.Write(decryptedCharArray); Console.WriteLine(); }}