A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-7-17 14:03  /  663 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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. namespace Sky.Decrypt
  8. {
  9.     /// <summary>
  10.     /// 解密
  11.     /// </summary>
  12.     public class Decryption
  13.     {
  14.         public Decryption()
  15.         {
  16.         }

  17.         /// <summary>
  18.         /// 获取文件内容——字符串
  19.         /// </summary>
  20.         /// <param name="path">文件路径</param>
  21.         /// <returns>文件内容</returns>
  22.         public string GetString(string path)
  23.         {
  24.             return this.DeserializeFile(path);
  25.         }

  26.         /// <summary>
  27.         /// 反序列化文件
  28.         /// </summary>
  29.         /// <param name="path">文件路径</param>
  30.         /// <returns>文件内容</returns>
  31.         private string DeserializeFile(string path)
  32.         {
  33.             string str = "";

  34.             if(!File.Exists(path))
  35.             {
  36.                 throw new Exception("File is not exist!");
  37.             }

  38.             IFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  39.             using(FileStream fileStream=new FileStream(path,FileMode.Open,FileAccess.Read))
  40.             {
  41.                 str = (string)binaryFormatter.Deserialize(fileStream);
  42.                 fileStream.Close();
  43.             }

  44.             return str;
  45.         }

  46.         public string DecryptString(string data,string key)
  47.         {
  48.             string str = string.Empty;

  49.             if(string.IsNullOrEmpty(data))
  50.             {
  51.                 throw new Exception("data is empty");
  52.             }

  53.             MemoryStream ms = new MemoryStream();
  54.             byte[] myKey = Encoding.UTF8.GetBytes(key);
  55.             byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

  56.             DES myProvider = new DESCryptoServiceProvider();
  57.             CryptoStream cs = new CryptoStream(ms, myProvider.CreateDecryptor(myKey, myIV), CryptoStreamMode.Write);

  58.             try
  59.             {
  60.                 byte[] bs =Convert.FromBase64String(data);
  61.                 cs.Write(bs, 0, bs.Length);
  62.                 cs.FlushFinalBlock();
  63.                 str = Encoding.UTF8.GetString(ms.ToArray());
  64.             }
  65.             finally
  66.             {
  67.                 cs.Close();
  68.                 ms.Close();
  69.             }
  70.             return str;
  71.         }
  72.     }
  73. }
复制代码
加密:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.Serialization;
  5. using System.IO;
  6. using System.Security.Cryptography;

  7. namespace Sky.Encrypt
  8. {
  9.     /// <summary>
  10.     /// 加密
  11.     /// </summary>
  12.     public class Encryption
  13.     {
  14.         /// <summary>
  15.         /// 生成证书文件
  16.         /// </summary>
  17.         /// <param name="data">注册信息</param>
  18.         /// <param name="fileName">证书文件路径</param>
  19.         /// <param name="key"></param>
  20.         public void GenerateFile(string data,string fileName,string key)
  21.         {
  22.             string str = this.EncryptString(data, key);
  23.             this.SerializeFile(str,fileName);
  24.         }

  25.         /// <summary>
  26.         /// 序列化对象
  27.         /// </summary>
  28.         /// <param name="data">数据字符串</param>
  29.         /// <param name="path">文件路径</param>
  30.         private void SerializeFile(string data, string path)
  31.         {
  32.             IFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  33.             if(data!=null)
  34.             {
  35.                 using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
  36.                 {
  37.                     binaryFormatter.Serialize(fileStream, data);
  38.                     fileStream.Close();
  39.                 }
  40.             }
  41.         }

  42.         public string EncryptString(string data, string key)
  43.         {
  44.             string str = string.Empty;

  45.             if(string.IsNullOrEmpty(data))
  46.             {
  47.                 return str;
  48.             }

  49.             MemoryStream ms = new MemoryStream();
  50.             byte[] myKey = Encoding.UTF8.GetBytes(key);
  51.             byte[] myIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

  52.             DES myProvider = new DESCryptoServiceProvider();
  53.             CryptoStream cs = new CryptoStream(ms, myProvider.CreateEncryptor(myKey, myIV), CryptoStreamMode.Write);

  54.             try
  55.             {
  56.                 byte[] bs = Encoding.UTF8.GetBytes(data);
  57.                 cs.Write(bs, 0, bs.Length);
  58.                 cs.FlushFinalBlock();
  59.                 str = Convert.ToBase64String(ms.ToArray());
  60.             }
  61.             finally
  62.             {
  63.                 cs.Close();
  64.                 ms.Close();
  65.             }
  66.             return str;
  67.         }
  68.     }
  69. }
复制代码

调用加密文件:

  1. Encryption encry = new Encryption();

  2. string xmldata = File.ReadAllText("文件路径1");

  3. string data = encry.EncryptString(xmldata,"abcdefgh");//abcdefgh关键,密码

  4. File.WriteAllText("保存到文件2",data);
复制代码

解密

  1. Decryption decrypt = new Decryption();

  2. string strData = File.ReadAllText("保存到文件2");

  3. string newData = decrypt.DecryptString(strData,"abcdefgh");//abcdefgh加密是的密钥
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马