代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
ASP.NET AES/DES加密、解密函数
作者:
Dezai.CN
/ 发布于
2011/7/8
/
687
using System.Web.Security;using System.Security.Cryptography;
<div><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> AES解密函数 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="pToDencrypt"></span><span style="color: rgb(0,128,0)">要解密的字符串</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="skey"></span><span style="color: rgb(0,128,0)">私钥(最高32字节,对应256位AES)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">解密后的结果</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> AESDecrypt(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> pToDencrypt, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> skey) { </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] keyArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> UTF8Encoding.UTF8.GetBytes(skey); </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] toEncryptArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Convert.FromBase64String(pToDencrypt); RijndaelManaged rDel </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> RijndaelManaged(); rDel.Key </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> keyArray; rDel.Mode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> CipherMode.ECB; rDel.Padding </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> PaddingMode.PKCS7; ICryptoTransform cTransform </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rDel.CreateDecryptor(); </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] resultArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> cTransform.TransformFinalBlock(toEncryptArray, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, toEncryptArray.Length); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> UTF8Encoding.UTF8.GetString(resultArray); } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> AES加密函数 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="pToEncrypt"></span><span style="color: rgb(0,128,0)">要加密的字符串</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="skey"></span><span style="color: rgb(0,128,0)">私钥(最高32字节,对应256位AES)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">加密结果</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> AESEncrypt(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> pToEncrypt, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> skey) { </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] keyArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> UTF8Encoding.UTF8.GetBytes(skey); </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] toEncryptArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> UTF8Encoding.UTF8.GetBytes(pToEncrypt); RijndaelManaged rDel </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> RijndaelManaged(); rDel.Key </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> keyArray; rDel.Mode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> CipherMode.ECB; rDel.Padding </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> PaddingMode.PKCS7; ICryptoTransform cTransform </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> rDel.CreateEncryptor(); </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] resultArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> cTransform.TransformFinalBlock(toEncryptArray, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, toEncryptArray.Length); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> Convert.ToBase64String(resultArray, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, resultArray.Length); } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 进行DES解密。 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="pToDecrypt"></span><span style="color: rgb(0,128,0)">要解密的字符串</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="sKey"></span><span style="color: rgb(0,128,0)">8位密钥</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">已解密的字符串。</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> DESDecrypt(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> pToDecrypt, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> sKey) { DESCryptoServiceProvider des </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> DESCryptoServiceProvider(); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">把字符串放入byte数组 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> len </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; len </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> pToDecrypt.Length </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] inputByteArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[len </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">]; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)"> (x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; x </span><span style="color: rgb(0,0,0)"><=</span><span style="color: rgb(0,0,0)"> len; x</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { i </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Convert.ToInt32(pToDecrypt.Substring(x </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">), </span><span style="color: rgb(128,0,128)">16</span><span style="color: rgb(0,0,0)">); inputByteArray[x] </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Convert.ToByte(i); } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">建立加密对象的密钥和偏移量,此值重要,不能修改 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> des.Key </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ASCIIEncoding.ASCII.GetBytes(sKey); des.IV </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ASCIIEncoding.ASCII.GetBytes(sKey); System.IO.MemoryStream ms </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.IO.MemoryStream(); CryptoStream cs </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, inputByteArray.Length); cs.FlushFinalBlock(); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> Encoding.Default.GetString(ms.ToArray()); } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> DES加密函数 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="pToEncrypt"></span><span style="color: rgb(0,128,0)">要加密的字符串</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="sKey"></span><span style="color: rgb(0,128,0)">8位密钥</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">加密后的字符串</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> DESEncrypt(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> pToEncrypt, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> sKey) { DESCryptoServiceProvider des </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> DESCryptoServiceProvider(); </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)">[] inputByteArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">; inputByteArray </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Encoding.Default.GetBytes(pToEncrypt); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">建立加密对象的密钥和偏移量 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">原文使用ASCIIEncoding.ASCII方法的GetBytes方法 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">使得输入密码必须输入英文文本 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> des.Key </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ASCIIEncoding.ASCII.GetBytes(sKey); des.IV </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> ASCIIEncoding.ASCII.GetBytes(sKey); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">写二进制数组到加密流 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">(把内存流中的内容全部写入) </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> System.IO.MemoryStream ms </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.IO.MemoryStream(); CryptoStream cs </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">写二进制数组到加密流 </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">(把内存流中的内容全部写入) </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> cs.Write(inputByteArray, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, inputByteArray.Length); cs.FlushFinalBlock(); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">建立输出字符串 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> StringBuilder ret </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> StringBuilder(); </span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)"> b </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">byte</span><span style="color: rgb(0,0,0)"> b_loopVariable </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> ms.ToArray()) { b </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> b_loopVariable; ret.AppendFormat(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">{0:X2}</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, b); } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> ret.ToString(); }</span></div>
试试其它关键字
DES
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3