欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP BASE64加解密(亲测可用)

程序员文章站 2022-07-02 18:19:11
核心代码: <% ' option explicit const base_64_map_init = "abcdefghijklmnopqr...

核心代码:

<%
  ' option explicit
   const base_64_map_init = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"
   dim newline
   dim base64encmap(63)
   dim base64decmap(127)
   '初始化函数
   public sub initcodecs()
     ' 初始化变量
     newline = "<p>" & chr(13) & chr(10)
     dim max, idx
       max = len(base_64_map_init)
     for idx = 0 to max - 1
        base64encmap(idx) = mid(base_64_map_init, idx + 1, 1)
     next
     for idx = 0 to max - 1
        base64decmap(asc(base64encmap(idx))) = idx
     next
   end sub
   'base64加密函数
   public function base64encode(plain)
     if len(plain) = 0 then
        base64encode = ""
        exit function
     end if
     dim ret, ndx, by3, first, second, third
     by3 = (len(plain) \ 3) * 3
     ndx = 1
     do while ndx <= by3
        first = asc(mid(plain, ndx+0, 1))
        second = asc(mid(plain, ndx+1, 1))
        third = asc(mid(plain, ndx+2, 1))
        ret = ret & base64encmap( (first \ 4) and 63 )
        ret = ret & base64encmap( ((first * 16) and 48) + ((second \ 16) and 15 ) )
        ret = ret & base64encmap( ((second * 4) and 60) + ((third \ 64) and 3 ) )
        ret = ret & base64encmap( third and 63)
        ndx = ndx + 3
     loop
     if by3 < len(plain) then
        first = asc(mid(plain, ndx+0, 1))
        ret = ret & base64encmap( (first \ 4) and 63 )
        if (len(plain) mod 3 ) = 2 then
          second = asc(mid(plain, ndx+1, 1))
          ret = ret & base64encmap( ((first * 16) and 48) + ((second \ 16) and 15 ) )
          ret = ret & base64encmap( ((second * 4) and 60) )
        else
          ret = ret & base64encmap( (first * 16) and 48)
          ret = ret '& "="
        end if
        ret = ret '& "="
     end if
     base64encode = ret
   end function
   'base64解密函数
   public function base64decode(scrambled)
     if len(scrambled) = 0 then
        base64decode = ""
        exit function
     end if
     dim reallen
     reallen = len(scrambled)
     do while mid(scrambled, reallen, 1) = "="
        reallen = reallen - 1
     loop
     dim ret, ndx, by4, first, second, third, fourth
     ret = ""
     by4 = (reallen \ 4) * 4
     ndx = 1
     do while ndx <= by4
        first = base64decmap(asc(mid(scrambled, ndx+0, 1)))
        second = base64decmap(asc(mid(scrambled, ndx+1, 1)))
        third = base64decmap(asc(mid(scrambled, ndx+2, 1)))
        fourth = base64decmap(asc(mid(scrambled, ndx+3, 1)))
        ret = ret & chr( ((first * 4) and 255) +  ((second \ 16) and 3))
        ret = ret & chr( ((second * 16) and 255) + ((third \ 4) and 15))
        ret = ret & chr( ((third * 64) and 255) + (fourth and 63))
        ndx = ndx + 4
     loop
     if ndx < reallen then
        first = base64decmap(asc(mid(scrambled, ndx+0, 1)))
        second = base64decmap(asc(mid(scrambled, ndx+1, 1)))
        ret = ret & chr( ((first * 4) and 255) +  ((second \ 16) and 3))
        if reallen mod 4 = 3 then
          third = base64decmap(asc(mid(scrambled,ndx+2,1)))
          ret = ret & chr( ((second * 16) and 255) + ((third \ 4) and 15))
        end if
     end if
     base64decode = ret
   end function

%>

使用方法:

' 初始化
 call initcodecs
response.write(base64encode("之我要加密的字符串"))
response.write(base64decode("bwfycziwmtawmjiw0"))