最近一直在做通行证网站,因为大部分子站是用asp写的,而我是用asp.net(C#)来写的通行证网站。通行证网站与各个子站都是通过 url方式传送数据,因为涉及到用户名、密码等保密数据,所以就需要对传输的数据进行加密,这样麻烦就来了。本来我用的是Des加密方式,而C#实现起来也并不困难,但是asp实现起来就麻烦了,asp只能对英文字母以及数字进行des加密解密,但是涉及到汉字就无能为力了。我对asp已经忘记的差不多了,所以也就没有去深钻下去。Des无法实现,那总要有一种加密的方式吧,呵呵,咱就用最简单的加密方式,按位异或。

  C#实现起来,没什么好说的,简单的很。代码粘下来。
引用
public class Des
{
    public Des()
    {
        //
    }
    
    public string EncryStrHex(string aText, string aKey, string aCode)
    {
        StringBuilder sb = new StringBuilder("");
        byte[] bsText = Encoding.GetEncoding(aCode).GetBytes(aText);
        byte[] bsKey = Encoding.GetEncoding(aCode).GetBytes(aKey);
        int j = 0;
        
        foreach(byte b in bsText)
        {
            if(j == bsKey.Length) j = 0;
            sb.AppendFormat("{0:X2}", b ^ bsKey[j]);
            j++;
        }
        
        return sb.ToString();
    }
    
    public string DecryStrHex(string aText, string aKey, string aCode)
    {
        byte[] bs = new byte[aText.Length / 2];
        byte[] bsKey = Encoding.GetEncoding(aCode).GetBytes(aKey);
        byte b;
        int j = 0;
        
        for(int i = 0; i < aText.Length / 2; i++)
        {
            if(j == bsKey.Length) j =0;
            b = (byte)Convert.ToInt16(aText.Substring(i * 2, 2), 16);
            bs[i] = (byte)(b ^ bsKey[j]);
            j++;
        }
        
        return Encoding.GetEncoding(aCode).GetString(bs, 0, bs.Length);
    }
}

  对于C#代码是够简单的,而asp实现起来就虽然代码也并不复杂,但是在调试的过程中是颇费周折的。首先是要注意Chr()和ChrW()的区别,再次就是对汉字的特殊处理。不啰嗦了,asp主体代码如下。
引用
<%
'加密函数
Function EncryStrHex(sText, sKey)
    Dim i, s, ok, s1, s2
    Dim j, k, l
    l = Len(sKey)
    j = 0
    For i = 1 To Len(sText)
        j = j + 1
        If j > l Then j = 1
        s = Hex(Asc(Mid(sText, i, 1)))
        If Len(s) > 2 Then
            k = Asc(Mid(sKey, j, 1))
            s1 = chn10(Left(s, 2))
            s2 = chn10(Right(s, 2))
            s = FixHex(Hex(s1 Xor k))
            j = j + 1
            k = Asc(Mid(sKey, j, 1))
            s = s & FixHex(Hex(s2 Xor k))
        Else
            k = Asc(Mid(sKey, j, 1))
            s = FixHex(Hex(chn10(s) Xor k))
        End If
        ok = ok & s
    Next
    myEn = ok
End Function

'解密函数
Function DecryStrHex(sText, sKey)
    Dim i, s, ok
    Dim j, k, l, m, t, t2
    l = Len(sKey)
    j = 0
    For i = 1 To Len(sText) / 2
        j = j + 1
        If j > l Then j = 1
        s = chn10(Mid(sText, i * 2 - 1, 2))
        k = Asc(Mid(sKey, j, 1))
        t = s Xor k
        If t > 126 Then
            i = i + 1
            j = j + 1
            If j > l Then j = 1
            s = chn10(Mid(sText, i * 2 - 1, 2))
            k = Asc(Mid(sKey, j, 1))
            t2 = s Xor k
            t = t * 256 + t2
        End If
        s = Chr(t)
        ok = ok & s
    Next
    myDe = ok
End Function
%>

  希望这些东西对于大家能有一点儿用处。

  顺便说一句,代码里面类的名字用了Des,实际上这个和Des一点儿关系都没有,只是为了兼容已经写好了的网站的代码。因为我刚开始是用Des 做的加密,整个网站里面都是调用Des类,后来不用Des加密了,但是改网站的代码就太麻烦了,而且如果以后再改回Des加密,难道再改网站代码?所以,我的新的加密类就沿用Des这个类名,为的就是不用修改网站的代码。



这里是源代码:

MyDes.cs

MyDes.asp
Tags: , , ,
纯技术分类 » PHP/ASP.NET | 评论(0) | 引用(0) | 阅读(1502)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]