博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密
阅读量:5035 次
发布时间:2019-06-12

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

采用16位密钥形式加密,把数据 dataset或文本转换为二进制流,然后进行加密解密。代码如下:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;namespace CryptoHelperLib{    public class CryptoHelper    {        // 对称加密算法提供器        private ICryptoTransform encryptor;     // 加密器对象        private ICryptoTransform decryptor;     // 解密器对象        //public string key = "ABCDEFGHIJKLMNOP";//长度16        //public static byte[] DESKey = new byte[] { 11, 23, 93, 102, 72, 41, 18, 12 };        //public static byte[] DESIV = new byte[] { 75, 158, 46, 97, 78, 57, 17, 36 };        private const int BufferSize = 1024;        public CryptoHelper(string algorithmName, string key)        {            SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName);            provider.Key = Encoding.UTF8.GetBytes(key);            provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };            encryptor = provider.CreateEncryptor();            decryptor = provider.CreateDecryptor();        }        public CryptoHelper(string key) : this("TripleDES", key) { }        public MemoryStream EncryptMemoryStream(MemoryStream itemStream)        {            // 创建空的密文流            MemoryStream encryptedStream = new MemoryStream();            CryptoStream cryptoStream =                new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);            // 将明文流写入到buffer中            // 将buffer中的数据写入到cryptoStream中            int bytesRead = 0;            byte[] buffer = new byte[BufferSize];            itemStream.Position = 0;            do            {                bytesRead = itemStream.Read(buffer, 0, BufferSize);                cryptoStream.Write(buffer, 0, bytesRead);            } while (bytesRead > 0);            cryptoStream.FlushFinalBlock();            byte[] buffer2 = encryptedStream.ToArray();            string encryptedText = Convert.ToBase64String(buffer2);            return encryptedStream;        }        public Stream EncryptByte(byte[] data)        {            MemoryStream clearStream = new MemoryStream(data);            clearStream.Position = 0;            // 创建空的密文流            MemoryStream encryptedStream = new MemoryStream();            CryptoStream cryptoStream =                new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);            // 将明文流写入到buffer中            // 将buffer中的数据写入到cryptoStream中            int bytesRead = 0;            byte[] buffer = new byte[BufferSize];            do            {                bytesRead = clearStream.Read(buffer, 0, BufferSize);                cryptoStream.Write(buffer, 0, bytesRead);            } while (bytesRead > 0);            cryptoStream.FlushFinalBlock();            // 获取加密后的文本            byte[] buffer2 = encryptedStream.ToArray();            string encryptedText = Convert.ToBase64String(buffer2);            return encryptedStream;        }        // 加密算法        public string EncryptText(string clearText)        {            // 创建明文流            byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);            MemoryStream clearStream = new MemoryStream(clearBuffer);            // 创建空的密文流            MemoryStream encryptedStream = new MemoryStream();            CryptoStream cryptoStream =                new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);            // 将明文流写入到buffer中            // 将buffer中的数据写入到cryptoStream中            int bytesRead = 0;            byte[] buffer = new byte[BufferSize];            do            {                bytesRead = clearStream.Read(buffer, 0, BufferSize);                cryptoStream.Write(buffer, 0, bytesRead);            } while (bytesRead > 0);            cryptoStream.FlushFinalBlock();            // 获取加密后的文本            buffer = encryptedStream.ToArray();            string encryptedText = Convert.ToBase64String(buffer);            return encryptedText;        }        public MemoryStream DecryptMemoryStream(MemoryStream encryptedStream)        {            MemoryStream clearStream = new MemoryStream();            CryptoStream cryptoStream =                new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);            int bytesRead = 0;            byte[] buffer = new byte[BufferSize];            do            {                bytesRead = cryptoStream.Read(buffer, 0, BufferSize);                clearStream.Write(buffer, 0, bytesRead);            } while (bytesRead > 0);            buffer = clearStream.GetBuffer();            MemoryStream clearStreamResult = new MemoryStream(buffer);            return clearStreamResult;        }        //        // 解密算法, http://www.51testing.com/html/67/n-220867-4.html        // 解密算法        public string DecryptText(string encryptedText)        {            byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);            Stream encryptedStream = new MemoryStream(encryptedBuffer);            MemoryStream clearStream = new MemoryStream();            CryptoStream cryptoStream =                new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);            int bytesRead = 0;            byte[] buffer = new byte[BufferSize];            do            {                bytesRead = cryptoStream.Read(buffer, 0, BufferSize);                clearStream.Write(buffer, 0, bytesRead);            } while (bytesRead > 0);            buffer = clearStream.GetBuffer();            string clearText =                Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length);            return clearText;        }        public static string Encrypt(string clearText, string key)        {            CryptoHelper helper = new CryptoHelper(key);            return helper.EncryptText(clearText);        }        public static string Decrypt(string encryptedText, string key)        {            CryptoHelper helper = new CryptoHelper(key);            return helper.DecryptText(encryptedText);        }    }}
View Code

调用示例:

// string key="ABCDEFGHIJKLMNOP"; //16位字符串        public byte[] DataSetToBytes(DataSet ds)        {            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();            MemoryStream dataStream = new MemoryStream();            MemoryStream dataStream2 = new MemoryStream();            ds.WriteXml(dataStream, XmlWriteMode.WriteSchema);                        CryptoHelperLib.CryptoHelper cryhelper = new CryptoHelperLib.CryptoHelper(key);            dataStream2= cryhelper.EncryptMemoryStream(dataStream);            byte[] buf = dataStream2.ToArray();            return buf;        }        public DataSet DataSetFromBytes(byte[] buf)        {            MemoryStream dataStream = new MemoryStream(buf);            MemoryStream dataStream2 = new MemoryStream();            CryptoHelperLib.CryptoHelper cryhelper = new CryptoHelperLib.CryptoHelper(key);            dataStream2 = cryhelper.DecryptMemoryStream(dataStream);            dataStream2.Position = 0;            DataSet ds = new DataSet();            ds.ReadXml(dataStream2);            return ds;        }

 

转载于:https://www.cnblogs.com/guo2001china/p/5363049.html

你可能感兴趣的文章
全文检索-Elasticsearch (四) elasticsearch.net 客户端
查看>>
Oracle DBMS_SESSION
查看>>
sublime复制当前行到下一行
查看>>
WPF 3D变换应用
查看>>
ArchLinux安装开源VMware Tools
查看>>
DB2 锁升级示例1
查看>>
16.RDD实战
查看>>
MainFrame知识小结(20120210)—dfsort/syncsort中的数据类型
查看>>
D - Flip tile
查看>>
Java连接RabbitMQ之创建连接
查看>>
开户vim编程之--cscope支持
查看>>
python数据类型图解
查看>>
C#微信登录-手机网站APP应用
查看>>
HTML5实践 -- iPhone Safari Viewport Scaling Bug
查看>>
一位数据挖掘成功人士 给 数据挖掘在读研究生 的建议
查看>>
Python3.6.0安装
查看>>
hdu1049
查看>>
H5项目常见问题及注意事项
查看>>
索尼(SONY) SVE1512S7C 把WIN8降成WIN7图文教程
查看>>
时间模块 && time datetime
查看>>