博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# DES加密解密用法
阅读量:6524 次
发布时间:2019-06-24

本文共 3896 字,大约阅读时间需要 12 分钟。

主要用到C#提供的以下三个类:

MemoryStream 内存流
DESCryptoServiceProvider 加密服务提供者类
CryptoStream 讲数据流连接到加密转换的流

下面是简易代码,已经过测试:

1     public static string strKey = "abcdefgh"; 2     public static string strIV = "ijklmnop"; 3     private XmlDocument XmlDoc = null; 4     private XmlElement XmlRoot = null; 5  6     // 字符串加密 7     public static string Encrypt(string _strQ) 8     { 9         byte[] buffer = Encoding.UTF8.GetBytes(_strQ);10         MemoryStream ms = new MemoryStream();11         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();12         CryptoStream encStream = new CryptoStream(ms, tdes.CreateEncryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);13         encStream.Write(buffer, 0, buffer.Length);14         encStream.FlushFinalBlock();15         return Convert.ToBase64String(ms.ToArray()).Replace("+", "%");16     }17 18     // 字符串解密19     public static string Decrypt(string _strQ)20     {21         _strQ = _strQ.Replace("%", "+");22         byte[] buffer = Convert.FromBase64String(_strQ);23         MemoryStream ms = new MemoryStream();24         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();25         CryptoStream encStream = new CryptoStream(ms, tdes.CreateDecryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);26         encStream.Write(buffer, 0, buffer.Length);27         encStream.FlushFinalBlock();28         return Encoding.UTF8.GetString(ms.ToArray());29     }30 31     // 解密加载32     public void XmlLoadDecrypt(XmlDocument xmlDoc, string fileName)33     {34         FileStream fileStream = new FileStream(fileName, FileMode.Open);35         byte[] bsXml = new byte[fileStream.Length];36         fileStream.Read(bsXml, 0, bsXml.Length);37         fileStream.Close();38 39         MemoryStream ms = new MemoryStream();40         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();41         CryptoStream encStream = new CryptoStream(ms, tdes.CreateDecryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);42         encStream.Write(bsXml, 0, bsXml.Length);43         encStream.FlushFinalBlock();44 45         xmlDoc.Load(new MemoryStream(ms.ToArray()));46     }47 48     // 加密存储49     public void XmlSaveEncrypt(XmlDocument xmlDoc, string fileName)50     {51         if (!File.Exists(fileName))52             File.Create(fileName).Close();53 54         FileStream fileStream = new FileStream(fileName, FileMode.Truncate);55         MemoryStream msXml = new MemoryStream();56         xmlDoc.Save(msXml);57 58         DESCryptoServiceProvider tdes = new DESCryptoServiceProvider();59         CryptoStream cs = new CryptoStream(fileStream, tdes.CreateEncryptor(Encoding.UTF8.GetBytes(strKey), Encoding.UTF8.GetBytes(strIV)), CryptoStreamMode.Write);60         cs.Write(msXml.ToArray(), 0, msXml.ToArray().Length);61         cs.FlushFinalBlock();62 63         msXml.Close();64         fileStream.Close();65     }66 67     // 测试代码68     string str1 = "abcdefghijklmnopqrstuvwxyz";69     if (GUI.Button(new Rect(0, 300, 100, 50), "Encrypt"))70     {71         Debug.Log(Encrypt(str1));72     }73     if (GUI.Button(new Rect(0, 350, 100, 50), "Decrypt"))74     {75         Debug.Log(Decrypt(Encrypt(str1)));76     }77     if (GUI.Button(new Rect(0, 400, 100, 50), "Load Decrypt Xml"))78     {79         XmlDoc = new XmlDocument();80         XmlLoadDecrypt(XmlDoc, "Assets/123.xml");81         XmlRoot = XmlDoc.DocumentElement;82         Debug.Log(XmlRoot.GetAttribute("Test"));83     }84     if (GUI.Button(new Rect(0, 450, 100, 50), "Save Encrypt Xml"))85     {86         XmlDoc = new XmlDocument();87         XmlRoot = XmlDoc.CreateElement("RegisterRecords");88         XmlDoc.AppendChild(XmlRoot);89         XmlRoot.SetAttribute("Test", "something");90         XmlSaveEncrypt(XmlDoc, "Assets/123.xml");91     }

 

 

转载地址:http://lyjbo.baihongyu.com/

你可能感兴趣的文章
微服务架构优缺点
查看>>
ext3与ext4区别
查看>>
UNIX/Linux 系统管理技术手册阅读(三)
查看>>
btrfs的使用(案例讲解)
查看>>
LVS Nginx HAProxy 优缺点
查看>>
images对象实现图片幻灯片
查看>>
Oracle 12c 日常维护
查看>>
Cobbler简介
查看>>
恢复 git reset -hard 的误操作
查看>>
C# WinForm 文件上传下载
查看>>
Spring Integration概述
查看>>
RDIFramework.NET ━ 9.6 模块(菜单)管理 ━ Web部分
查看>>
Android安全问题 静音拍照与被拍
查看>>
cocos2d-x 3.1.1 学习笔记[13] listen 监听器
查看>>
WTL介绍
查看>>
应用程序框架实战三十四:数据传输对象(DTO)介绍及各类型实体比较(转)
查看>>
放量滞涨,抛出信号
查看>>
BeanFactory not initialized or already closed - call 'refresh' before accessing beans解决办法
查看>>
linux主机下的Vmware Workstation配置NAT设置 端口映射-Ubuntu为例
查看>>
unity physics joint
查看>>