采用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. python3 爬取汽车之家所有车型数据操作步骤(更新版)

    题记: 互联网上关于使用python3去爬取汽车之家的汽车数据(主要是汽车基本参数,配置参数,颜色参数,内饰参数)的教程已经非常多了,但大体的方案分两种: 1.解析出汽车之家某个车型的网页,然后正则表 ...

  2. [Bzoj2588]Count on a tree(主席树+LCA)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  3. ListNode Java创建链表

    用了一种自创的比较简洁的方式来创建链表 class ListNode { //为了方便,这两个变量都使用pub1ic, //存放数据的变量,直接为int型 public int data; //存放结 ...

  4. kettle - Linux下定时执行kettle作业

    Linux下实现kettle 自动同步数据 1.安装jdk tar -zxvf jdk-7u25-linux-x64.tar.gz -C /usr/share 2.配置java环境 vim /etc/ ...

  5. opencv中的仿射变换

    什么是仿射变换? 原理:1.一个任意的仿射变换都能表示为 乘以一个矩阵(线性变换) 接着再 加上一个向量(平移) 2.综上所述,我们能够用仿射变换来表示: 1)旋转(线性变换) 2)平移(向量加) 3 ...

  6. git使用问题整理

    git访问远端仓库报"fatal: Authentication failed for"错误的,可能原因是账户密码变更,git配置了使用creditial helper,所以需要取 ...

  7. Python框架之Django的相册组件

    Python框架之Django的相册组件 恩,没错,又是Django,虽然学习笔记已经结贴,但是学习笔记里都是基础的,Django的东西不管怎么说还是很多的,要学习的东西自然不会仅仅用十几篇博文就能学 ...

  8. 虚拟机VMware安Mac OS时没有Apple mac选项

    相信大家很多人在虚拟机安装mac os时候发现在选择客户机操作系统时候,没有Apple mac os选项,这样就会导致无法进行下一步,下面我来给大家详细介绍怎么添加这一项. 1.首先安装unlocke ...

  9. python实现删除空文件夹 附源代码

    前言:空文件夹虽然不占空间,但是有时候看着确实挺烦的(别误会,我不是强迫症!),所以写了一个用于删除当前目录下的空文件夹的小程序 环境:win7 64位:python2.7:IDE pycharm20 ...

  10. [笔记]《算法图解》第十章 K最近邻算法

    K最近邻算法 简称KNN,计算与周边邻居的距离的算法,用于创建分类系统.机器学习等. 算法思路:首先特征化(量化) 然后在象限中选取目标点,然后通过目标点与其n个邻居的比较,得出目标的特征. 余弦相似 ...