国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

文件加密及解密

2019-11-17 03:04:31
字體:
來源:轉載
供稿:網友

文件加密及解密

 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 Decryption14     {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             try70             {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             finally77             {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加密是的密鑰


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 林周县| 西昌市| 商都县| 巴塘县| 临西县| 唐海县| 金溪县| 谢通门县| 马关县| 丹寨县| 嵊州市| 资溪县| 宜兴市| 海宁市| 连江县| 博兴县| 禹州市| 乌拉特前旗| 东辽县| 石柱| 衡阳市| 延边| 开平市| 张掖市| 榆社县| 鄂州市| 沐川县| 永德县| 镇坪县| 革吉县| 景泰县| 钦州市| 兰西县| 玉门市| 安吉县| 横峰县| 陇西县| 奎屯市| 柏乡县| 舞阳县| 资源县|