简单的加密方法:XOR
发布时间:2006-10-14 2:57:56   收集提供:gaoqian
  念书的时候使用对数据进行加密的方法最简单的就是异或了,看到有人想要加密算法,就把以前的资料翻了一下,整理了一系列加密的函数,当然简单的加密也是容易破解的,但聊胜于无(记得把密要钥放好了),总比让人一打开数据库就看见密码明码好吧。:-)

<%
'最简单的加密方法:XOR
'----------------------

g_CryptThis = "中国-China"
strFullKeyLen = Len(g_CryptThis)

strFullKey = KeyGen(strFullKeyLen)

Response.Write "<p>原始字符串: " & g_CryptThis & "<p>"
Response.Write "<p>密钥: " & strFullKey  & "<p>"
Response.Write "<p>加密后: " & Server.URLEncode(EnCrypt(g_CryptThis)) & "<p>"
Response.Write "<p>解密后: " & DeCrypt(EnCrypt(g_CryptThis)) & "<p>"

'异或加密
Function EnCrypt(strCryptThis)
   Dim strChar, iKeyChar, iStringChar, i
   for i = 1 to Len(strCryptThis)
      iKeyChar = Asc(mid(strFullKey,i,1))
      iStringChar = Asc(mid(strCryptThis,i,1))
      iCryptChar = iKeyChar Xor iStringChar
      strEncrypted =  strEncrypted & Chr(iCryptChar)
   next
   EnCrypt = strEncrypted
End Function

'异或解密
Function DeCrypt(strEncrypted)
Dim strChar, iKeyChar, iStringChar, i
   for i = 1 to Len(strEncrypted)
      iKeyChar = (Asc(mid(strFullKey,i,1)))
      iStringChar = Asc(mid(strEncrypted,i,1))
      iDeCryptChar = iKeyChar Xor iStringChar
      strDecrypted =  strDecrypted & Chr(iDeCryptChar)
   next
   DeCrypt = strDecrypted
End Function

'产生指定长度的随机密钥
Function KeyGen(strlength)
    Dim i,UB
    Dim Temp
    Dim Poss
    Poss = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Temp = ""

    UB = Len(Poss)
    For i = 1 To strlength
        Randomize
        Temp = Temp & Mid(Poss,Int((UB - 0 + 1) * Rnd + 1),1)
    Next
    KeyGen = Temp
End Function
%>

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50