public class DBSecurity
{
//sKey sIV这两个自己随意设定,不能外泄
private const string sKey = "11,22,33,43,34,56,65,78";
private const string sIV = "12,23,21,34,65,56,85,96";
# region 加密解密
//方法
//加密方法
public static string Encrypt(string pToEncrypt)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); //建立加密对象的密钥和偏移量 byte[] b_key = new byte[];
string[] s_keys = new string[];
s_keys = sKey.Split(','); for (int i = ; i <= ; i++)
{
b_key[i] = Convert.ToByte(s_keys[i].ToString());
} des.Key = b_key; byte[] b_iv = new byte[];
string[] s_ivs = new string[];
s_ivs = sIV.Split(','); for (int i = ; i <= ; i++)
{
b_iv[i] = Convert.ToByte(s_ivs[i].ToString());
} des.IV = b_iv; MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch
{
return "";
} } //解密方法
public static string Decrypt(string pToDecrypt)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / ];
for (int x = ; x < pToDecrypt.Length / ; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * , ), ));
inputByteArray[x] = (byte)i;
} //建立加密对象的密钥和偏移量,此值重要,不能修改
byte[] b_key = new byte[];
string[] s_keys = new string[];
s_keys = sKey.Split(','); for (int i = ; i <= ; i++)
{
b_key[i] = Convert.ToByte(s_keys[i].ToString());
} des.Key = b_key; byte[] b_iv = new byte[];
string[] s_ivs = new string[];
s_ivs = sIV.Split(','); for (int i = ; i <= ; i++)
{
b_iv[i] = Convert.ToByte(s_ivs[i].ToString());
} des.IV = b_iv; MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock(); //Get the decrypted data back from the memory stream
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch (Exception exp)
{
string s = exp.Message.ToString();
return "";
}
} #endregion
}

利用CryptoStream进行加密解密的更多相关文章

  1. C#原生加密方法: System.Security.Cryptography.CryptoStream DataSet加密解密

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

  2. 利用MYSQL的加密解密办法应对三级安全等级保护

    -- 创建测试表 drop table if EXISTS t_passwd_2; create table t_passwd_2(pass1 varchar(64)); -- 对身份证号加密inse ...

  3. (译)利用ASP.NET加密和解密Web.config中连接字符串

    介绍 这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串 背景描述 在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Server, A ...

  4. 利用ASP.NET加密和解密Web.config中连接字符串

    摘自:博客园 介绍 这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串 背景描述 在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Se ...

  5. 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. 利用openssl进行RSA加密解密

    openssl是一个功能强大的工具包,它集成了众多密码算法及实用工具.我们即可以利用它提供的命令台工具生成密钥.证书来加密解密文件,也可以在利用其提供的API接口在代码中对传输信息进行加密. RSA是 ...

  7. 加密解密,CryptoStream()的使用

    一:上图 二:代码 主界面代码 using System; using System.Collections.Generic; using System.ComponentModel; using S ...

  8. c# 如何利用异或运算进行简单加密解密

    利用“^”异或运算对字符串进行加密 原理:按位做“异或”运算是->位值相同得1,不同得0,如下计算 1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0 例如: < ...

  9. 利用RSACryptoServiceProvider进行RSA加密解密

    前言: 本文只介绍How to use,对于加密算法的研究不予讨论. 关于私钥的存储,微软给的建议是使用windows自带的秘钥容器,相见文档. 为了直观看到私钥和公钥,本文直接将其存入XML文件中. ...

随机推荐

  1. 小小地预览HTML5

    程序示例 <!doctype html> <html> <head> <title>First </title> <meta char ...

  2. The 7th Zhejiang Provincial Collegiate Programming Contest->Problem B:B - Somali Pirates

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3323 题意:去掉字符串里面的数字然后按输入顺序输出 #include< ...

  3. Hibernate4.1.4配置二级缓存EHCache步骤

    1.当然首先引入EHCache相关的jar包 这些包不需要另外下载,在Hibernate官方网站下载Hibernate4.1.7的压缩包(如:hibernate-release-4.1.7.Final ...

  4. spoj 390

    简单题  记得uva上有个一样的  画个图就好了 #include <cstdio> #include <cmath> const double pi = acos(-1); ...

  5. Python的作用域

    Python的作用域 转自:http://www.cnblogs.com/frydsh/archive/2012/08/12/2602100.html Python是静态作用域语言,尽管它自身是一个动 ...

  6. snoopy(强大的PHP采集类) 详细介绍

    Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单,可以用来开发一些采集程序和小偷程序,本文章详细介绍snoopy的使用教程. Snoopy的一些特点: 抓取网页的内容 fe ...

  7. linux 配置java环境变量

    修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题. ·用文本编辑器打开/etc/profi ...

  8. [itint5]支持删除的后继查询

    http://www.itint5.com/oj/#49 这一题一开始想到是用HashSet+链表来做,链表记录prev和next.这样也可以,后来看到都是连续的整数,而且交流了一下觉得可以用类似并查 ...

  9. UIWebView与JS的深度交互

    我要实现这样一个需求:按照本地的CSS文件展示一串网络获取的带HTML格式的只有body部分的文本,需要自己拼写完整的 HTML.除此之外,还需要禁用获取的HTML文本中自带的 < img &g ...

  10. editplus的配置文件来支持sql语法高亮【转】

      editplus默认是没有sql语法高亮的,原因是它的内部没有sql.stx的这样一个语法文件 我们自己在 EditPlus 的安装目录下面新建一个文件名为sql.stx,然后打开editplus ...