代码语言
.
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
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
MD5加解密
作者:
小小超
/ 发布于
2012/6/13
/
682
MD5加解密
<div> <strong>其中的一个加密解密类:</strong> <strong>using System; using System.Collections.Generic; using System.Text; using System.Globalization; using System.Security.Cryptography; using System.IO; namespace WindowsApplication2 { class DES { // 创建Key public string GenerateKey() { DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } ///MD5加密 public string MD5Encrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString();</strong> <strong> }</strong> <strong> ///MD5解密 public string MD5Decrypt(string pToDecrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider();</strong> <strong> byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; }</strong> <strong> des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock();</strong> <strong> StringBuilder ret = new StringBuilder();</strong> <strong> return System.Text.Encoding.Default.GetString(ms.ToArray()); }</strong> <strong> } }</strong> <strong>另外一个加密解密类:</strong> <strong>using System; using System.Text; using System.IO; using System.Globalization; using System.Security.Cryptography; using System.Collections.Generic;</strong> <strong>namespace WindowsApplication2 { class MD5 { // 创建Key public string GenerateKey() { DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } // 加密字符串 public string EncryptString(string sInputString, string sKey) { byte[] data = Encoding.UTF8.GetBytes(sInputString); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); return BitConverter.ToString(result); } // 解密字符串 public string DecryptString(string sInputString, string sKey) { string[] sInput = sInputString.Split("-".ToCharArray()); byte[] data = new byte[sInput.Length]; for (int i = 0; i < sInput.Length; i++) { data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber); } DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateDecryptor(); byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); return Encoding.UTF8.GetString(result); }</strong> <strong> } } </strong> <strong>调用这两个类:</strong> <strong>public partial class EDForm : Form { public EDForm() { InitializeComponent(); } DES des = new DES(); string key = null; /// <summary> /// 加密字符串 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { key = des.GenerateKey(); this.textBox2.Text = des.MD5Encrypt(textBox1.Text,key); }</strong> <strong> private void button2_Click(object sender, EventArgs e) { MessageBox.Show(des.MD5Decrypt(textBox2.Text, key)); } }</strong> 注意:在调用时key必须是同一个,如若不然就会报“输入的数据格式不对”错了。 </div>
试试其它关键字
MD5加解密
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
小小超
贡献的其它代码
(
2
)
.
MD5加解密
.
读取SQLSERVER中字段备注信息
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3