字符串加密解密函数

 

ContractedBlock.gif ExpandedBlockStart.gif          Base64加密 #region Base64加密
InBlock.gif        [WebMethod]
InBlock.gif        
public string Base64Encrypt(string pToEncrypt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(pToEncrypt));
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
Base64编码 #region Base64编码
InBlock.gif        [WebMethod]
InBlock.gif        
public string Base64Decrypt(string pToDeCrypt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return System.Text.Encoding.Default.GetString(Convert.FromBase64String(pToDeCrypt));
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
MD5加密 #region MD5加密
InBlock.gif        [WebMethod]
InBlock.gif        
public string Encrypt_MD5_Standard(string strpwd)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MD5 MD5
=new MD5CryptoServiceProvider();
InBlock.gif            
byte[] datSource = System.Text.Encoding.Default.GetBytes(strpwd);
InBlock.gif            
byte[] newSource = MD5.ComputeHash(datSource);
InBlock.gif            
string byte2String = null;
InBlock.gif            
for (int i=0; i<newSource.Length; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string thisByte=newSource[i].ToString("x");
InBlock.gif                
if(thisByte.Length==1) thisByte="0"+thisByte;
InBlock.gif                byte2String 
+=  thisByte;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return byte2String;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
Passport 加密函数 #region Passport 加密函数
InBlock.gif        [WebMethod]
InBlock.gif        
public string  Passport_Encrypt(string txt, string key) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
// 使用随机数发生器产生 0~32000 的值并 MD5()
InBlock.gif
            string encrypt_key = Encrypt_MD5_Standard(GetRandomNumber(032000).ToString());
InBlock.gif            
// 变量初始化
InBlock.gif
            int ctr = 0;
InBlock.gif            
string tmp = "";
InBlock.gif
InBlock.gif            
// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
InBlock.gif
            for (int i = 0; i < txt.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
InBlock.gif
                ctr = ctr == encrypt_key.Length ? 0 : ctr;
InBlock.gif                
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
InBlock.gif                
// 与 $encrypt_key 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
InBlock.gif
                tmp +=  encrypt_key[ctr].ToString() + ((char)(txt[i] ^ encrypt_key[ctr++])).ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
// 返回结果,结果为 passport_key() 函数返回值的 base65 编码结果
InBlock.gif
            return Base64Encrypt(Passport_Key(tmp, key));
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
Passport 解密函数 #region Passport 解密函数
InBlock.gif        [WebMethod]
InBlock.gif        
public string  Passport_Decrypt(string txt, string key) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// $txt 的结果为加密后的字串经过 base64 解码,然后与私有密匙一起,
InBlock.gif            
// 经过 passport_key() 函数处理后的返回值
InBlock.gif
            txt = Passport_Key(Base64Decrypt(txt), key);
InBlock.gif
InBlock.gif            
// 变量初始化
InBlock.gif
            string tmp = "";
InBlock.gif
InBlock.gif            
// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
InBlock.gif
            for (int i = 0; i < txt.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
InBlock.gif                
// 与 $txt 的第 $i + 1 位取异或。然后 $i = $i + 1
InBlock.gif
                tmp += ((char)(txt[i] ^ txt[++i])).ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
// 返回 $tmp 的值作为结果
InBlock.gif
            return tmp;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
Passport 密匙处理函数 #region Passport 密匙处理函数
InBlock.gif        [WebMethod]
InBlock.gif        
public string Passport_Key(string txt, string encrypt_key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 将 $encrypt_key 赋为 $encrypt_key 经 md5() 后的值
InBlock.gif
            encrypt_key = Encrypt_MD5_Standard(encrypt_key);
InBlock.gif            
// 变量初始化
InBlock.gif
            int ctr = 0;
InBlock.gif            
string tmp = "";
InBlock.gif            
InBlock.gif            
// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
InBlock.gif
            for (int i = 0; i < txt.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 如果 $ctr = $encrypt_key 的长度,则 $ctr 清零
InBlock.gif
                ctr = ctr == encrypt_key.Length ? 0 : ctr;
InBlock.gif                
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
InBlock.gif                
// 与 $encrypt_key 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
InBlock.gif
                tmp +=   ((char)(txt[i] ^ encrypt_key[ctr++])).ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return tmp;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion

EG:
string a = "fanrongsheng";需要加密的字符串
string key = "loveaspnet";私有密码
//strPvt加密后的字符串
string strPvt = Passport_Encrypt(a,key);
//解密字符串
string strSour = Passport_Decrypt(strPvt,key);
strSour=a
欢迎大家评论,指点。。。

转载于:https://www.cnblogs.com/fanrsh/archive/2006/06/05/417691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值