采用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 = ;
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 = ;
byte[] buffer = new byte[BufferSize];
itemStream.Position = ;
do
{
bytesRead = itemStream.Read(buffer, , BufferSize);
cryptoStream.Write(buffer, , bytesRead);
} while (bytesRead > ); 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 = ;
// 创建空的密文流
MemoryStream encryptedStream = new MemoryStream(); CryptoStream cryptoStream =
new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write); // 将明文流写入到buffer中
// 将buffer中的数据写入到cryptoStream中
int bytesRead = ;
byte[] buffer = new byte[BufferSize];
do
{
bytesRead = clearStream.Read(buffer, , BufferSize);
cryptoStream.Write(buffer, , bytesRead);
} while (bytesRead > );
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 = ;
byte[] buffer = new byte[BufferSize];
do
{
bytesRead = clearStream.Read(buffer, , BufferSize);
cryptoStream.Write(buffer, , bytesRead);
} while (bytesRead > ); 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 = ;
byte[] buffer = new byte[BufferSize]; do
{
bytesRead = cryptoStream.Read(buffer, , BufferSize);
clearStream.Write(buffer, , bytesRead);
} while (bytesRead > ); 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 = ;
byte[] buffer = new byte[BufferSize]; do
{
bytesRead = cryptoStream.Read(buffer, , BufferSize);
clearStream.Write(buffer, , bytesRead);
} while (bytesRead > ); buffer = clearStream.GetBuffer();
string clearText =
Encoding.UTF8.GetString(buffer, , (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);
}
}
}

调用示例:

    // 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 = ;
DataSet ds = new DataSet();
ds.ReadXml(dataStream2);
return ds;
}

C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密的更多相关文章

  1. 转:system.Security.Cryptography C# 加密和解密

    以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先 ...

  2. 部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法

    转载:http://www.cnblogs.com/jys509/p/4499978.html 在调用RSA加密的.pfx密钥时,在本地调试没有问题,可以布署到服务器,就会报以下的错误: 用户代码未处 ...

  3. "System.Security.Cryptography.CryptographicException: 拒绝访问" 问题的解决方法

    .net web程序使用rsa算法进行加解密时,程序报告“System.Security.Cryptography.CryptographicException: 拒绝访问”错.按网上搜的解决方法做了 ...

  4. net中System.Security.Cryptography 命名空间 下的加密算法

    .net中System.Security.Cryptography命名空间 在.NETFramework出现之前,如果我们需要进行加密的话,我们只有各种较底层的技术可以选择,如 Microsoft C ...

  5. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  6. 使用证书部署出现System.Security.Cryptography.CryptographicException 错误解决方案

    一.System.Security.Cryptography.CryptographicException: 找不到对象 at System.Security.Cryptography.Cryptog ...

  7. WCF:System.Security.Cryptography.CryptographicException: 密钥集不存在

    WCF使用IIS部署时,使用x509证书验证,在创建证书并正确配置程序后,访问出现错误提示: System.Security.Cryptography.CryptographicException: ...

  8. 多线程下System.Security.Cryptography.Aes CreateDecryptor报“Safe handle has been closed”的解决方案

    因为系统需要对一些核心数据进行预加载以保证查询速度. 所以在application_start 事件中启用了后台线程对相关的数据进行加载并解密(为了保证解密的效率,将AES对像做了静态对像来保存:pr ...

  9. System.Security.Cryptography.RSA.FromXmlString 系统找不到指定的文件和X509读取证书文件系统找不到指定的文件异常

    前言: 最近公司增加服务器,在新增加的服务器中发现一些问题. 1.应用程序在读取证书文件中出现"系统找不到指定的文件."异常,但是已经确认证书文件存在.本地测试也可以读取,就在新增 ...

随机推荐

  1. JZOJ 4272. 【NOIP2015模拟10.28B组】序章-弗兰德的秘密

    272. [NOIP2015模拟10.28B组]序章-弗兰德的秘密 (File IO): input:frand.in output:frand.out Time Limits: 1000 ms  M ...

  2. 如何将emoji表情存放到mysql数据库中

    昨晚在爬取猫眼电影评论时在将评论信息插入到数据库中时出现问题,总是在插入一条数据时就会报错: 看着应该时字符编码的问题,比如新建的数据库新建的表,默认字符编码是:Latin1, 这种编码是无法插入中文 ...

  3. The 2016 ACM-ICPC Asia Shenyang Regional Contest

    A. Thickest Burger 大数 × 2 + 小数 #include <cstdio> #include <algorithm> using namespace st ...

  4. HDU 1506 Largest Rectangle in a Histogram(单调栈、笛卡尔树)

    题意:给定n个连续排列的矩形的高,矩形的宽都为1.问最大矩形覆盖. 例如:n = 7,h[i] = (2 1 4 5 1 3 3),最大覆盖为8. Sample Input 7 2 1 4 5 1 3 ...

  5. 图学java基础篇之集合

    (本文部分图片引用自其他博客,最后有链接,侵删.由于笔记使用markdown记录,格式可能不是太好看,见谅) 集合结构 红字为java.util包下的,绿字为concurrent包下扩展的与并发相关的 ...

  6. HTML练习题

    1.查询一下对div和span标签的理解 div标签:是用来为HTML文档内大块的内容提供结构和背景的元素.DIV的起始标签和结束标签之间的所有内容都是用来构成这个块的,中文我们把它称作“层”. sp ...

  7. 【bzoj3339】Rmq Problem

    [bzoj3339]Rmq Problem   Description Input Output Sample Input 7 50 2 1 0 1 3 21 32 31 43 62 7 Sample ...

  8. laravel5.2总结--任务调度

    你可以通过 command() 来调用 artisan 命令, call 来调用方法或函数, 或者 terminal() 来执行单行命令脚本:   1.在app/Console/Commands文件夹 ...

  9. windows下git安装以及基本配置

    一.首先要下载git然后进行默认安装即可,然后查看系统环境变量是否添加     二.桌面右击鼠标,选择git bash 进入操作页面 git --version 检测是否安装成功   安装完成有配置一 ...

  10. Python 字节与字符串的转换

    html = urlopen("http://www.cnblogs.com/ryanzheng/p/9665224.html") bsObj = BeautifulSoup(ht ...