大家自己写的可逆的加密函数,都用什么方法啊? 用DES和AES吧,C#有现成的啊,如果要自己写.,如果安全性不高,可以写些简单的,比如TEA了,背包公钥什么的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 举个例子吧string pwd="123456";//加密MessageBox.Show("加密后的字符串");//解密MessageBox.Show("解密后的字符串"); 文件名:Security.CSusing System;using System.Security.Cryptography;using System.IO;using System.Text;namespace EIP.Framework{ /// /// Security 的摘要说明。 /// Security类实现.NET框架下的加密和解密。 /// CopyRight [email protected]@[email protected] /// public class Security { string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key string _PassWordKey = "hgfedcba"; //PassWord加密Key public Security() { // // TODO: 在此处添加构造函数逻辑 // } /// /// 加密URL传输的字符串 /// /// /// public string EncryptQueryString(string QueryString) { return Encrypt(QueryString,_QueryStringKey); } /// /// 解密URL传输的字符串 /// /// /// public string DecryptQueryString(string QueryString) { return Decrypt(QueryString,_QueryStringKey); } /// /// 加密帐号口令 /// /// /// public string EncryptPassWord(string PassWord) { return Encrypt(PassWord,_PassWordKey); } /// /// 解密帐号口令 /// /// /// public string DecryptPassWord(string PassWord) { return Decrypt(PassWord,_PassWordKey); } /// /// DEC 加密过程 /// /// /// /// public string Encrypt(string pToEncrypt,string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte数组中 byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); //byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 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(); } /// /// DEC 解密过程 /// /// /// /// public string Decrypt(string pToDecrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 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; } 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(); StringBuilder ret = new StringBuilder(); //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 return System.Text.Encoding.Default.GetString(ms.ToArray()); } /// /// 检查己加密的字符串是否与原文相同 /// /// /// /// /// public bool ValidateString(string EnString, string FoString, int Mode) { switch (Mode) { default: case 1: if (Decrypt(EnString,_QueryStringKey) == FoString.ToString()) { return true; } else { return false; } case 2: if (Decrypt(EnString,_PassWordKey) == FoString.ToString()) { return true; } else { return false; } } } }}类中URL及帐号加密使用了不同的KEY。调用URL加密过程如下:EIP.Framework.Security objSecurity = new EIP.Framework.Security();objSecurity.EncryptQueryString(''待加密的字符串'');解密:objSecurity.DecryptQueryString(''传递过来的参数);源文档 <http://blog.csdn.net/kangsoft/archive/2004/09/06/96225.aspx> 再怎么加密都没用,人家只要反编译这个dll,就能看到源代码也自然能看到密码,所以必须从根本上解决这个问题 如何在OpenFileDialog控件中同时显示多种格式的图片文件 owc11 的问题 怎么序列化类成字符串并返回 关于附件的上传下载(winform). System.TypeLoadException 请问如果希望在编写的一个windows服务开始运行的时候打开一个窗口该怎样编写? 使用smtp连接邮件服务器发送邮件,为什么会发送重复邮件? C#自己写的delay延时函数 如何得到一个随机文件名? 新手问题:请问一个C#中对象的数组定义问题 winform中的Datagrid应用问题? 在dataset中表里新增了一个列,如何在这个列中显示图片呢?
string pwd="123456";
//加密
MessageBox.Show("加密后的字符串");
//解密
MessageBox.Show("解密后的字符串");
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace EIP.Framework
{
///
/// Security 的摘要说明。
/// Security类实现.NET框架下的加密和解密。
/// CopyRight [email protected]@[email protected]
///
public class Security
{
string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key
string _PassWordKey = "hgfedcba"; //PassWord加密Key
public Security()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///
/// 加密URL传输的字符串
///
///
///
public string EncryptQueryString(string QueryString)
{
return Encrypt(QueryString,_QueryStringKey);
}
///
/// 解密URL传输的字符串
///
///
///
public string DecryptQueryString(string QueryString)
{
return Decrypt(QueryString,_QueryStringKey);
}
///
/// 加密帐号口令
///
///
///
public string EncryptPassWord(string PassWord)
{
return Encrypt(PassWord,_PassWordKey);
}
///
/// 解密帐号口令
///
///
///
public string DecryptPassWord(string PassWord)
{
return Decrypt(PassWord,_PassWordKey);
} ///
/// DEC 加密过程
///
///
///
///
public string Encrypt(string pToEncrypt,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
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();
}
///
/// DEC 解密过程
///
///
///
///
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 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;
} 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();
StringBuilder ret = new StringBuilder(); //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
///
/// 检查己加密的字符串是否与原文相同
///
///
///
///
///
public bool ValidateString(string EnString, string FoString, int Mode)
{
switch (Mode)
{
default:
case 1:
if (Decrypt(EnString,_QueryStringKey) == FoString.ToString())
{
return true;
}
else
{
return false;
}
case 2:
if (Decrypt(EnString,_PassWordKey) == FoString.ToString())
{
return true;
}
else
{
return false;
}
}
}
}
}
类中URL及帐号加密使用了不同的KEY。调用URL加密过程如下:
EIP.Framework.Security objSecurity = new EIP.Framework.Security();
objSecurity.EncryptQueryString(''待加密的字符串'');
解密:objSecurity.DecryptQueryString(''传递过来的参数);源文档 <http://blog.csdn.net/kangsoft/archive/2004/09/06/96225.aspx>