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. 关于android内存泄漏的研究

    博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基本也快结束了,有点时间来写写博客.好了 ...

  2. CentOS服务器 6.6 安装MySQL5.5.46

    原文:http://www.linuxidc.com/Linux/2012-06/62288.htm 稍微修改了几个空格的错误 一.安装cmake# 安装所需依赖包(这段指令是一直输入的)yum -y ...

  3. 4.0 spring-注册解析的Bean

    1.0 registerBeanDefinition 对于配置文件,解析也解析完了,装饰也装饰完了,对于得到的BeanDefinition已经可以满足后续的使用了,唯一剩下的工作就是注册了, 也就是: ...

  4. C#读取网页源码

    #region 1.读取 网页源码 + static string ReadHtml(string urlStr,int type) /// <summary> /// 读取 网页源码 + ...

  5. WPF SplitButton 的杂七杂八

    原文: http://www.codeproject.com/Articles/20612/A-WPF-SplitButton SplitButton.cs using System; using S ...

  6. uva10943

    递推  还是比较容易的 /************************************************************************* > Author: ...

  7. URAL 1260 Nudnik Photographer(递推)

    题目链接 题意 : 给你1到n这n个数,排成一排,然后1放在左边最开始,剩下的数进行排列,要求排列出来的数列必须满足任何两个相邻的数之间的差不能超过2,问你有多少种排列 思路 : 对于dp[n], n ...

  8. poj 3301 Texas Trip 三分法

    思路:三分法求解凸函数的极值,三分法介绍在这:http://hi.baidu.com/czyuan_acm/item/81b21d1910ea729c99ce33db 很容易就可以推出旋转后的坐标: ...

  9. 李洪强iOS开发之静态库

    iOS开发拓展篇—静态库 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的分类 根据源代码的公开情况,库可以分为2种类型 (1)开源库 公开源代码,能看到具体实现 ...

  10. jquery ajax 访问webServer的xml文件

    最近项目中要使用到通过ajax访问webServer的xml文件,通过下面的方式可以直接访问webServer的xml文件,不需要在web.xml中进行任何配置.它的返回参数就是服务器上的xml文件. ...