各位大虾,我要让用户可以找回密码,并且要给他密码加密,可是怎样使用可逆的加密?
解决方案 »
- 如何在cs中添加或改变属性
- 读取文本文件?
- 远程webservice调用
- 做留言本时如何吧用户提交上来的HTML标签用正则表达试过滤掉
- 如何在Label中显示格式为yyyy-mm-dd的时间格式?
- 收集到的一写常用正则表达式,给大家参考
- 听说CSDN有对计算机图书的书评,请问在哪呀?
- 怎样调用出选项卡工具?
- 请问怎样做到点击 DataGrid 的某一行能够让它高亮显示?
- ASP.NET中邮件发送问题
- 求首字母是字母,数字和字母混和,并且必须有数字的正则表达式
- 求正则表达式,抓取下边红色标记内容,并求出<a></a>间内容及href内容,如:"/Info/2007-10/XTW20071013184427.htm"和<a></a>间内容,谢谢!
函数名:encode
参数: string as_code
返回:string
String ls_rtncode,ls_code1,ls_code2,ls_temp
Long ll_len,ll_i,ll_j
Integer li_code1,li_code2
ll_len = Len(as_code)
IF ll_len <= 0 THEN Return ""
ls_rtncode = ""
ll_j = 1
FOR ll_i = 1 TO ll_len
li_code1 = ASC(Mid(as_code,ll_i,1))
li_code2 = ASC(Mid(gs_key_para,ll_j,1))
li_code1 += li_code2
DO WHILE li_code1 > 127
if li_code1 > 127 then
li_code1 = li_code1 - 127
end if
LOOP
ls_temp = char(li_code1)
ls_rtncode += ls_temp
ll_j ++
if ll_j > len(gs_key_para) then ll_j = 1
next
return ls_rtncode
解密 :
函数名:decode
参数: string as_code
返回:string
String ls_rtncode,ls_code1,ls_code2,ls_temp
Long ll_len,ll_i,ll_j
Integer li_code1,li_code2
ll_len = Len(as_code)
IF ll_len <= 0 THEN Return ""
ls_rtncode = ""
ll_j = 1
FOR ll_i = 1 TO ll_len
li_code1 = ASC(Mid(as_code,ll_i,1))
li_code2 = ASC(Mid(gs_key_para,ll_j,1))
li_code1 -= li_code2
DO WHILE li_code1 < 0
if li_code1 < 0 then
li_code1 = li_code1 + 127
end if
LOOP
ls_temp = char(li_code1)
ls_rtncode += ls_temp
ll_j ++
if ll_j > len(gs_key_para) then ll_j = 1
next
return ls_rtncode
全局变量 gs_key_para="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/// 3DES加密
/// </summary>
/// <param name="Value">待加密字符串</param>
/// <param name="sKey">密钥</param>
/// <param name="sIV">矢量</param>
/// <returns>加密后字符串</returns>
private static string encryptString(string Value, string sKey, string sIV)
{
//构造对称算法
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
//指定加密的运算模式
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
//获取或设置加密算法的填充模式
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
} /// <summary>
/// 3DES解密
/// </summary>
/// <param name="Value">待解密字符串</param>
/// <param name="sKey">密钥</param>
/// <param name="sIV">矢量</param>
/// <returns>解密后字符串</returns>
private static string decryptString(string Value, string sKey, string sIV)
{
//构造对称算法
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Web.Security; namespace EncryptingDecrypting
{
/// <summary >
/// SymmCrypto 的摘要说明。
/// SymmCrypto类实现.NET框架下的加密和解密服务。
/// 作者:badboy
/// </summary >
public class SymmCrypto
{
public enum SymmProvEnum : int
{
DES, RC2, Rijndael
}
private SymmetricAlgorithm mobjCryptoService;
/// <res >
/// 使用.Net SymmetricAlgorithm 类的构造器.
/// </res >
public SymmCrypto(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.RC2:
mobjCryptoService = new RC2CryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}
/// <res >
/// 使用自定义SymmetricAlgorithm类的构造器.
/// </res >
public SymmCrypto(SymmetricAlgorithm ServiceProvider)
{
mobjCryptoService = ServiceProvider;
}
/// <res >
/// Depending on the legal key size limitations of
/// a specific CryptoService provider and length of
/// the private key provided, padding the secret key
/// with space character to meet the legal size of the algorithm.
/// </res >
private byte[] GetLegalKey(string Key)
{
string sTemp;
if (mobjCryptoService.LegalKeySizes.Length > 0)
{
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
// key sizes are in bits
while (Key.Length * 8 > moreSize)
{
lessSize = moreSize;
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp = Key.PadRight(moreSize / 8, " ");
}
else
sTemp = Key;
// convert the secret key to byte array
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
public string Encrypting(string Source, string Key)
{
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
// create a MemoryStream so that the process can be done without I/O files
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);
// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
// create an Encryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
// create Crypto Stream that transforms a stream using the encryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
// write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
// get the output and trim the "\0 " bytes
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut[i] == 0)
break;
// convert into Base64 so that the result can be used in xml
return System.Convert.ToBase64String(bytOut, 0, i);
}
public string Decrypting(string Source, string Key)
{
// convert from Base64 to binary
byte[] bytIn = System.Convert.FromBase64String(Source);
// create a MemoryStream with the input
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
byte[] bytKey = GetLegalKey(Key);
// set the private key
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
// create a Decryptor from the Provider Service instance
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
// create Crypto Stream that transforms a stream using the decryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
// read out the result from the Crypto Stream
System.IO.StreamReader sr = new System.IO.StreamReader( cs );
return sr.ReadToEnd();
}
}
}
使用方法:
加密:
SymmCrypto de = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
string strValue = de.Encrypting(writeValue,strkey);
//strkey为密钥,writeValue是要加密的字符串.
解密:
SymmCrypto de = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
string retValue = de.Decrypting(strValue,strkey);
//strkey为密钥,strValue是要解密的字符串.