文件加密及解密
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 using System.Runtime.Serialization; 6 using System.Security.Cryptography; 7 8 namespace Sky.Decrypt 9 { 10 /// <summary> 11 /// 解密 12 /// </summary> 13 public class Decryption 14 { 15 public Decryption() 16 { 17 } 18 19 /// <summary> 20 /// 获取文件内容——字符串 21 /// </summary> 22 /// <param name="path">文件路径</param> 23 /// <returns>文件内容</returns> 24 public string GetString(string path) 25 { 26 return this.DeserializeFile(path); 27 } 28 29 /// <summary> 30 /// 反序列化文件 31 /// </summary> 32 /// <param name="path">文件路径</param> 33 /// <returns>文件内容</returns> 34 private string DeserializeFile(string path) 35 { 36 string str = ""; 37 38 if(!File.Exists(path)) 39 { 40 throw new Exception("File is not exist!"); 41 } 42 43 IFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 44 using(FileStream fileStream=new FileStream(path,FileMode.Open,FileAccess.Read)) 45 { 46 str = (string)binaryFormatter.Deserialize(fileStream); 47 fileStream.Close(); 48 } 49 50 return str; 51 } 52 53 public string DecryptString(string data,string key) 54 { 55 string str = string.Empty; 56 57 if(string.IsNullOrEmpty(data)) 58 { 59 throw new Exception("data is empty"); 60 } 61 62 MemoryStream ms = new MemoryStream(); 63 byte[] myKey = Encoding.UTF8.GetBytes(key); 64 byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 65 66 DES myProvider = new DESCryptoServiceProvider(); 67 CryptoStream cs = new CryptoStream(ms, myProvider.CreateDecryptor(myKey, myIV), CryptoStreamMode.Write); 68 69 try 70 { 71 byte[] bs =Convert.FromBase64String(data); 72 cs.Write(bs, 0, bs.Length); 73 cs.FlushFinalBlock(); 74 str = Encoding.UTF8.GetString(ms.ToArray()); 75 } 76 finally 77 { 78 cs.Close(); 79 ms.Close(); 80 } 81 return str; 82 } 83 } 84 }
加密:
using System; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; using System.IO; using System.Security.Cryptography; namespace Sky.Encrypt { /// <summary> /// 加密 /// </summary> public class Encryption { /// <summary> /// 生成证书文件 /// </summary> /// <param name="data">注册信息</param> /// <param name="fileName">证书文件路径</param> /// <param name="key"></param> public void GenerateFile(string data,string fileName,string key) { string str = this.EncryptString(data, key); this.SerializeFile(str,fileName); } /// <summary> /// 序列化对象 /// </summary> /// <param name="data">数据字符串</param> /// <param name="path">文件路径</param> private void SerializeFile(string data, string path) { IFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); if(data!=null) { using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write)) { binaryFormatter.Serialize(fileStream, data); fileStream.Close(); } } } public string EncryptString(string data, string key) { string str = string.Empty; if(string.IsNullOrEmpty(data)) { return str; } MemoryStream ms = new MemoryStream(); byte[] myKey = Encoding.UTF8.GetBytes(key); byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; DES myProvider = new DESCryptoServiceProvider(); CryptoStream cs = new CryptoStream(ms, myProvider.CreateEncryptor(myKey, myIV), CryptoStreamMode.Write); try { byte[] bs = Encoding.UTF8.GetBytes(data); cs.Write(bs, 0, bs.Length); cs.FlushFinalBlock(); str = Convert.ToBase64String(ms.ToArray()); } finally { cs.Close(); ms.Close(); } return str; } } }
调用加密文件:
Encryption encry = new Encryption();
string xmldata = File.ReadAllText("文件路径1");
string data = encry.EncryptString(xmldata,"abcdefgh");//abcdefgh关键,密码
File.WriteAllText("保存到文件2",data);
解密
Decryption decrypt = new Decryption();
string strData = File.ReadAllText("保存到文件2");
string newData = decrypt.DecryptString(strData,"abcdefgh");//abcdefgh加密是的密钥