查看: 2047|回复: 2

[文章教程] 常用加密解密算法技术经典ASP终结版(天天asp家园首发)

[复制链接]
ttasp 发表于 2019-4-4 11:39:25 | 显示全部楼层 |阅读模式
1.ESCAPE加密与解密(支持中文);

  1. <%
  2. dim str,str2,str3
  3. str = "天天asp"
  4. str2 = escape(str) '加密
  5. str3 = UnEscape(str2) '解密

  6. Response.Write("原字符:"&str& "<br />")  
  7. Response.Write("加密后:" &str2&"<br />")
  8. Response.Write("解密后:" &str3&"<br />")
  9. %>
复制代码


2.BASE64编码解码(支持中文)
  1. <%
  2. Function Base64Encode(ByVal Str)
  3.         dim Xml, Stream, Node, Encode
  4.         Set Xml = Server.CreateObject("Msxml2.DOMDocument")
  5.         Set Stream = Server.CreateObject("ADODB.Stream")
  6.         Set Node = Xml.CreateElement("TmpNode")
  7.         Node.DataType = "bin.base64"
  8.         Stream.Charset = "UTF-8"
  9.         Stream.Type = 2 '0 = AdStateClosed/1 = AdStateOpen
  10.         Stream.Open()
  11.         Stream.WriteText(Str)
  12.         Stream.Position = 0
  13.         Stream.Type = 1 '1=adTypeBinary/2=adTypeText
  14.         Node.NodeTypedValue = Stream.Read(-1) '-1 = AdReadAll
  15.         Stream.Close()
  16.         Encode = Replace(Node.Text, Vblf, "")
  17.         Encode = Replace(Encode, Vbcr, "")
  18.         Base64Encode = Replace(Encode,"77u/","") 'base64编码后,比起.net多了77u/
  19.         Set Node = Nothing
  20.         Set Stream = Nothing
  21.         Set Xml = Nothing
  22. End Function

  23. Function Base64Decode(ByVal Str)
  24.         Dim Val
  25.         With Server.CreateObject("Msxml2.DOMDocument").CreateElement("TmpNode")
  26.                 .DataType = "bin.base64"
  27.                 .Text = Str
  28.                 Val = .NodeTypedValue
  29.         End With
  30.         With Server.CreateObject("ADODB.Stream")
  31.                 .Type = 1
  32.                 .Open
  33.                 .Write Val
  34.                 .Position = 0
  35.                 .Type = 2
  36.                 .Charset = "UTF-8"
  37.                 Base64Decode = .ReadText(-1)
  38.         End With
  39. End Function

  40. dim str,encode,newline
  41. newline="<br>"
  42. str= "asp学习交流论坛,天天ASP家园!"

  43. encode = base64Encode(str)
  44. response.write "加密前为:" & str & newline
  45. response.write "加密后为:" & encode & newline
  46. response.write "解密后为:" & base64Decode(encode) & newline
  47. %>
复制代码


下面部分代码采用了.NET COM Interop技术,所以要求系统必须有安装.NET Framework 1.1。【默认Window 2003下有预装.NET Framework 1.1,其他低版本OS默认不包含该组件,意味着可能部分XP用户如果在没有安装.NET Framework 1.1的情况下,可能会无法支持下面代码。如果有安装最低.NET Framework 1.1,那么下面的asp代码将能很好的运行在 Windows 98, ME, NT 4.0, 2000, XP, Server 2003, Vista, and Server 2008 以上 。。。等Windows系统下.

3.MD5加密(支持中文和Base64,Hex)

QQ截图20190405132541.jpg

md5加密.rar (873 Bytes, 下载次数: 3, 售价: 1 个ASP币)

4.SHA1加密(支持中文和Base64,Hex)
sha1加密.rar (752 Bytes, 下载次数: 1, 售价: 1 个ASP币)

QQ截图20190405132541.jpg

5.SHA256加密(支持中文和Base64,Hex)

sha256

sha256


sha256.rar (749 Bytes, 下载次数: 5, 售价: 1 个ASP币)



  1. 经典ASP常用加密算法终结版
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>经典ASP常用加密算法终结版</title>
  6. </head>
  7. <body>
  8. <%
  9. Function Hash(HashType, Target)
  10. On Error Resume Next

  11. Dim PlainText

  12. If IsArray(Target) = True Then PlainText = Target(0) Else PlainText = Target End If

  13. With CreateObject("ADODB.Stream")
  14. .Open
  15. .CharSet = "Windows-1252"
  16. .WriteText PlainText
  17. .Position = 0
  18. .CharSet = "UTF-8"
  19. PlainText = .ReadText
  20. .Close
  21. End With

  22. Set UTF8Encoding = CreateObject("System.Text.UTF8Encoding")
  23. Dim PlainTextToBytes, BytesToHashedBytes, HashedBytesToHex

  24. PlainTextToBytes = UTF8Encoding.GetBytes_4(PlainText)

  25. Select Case HashType
  26. Case "md5": Set Cryptography = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider") '< 64 (collisions found)
  27. Case "ripemd160": Set Cryptography = CreateObject("System.Security.Cryptography.RIPEMD160Managed")
  28. Case "sha1": Set Cryptography = CreateObject("System.Security.Cryptography.SHA1Managed") '< 80 (collision found)
  29. Case "sha256": Set Cryptography = CreateObject("System.Security.Cryptography.SHA256Managed")
  30. Case "sha384": Set Cryptography = CreateObject("System.Security.Cryptography.SHA384Managed")
  31. Case "sha512": Set Cryptography = CreateObject("System.Security.Cryptography.SHA512Managed")
  32. Case "md5HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACMD5")
  33. Case "ripemd160HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACRIPEMD160")
  34. Case "sha1HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA1")
  35. Case "sha256HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA256")
  36. Case "sha384HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA384")
  37. Case "sha512HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA512")
  38. End Select

  39. Cryptography.Initialize()

  40. If IsArray(Target) = True Then Cryptography.Key = UTF8Encoding.GetBytes_4(Target(1))

  41. BytesToHashedBytes = Cryptography.ComputeHash_2((PlainTextToBytes))

  42. For x = 1 To LenB(BytesToHashedBytes)
  43. HashedBytesToHex = HashedBytesToHex & Right("0" & Hex(AscB(MidB(BytesToHashedBytes, x, 1))), 2)
  44. Next

  45. If Err.Number <> 0 Then Response.Write(Err.Description) Else Hash = LCase(HashedBytesToHex)

  46. On Error GoTo 0
  47. End Function

  48. Response.Write(Hash("sha256HMAC","bbs.ttasp.com"))

  49. %>

  50. </body>
  51. </html>
复制代码


更新基本完成
小叶白龙 发表于 2019-4-8 11:41:56 | 显示全部楼层
为啥我的base64加密的和你们的不一样呢
你们的代码
加密前为:你好
加密后为:5L2g5aW9
解密后为:你好

但我的为啥是
加密前为:你好
加密后为:JiMyMDMyMDsmIzIyOTA5Ow==
解密后为:你好

  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  2. <%
  3.         session.codepage=65001
  4.         response.charset="utf-8"
  5.         server.scripttimeout=999999%>
  6. <%
  7.      'OPTION EXPLICIT
  8.      const BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  9.      dim nl
  10.      ' zero based arrays
  11.      dim Base64EncMap(63)
  12.      dim Base64DecMap(127)

  13.      ' must be called before using anything else
  14.      PUBLIC SUB initCodecs()
  15.           ' init vars
  16.           nl = "<P>" & chr(13) & chr(10)
  17.           ' setup base 64
  18.           dim max, idx
  19.              max = len(BASE_64_MAP_INIT)
  20.           for idx = 0 to max - 1
  21.                ' one based string
  22.                Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
  23.           next
  24.           for idx = 0 to max - 1
  25.                Base64DecMap(ASC(Base64EncMap(idx))) = idx
  26.           next
  27.      END SUB

  28.      ' encode base 64 encoded string
  29.      PUBLIC FUNCTION base64Encode(plain)
  30.           if len(plain) = 0 then
  31.                base64Encode = ""
  32.                exit function
  33.           end if

  34.           dim ret, ndx, by3, first, second, third
  35.           by3 = (len(plain) \ 3) * 3
  36.           ndx = 1
  37.           do while ndx <= by3
  38.                first  = asc(mid(plain, ndx+0, 1))
  39.                second = asc(mid(plain, ndx+1, 1))
  40.                third  = asc(mid(plain, ndx+2, 1))
  41.                ret = ret & Base64EncMap(  (first \ 4) AND 63 )
  42.                ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
  43.                ret = ret & Base64EncMap( ((second * 4) AND 60) + ((third \ 64) AND 3 ) )
  44.                ret = ret & Base64EncMap( third AND 63)
  45.                ndx = ndx + 3
  46.           loop
  47.           ' check for stragglers
  48.           if by3 < len(plain) then
  49.                first  = asc(mid(plain, ndx+0, 1))
  50.                ret = ret & Base64EncMap(  (first \ 4) AND 63 )
  51.                if (len(plain) MOD 3 ) = 2 then
  52.                     second = asc(mid(plain, ndx+1, 1))
  53.                     ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
  54.                     ret = ret & Base64EncMap( ((second * 4) AND 60) )
  55.                else
  56.                     ret = ret & Base64EncMap( (first * 16) AND 48)
  57.                     ret = ret & "="
  58.                end if
  59.                ret = ret & "="
  60.           end if

  61.           base64Encode = ret
  62.      END FUNCTION

  63.      ' decode base 64 encoded string
  64.      PUBLIC FUNCTION base64Decode(scrambled)

  65.           if len(scrambled) = 0 then
  66.                base64Decode = ""
  67.                exit function
  68.           end if

  69.           ' ignore padding
  70.           dim realLen
  71.           realLen = len(scrambled)
  72.           do while mid(scrambled, realLen, 1) = "="
  73.                realLen = realLen - 1
  74.           loop
  75.           dim ret, ndx, by4, first, second, third, fourth
  76.           ret = ""
  77.           by4 = (realLen \ 4) * 4
  78.           ndx = 1
  79.           do while ndx <= by4
  80.                first  = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
  81.                second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
  82.                third  = Base64DecMap(asc(mid(scrambled, ndx+2, 1)))
  83.                fourth = Base64DecMap(asc(mid(scrambled, ndx+3, 1)))
  84.                ret = ret & chr( ((first * 4) AND 255) +   ((second \ 16) AND 3))
  85.                ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15) )
  86.                ret = ret & chr( ((third * 64) AND 255) +  (fourth AND 63) )
  87.                ndx = ndx + 4
  88.           loop
  89.           ' check for stragglers, will be 2 or 3 characters
  90.           if ndx < realLen then
  91.                first  = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
  92.                second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
  93.                ret = ret & chr( ((first * 4) AND 255) +   ((second \ 16) AND 3))
  94.                if realLen MOD 4 = 3 then
  95.                     third = Base64DecMap(asc(mid(scrambled,ndx+2,1)))
  96.                     ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15) )
  97.                end if
  98.           end if

  99.           base64Decode = ret
  100.      END FUNCTION
  101. Function UrlDecode(encodestr)

  102. newstr=""
  103. havechar=false
  104. lastchar=""
  105. for i=1 to len(encodestr)
  106. char_c=mid(encodestr,i,1)
  107. if char_c="+" then
  108. newstr=newstr & " "
  109. elseif char_c="%" then
  110. next_1_c=mid(encodestr,i+1,2)
  111. next_1_num=cint("&h" & next_1_c)

  112. if havechar then
  113. havechar=false
  114. newstr=newstr & chr(cint("&h" & lastchar & next_1_c))
  115. else
  116. if abs(next_1_num)<=127 then
  117. newstr=newstr & chr(next_1_num)
  118. else
  119. havechar=true
  120. lastchar=next_1_c
  121. end if
  122. end if
  123. i=i+2
  124. else
  125. newstr=newstr & char_c
  126. end if

  127. next
  128. UrlDecode=newstr
  129. End Function



  130.    call initCodecs
  131.         inp = "你好"
  132.     inp=server.htmlEncode(inp)
  133.     encode = base64Encode(inp)
  134.     decode= base64Decode(encode)

  135.     response.write "urlE = " & inp & nl
  136.     response.write "Encoded value = " & encode & nl
  137.     response.write "Decoded value = " & UrlDecode(decode) & nl
  138.         %>
复制代码


好像和编码有关
xuanxiao 发表于 2019-7-7 14:34:24 | 显示全部楼层
小叶白龙 发表于 2019-4-8 11:41
为啥我的base64加密的和你们的不一样呢
你们的代码
加密前为:你好

统一下编码,没问题的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号