【ADO.NET基础-数据加密】第一篇(加密解密篇)
可以采用下面的函数实现密码的加密
public static string EncryptString(string str)
{
//密文
string key = "www"; //把密文转成二进制型 byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //把字符串转成二进制型 byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str); //生成DESCryptoServiceProvider对象
System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //设定
des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //写出被加密的数据的MemoryStream
System.IO.MemoryStream msOut = new System.IO.MemoryStream(); //生成DES加密对象
System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateEncryptor(); //生成CryptoStream
System.Security.Cryptography.CryptoStream cryptStreem =
new System.Security.Cryptography.CryptoStream(msOut,
desdecrypt,
System.Security.Cryptography.CryptoStreamMode.Write); //写入
cryptStreem.Write(bytesIn, , bytesIn.Length);
cryptStreem.FlushFinalBlock(); //取得加密后的数据
byte[] bytesOut = msOut.ToArray(); //关闭
cryptStreem.Close();
msOut.Close(); //返回
return System.Convert.ToBase64String(bytesOut);
} 可以采用下面的函数实现密码的解密
/// <summary>
/// 对数据解密 /// </summary>
/// <param name="str">被加密的数据</param>
/// <returns>解密后的数据</returns>
public static string DecryptString(string str)
{
//秘文
string key = "www"; string result = "";
//把密文转成二进制型
byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //生成DESCryptoServiceProvider对象
System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //设定
des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //返回
byte[] bytesIn = System.Convert.FromBase64String(str); //生成MemoryStream对象以便读出被加密的数据
System.IO.MemoryStream msIn = new System.IO.MemoryStream(bytesIn); //生成DES复原对象
System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor(); //生成CryptoStream对象以便读入数据
System.Security.Cryptography.CryptoStream cryptStreem =
new System.Security.Cryptography.CryptoStream(msIn,
desdecrypt,
System.Security.Cryptography.CryptoStreamMode.Read); //生成StreamReader以便取得解密后的数据
System.IO.StreamReader srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8); //取得解密后的数据
result = srOut.ReadToEnd(); //关闭
srOut.Close();
cryptStreem.Close();
msIn.Close(); return result;
} /// <summary>
/// 使用共有key变更二进制的数据的大小 /// </summary>
/// <param name="bytes">变更二进制的数据的大小</param>
/// <param name="newSize">二进制的数据的大小</param>
/// <returns>变更后的二进制列</returns>
private static byte[] ResizeBytesArray(byte[] bytes, int newSize)
{
byte[] newBytes = new byte[newSize];
if (bytes.Length < newSize)
{
for (int i = ; i < bytes.Length; i++)
newBytes[i] = bytes[i];
}
else
{
int pos = ;
for (int i = newSize; i < bytes.Length; i++)
{
newBytes[pos++] ^= bytes[i];
if (pos >= newBytes.Length)
pos = ;
}
}
【ADO.NET基础-数据加密】第一篇(加密解密篇)的更多相关文章
- 【推荐】JAVA基础◆浅谈3DES加密解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- loadrunder之脚本篇——加密解密
密码加密 可以给密码加密,意在把结果字符串作为脚本的参数或者参数值.例如,完整可能有一个用户密码填写的表单,你想测试网站针对不同密码的反应,但是你又想保护密码的安全.Password Encoder允 ...
- linux基础之加密解密、PKI及SSL、创建私有CA
加密解密基础 1. 对称加密: 加密和解密使用同一个密钥 常见的加密算法有:DES.3DES.AES.Blowfish.Twofish.IDEA.RC6.CAST5 特性: 1. 加密.解密使用同一个 ...
- 创建私有CA, 加密解密基础, PKI, SSL
发现了一篇图文并茂的超棒的 加密解密, CA, PKI, SSL 的基础文章, 链接如下:https://blog.csdn.net/lifetragedy/article/details/5223 ...
- .NET进阶篇04-Serialize序列化、加密解密
知识需要不断积累.总结和沉淀,思考和写作是成长的催化剂这篇很轻松,没有什么费脑子的,所以解析较少,代码较多,为数不多的拿来即用篇整个章节分布请移步 内容目录 一.概述二.序列化1.二进制文件2.XML ...
- openssl生成CA签署 及 加密解密基础
openssl 生成私有CA 及签署证书 openssl 配置文件: /etc/pki/tls/openssl.cnf 1. 在openssl CA 服务器端生成私钥 cd /etc/pki/CA/ ...
- SWF加解密资源索引之加密混淆篇【转】
============================ SWF加解密资源索引之加密混淆篇 ============================ [心得] swf加密混淆器(带源码) http:/ ...
- 【MM系列】SAP MM模块-基础配置第一篇
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-基础配置第一篇 ...
- jmeter加密解密(加密篇)
最近刚好在弄jmeter加密解密,可以分享下.(有一段时间没写了,有点不知道从何写起,这篇写的有点乱o(╥﹏╥)o) 需求是:接口中的请求体的部分参数需要先加密再请求,返回的结果中部分字段需解密. 1 ...
随机推荐
- HTML(五)列表,区块,布局,表单和输入
HTML 列表 无序列表 Coffee Tea Milk 默认是圆点,也可以 圆圈 正方形 有序列表 Coffee Tea Milk Coffee Tea Milk 默认是用数字排序 大写字母 小写字 ...
- P1603 斯诺登的密码-字符串加法的妙用
传送门:https://www.luogu.org/problemnew/show/P1603 题意: 首先在给定的字符串中,找出特定的单词,把它转化成特定的数字, 然后在这些数字中,找出排列结果最小 ...
- Codeforces#398 &767C. Garland 树形求子节点的和
传送门 题意:在一个树上,问能否切两刀,使得三块的节点值的和相同. 思路: 由于这个总的节点和是不变的,每块的节点值和sum固定,dfs搜索,和等于sum/3,切.若不能分成三块(不能被3整除,-1) ...
- hdu 6092 Rikka with Subset(多重背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6092 #include <cstdio> #include <iostream> ...
- lightoj 1036 - A Refining Company(简单dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1036 题解:设dp[i][j]表示处理到(i,j)点时的最大值然后转移显然是 ...
- HDU 1223 还是畅通工程(最小生成树prim模板)
一个很简单的prim模板,但虽然是模板,但也是最基础的,也要脱离模板熟练打出来 后期会更新kruskal写法 #include<iostream> #include<cstdio&g ...
- 【LeetCode】DFS 总结
DFS(深度优先搜索) 常用来解决可达性的问题. 两个要点: 栈:用栈来保存当前节点信息,当遍历新节点返回时能够继续遍历当前节点.可以使用递归栈. 标记:和 BFS 一样同样需要对已经遍历过的节点进行 ...
- top_down设计技巧
写在前面的话 之前梦翼师兄和大家一起学习了层次化设计方法,大家应该懂了,哦,原来所谓的层次化设计就是将一个大的系统不断地拆分成一些便于实现的最小逻辑单元.如果大家真的只是这么想的话,那么梦翼师兄真的是 ...
- redis.windows.conf配置详解
redis.windows.conf配置详解 转自:https://www.cnblogs.com/kreo/p/4423362.html # redis 配置文件示例 # 当你需要为某个配置项指定内 ...
- 基础知识:Mysql基本操作命令
启动mysql : mysql -hlocalhost -uroot -p 创建数据库:create database 数据库名字; 指定要操作的数据库:use 数据库名字; 查看数据表建表语句:s ...