采用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. c#用object将datatable快速填充excel后下载表格后打不开的问题

    最近在用c#的asp.net,需要批量导出数据.原本用的是stringbuilder逐个填充,但是只能做到html强制格式转换为xls,这不是真正的excel表格,所以在网上找了datatable快速 ...

  2. MySQL存储引擎MyISAM与InnoDB的区别比较

    使用MySQL当然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都会看到. MySQL默认的存储引擎是MyISAM,其他常用的就是InnoDB了. 至于到底用哪种存储引擎比较好?这个问题 ...

  3. Vuejs中关于computed、methods、watch的区别。

    Vue.js在模板表达式中限制了,绑定表达式最多只能有一条表达式,但某些数据需要一条以上的表达式运算实现,此时就可以将此数据放在计算属性(computed)当中. Vuejs中关于computed.m ...

  4. python简单试题4

    ( ps : 题目中用到的一些random函数在最后末尾处有介绍)  1,在屏幕上显示跑马灯文字 import os # 调用os模块 import time # 调用时间模块 def main(): ...

  5. Green Space【绿色空间】

    Green Space Living in an urban area with green spaces has a long-lasting positive impact on people's ...

  6. Unity 与Mono和.Net的关系

    一.分析 首先,我们要知道Unity,Mono,.Net 三者的关系.需要简单说一下.Net. .Net拥有跨语言,跨平台性. 跨语言:就是只要是面向.Net平台的编程语言,用其中一种语言编写的类型就 ...

  7. django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x97\\xAE\\xE9\\xA2\\x98' for column 'name' at row 5")

    django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x ...

  8. 朴素贝叶斯python小样本实例

    朴素贝叶斯优点:在数据较少的情况下仍然有效,可以处理多类别问题缺点:对于输入数据的准备方式较为敏感适用数据类型:标称型数据朴素贝叶斯决策理论的核心思想:选择具有最高概率的决策朴素贝叶斯的一般过程(1) ...

  9. 【Best Time to Buy and Sell Stock III 】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  10. Monkey官方帮助翻译&介绍

    都说想学好就看原文,英文不好为了翻译这个可费了大劲了.表格从GOOGLE官网复制到WORD里编辑,结果贴上来格式全乱了,只得不弄表格了.表格中官网的事件不是最新的,比最新的少2个,具体我会另发一篇文章 ...