字符串加密和解密函数 我要将用户名和密码加密存到数据库中,在将它取出来时在解密显示出来代码怎么写在线等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 字符串加密和解密函数http://blog.csdn.net/net_lover/archive/2003/05.aspx 用一个 GetHashCode()最简单 net_lover和我自己写的一样但是解密时返回值为空LY105有例子吗 lz要的是对称的加密算法,md5明显不能用 public static string DesDecrypt(string PasswordString) { byte[] byKey = null; byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; byte[] inputByteArray = new Byte[PasswordString.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes("&%#@?,:*".Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(PasswordString); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch(System.Exception error) { return error.Message; } }这里解密时返回值为空 using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.IO;using System.Text;using System.Security.Cryptography;namespace eMeng.Exam{/// <summary>/// EncryptString 的摘要说明。/// </summary>public class EncryptString : System.Web.UI.Page{protected System.Web.UI.WebControls.TextBox TextBox1;protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;protected System.Web.UI.WebControls.TextBox TextBox2;protected System.Web.UI.WebControls.TextBox Textbox3;protected System.Web.UI.HtmlControls.HtmlForm Form1;private void Page_Load(object sender, System.EventArgs e){// 在此处放置用户代码以初始化页面if(!this.IsPostBack){ArrayList MyList = new ArrayList();MyList.Add("加密");MyList.Add("解密");RadioButtonList1.DataSource = MyList;RadioButtonList1.DataBind();}}#region Web 窗体设计器生成的代码override protected void OnInit(EventArgs e){//// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent();base.OnInit(e);}/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){ this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);this.Load += new System.EventHandler(this.Page_Load);}#endregionprivate void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e){if( RadioButtonList1.SelectedIndex == 0)TextBox2.Text = EncryptText(TextBox1.Text);elseTextbox3.Text = DecryptText(TextBox2.Text);}// 加密public string EncryptText(String strText){return Encrypt(strText, "&%#@?,:*");}//'解密public String DecryptText(String strText){return Decrypt(strText, "&%#@?,:*");}//'加密函数private String Encrypt(String strText, String strEncrKey){Byte[] byKey = {};Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};try{byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));DESCryptoServiceProvider des = new DESCryptoServiceProvider();Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);MemoryStream ms = new MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();return Convert.ToBase64String(ms.ToArray());}catch(Exception ex){return ex.Message;}}//'解密函数private String Decrypt(String strText, String sDecrKey){Byte[] byKey = {};Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};Byte[] inputByteArray = new byte[strText.Length];try{byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));DESCryptoServiceProvider des = new DESCryptoServiceProvider();inputByteArray = Convert.FromBase64String(strText);MemoryStream ms = new MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();System.Text.Encoding encoding = System.Text.Encoding.UTF8;return encoding.GetString(ms.ToArray());}catch(Exception ex){return ex.Message;}}}} <%@ Page language="c#" EnableViewState = "true" Codebehind="EncryptString.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.EncryptString" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><title>一个可逆加密的例子</title><meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"><meta name="CODE_LANGUAGE" content="Visual Basic 7.0"><meta name="vs_defaultClientScript" content="JavaScript"><meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"></HEAD><body><form id="Form1" method="post" runat="server"><p align="center">一个可逆加密的例子<asp:TextBox id="TextBox1" runat="server" Width="96%">http://dotnet.aspx.cc/</asp:TextBox><asp:RadioButtonList id="RadioButtonList1" runat="server" Font-Bold="True" RepeatDirection="Horizontal"AutoPostBack="True"></asp:RadioButtonList><asp:TextBox id="TextBox2" runat="server" Width="96%"></asp:TextBox><asp:TextBox id="Textbox3" runat="server" Width="96%"></asp:TextBox></p></form></body></HTML> out参数的问题 求救一个正则表达式 大家觉得上海众生的服务器如何啊 怎样来判断值改变哦 对于学习C#应从什么地方入手 关于水晶报表走势图 100分,在线等,c#的程序安装问题 ! js代码失效?在本地上或者其他服务器上测试的可以。 前台DropDownList动态取得值 ,后台无法获取SelectedValue 请问winform中的datagrid每列的宽度是否能自动变化,显示全列中所有的数据 this问题指点 用C1FlexGrid控件,如何在FlexGrid里写成树型结构的形式?代码如何写呢?我用的是C#谢谢!请高手指点~~
http://blog.csdn.net/net_lover/archive/2003/05.aspx
用一个 GetHashCode()最简单
LY105有例子吗
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[PasswordString.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes("&%#@?,:*".Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(PasswordString);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
return error.Message;
} }
这里解密时返回值为空
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace eMeng.Exam
{
/// <summary>
/// EncryptString 的摘要说明。
/// </summary>
public class EncryptString : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox Textbox3;
protected System.Web.UI.HtmlControls.HtmlForm Form1;private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
ArrayList MyList = new ArrayList();
MyList.Add("加密");
MyList.Add("解密");
RadioButtonList1.DataSource = MyList;
RadioButtonList1.DataBind();
}
}#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);}
#endregionprivate void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if( RadioButtonList1.SelectedIndex == 0)
TextBox2.Text = EncryptText(TextBox1.Text);
else
Textbox3.Text = DecryptText(TextBox2.Text);
}
// 加密
public string EncryptText(String strText)
{
return Encrypt(strText, "&%#@?,:*");
}//'解密
public String DecryptText(String strText)
{
return Decrypt(strText, "&%#@?,:*");
}
//'加密函数
private String Encrypt(String strText, String strEncrKey)
{
Byte[] byKey = {};
Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}//'解密函数
private String Decrypt(String strText, String sDecrKey)
{
Byte[] byKey = {};
Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
Byte[] inputByteArray = new byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>一个可逆加密的例子</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<p align="center">一个可逆加密的例子
<asp:TextBox id="TextBox1" runat="server" Width="96%">http://dotnet.aspx.cc/</asp:TextBox>
<asp:RadioButtonList id="RadioButtonList1" runat="server" Font-Bold="True" RepeatDirection="Horizontal"
AutoPostBack="True"></asp:RadioButtonList>
<asp:TextBox id="TextBox2" runat="server" Width="96%"></asp:TextBox>
<asp:TextBox id="Textbox3" runat="server" Width="96%"></asp:TextBox>
</p>
</form>
</body>
</HTML>