How to use ASP for base64 encryption

Source: Internet
Author: User
Tags base64 exit chr
<%
OPTION EXPLICIT
Const BASE_64_MAP_INIT =
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim nl
' Zero based arrays
Dim Base64encmap (63)
Dim Base64decmap (127)

' must be called before using anything else
Public SUB Initcodecs ()
' Init VARs
NL = "<P>" & Chr (+) & Chr (10)
' Setup Base 64
Dim max, IDX
max = Len (base_64_map_init)
For idx = 0 to Max-1
' One based string
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

' Encode base encoded string
Public FUNCTION Base64Encode (plain)

If Len (plain) = 0 Then
Base64Encode = ""
Exit function
End If

Dim ret, Ndx, By3, second, third
By3 = (len (plain) \ 3) * 3
NDX = 1
Do While Ndx <= by3
A = ASC (Mid (Plain, ndx+0, 1))
Second = ASC (Mid (Plain, ndx+1, 1))
Third = ASC (Mid (Plain, ndx+2, 1))
RET = ret & Base64encmap ((4) and 63)
RET = ret & Base64encmap (((A) ((a) +) + ((second \ 16)
and 15))
RET = ret & Base64encmap ((Second * 4) and) + ((third \ 64)
and 3))
RET = ret & Base64encmap (third and 63)
NDX = ndx + 3
Loop
' Check for stragglers
If By3 < Len (plain) Then
A = ASC (Mid (Plain, ndx+0, 1))
RET = ret & Base64encmap ((4) and 63)
if (Len (plain) MOD 3) = 2 Then
Second = ASC (Mid (Plain, ndx+1, 1))
RET = ret & Base64encmap (((a) and 48) +
((Second \
) and 15))
RET = ret & Base64encmap ((Second * 4) and 60))
Else
RET = ret & Base64encmap ((a) and 48)
RET = ret & "="
End If
RET = ret & "="
End If

Base64Encode = RET
End FUNCTION

' Decode base encoded string
Public FUNCTION Base64decode (scrambled)

If Len (scrambled) = 0 Then
Base64decode = ""
Exit function
End If

' Ignore padding
Dim Reallen
Reallen = Len (scrambled)
Do while mid (scrambled, Reallen, 1) = "="
Reallen = realLen-1
Loop
Dim ret, Ndx, By4, A, second, third, fourth
ret = ""
By4 = (reallen \ 4) * 4
NDX = 1
Do While Ndx <= by4
i = BASE64DECMAP (ASC (Scrambled, ndx+0, 1))
Second = BASE64DECMAP (ASC (Scrambled, ndx+1, 1))
Third = Base64decmap (ASC (Scrambled, ndx+2, 1))
Fourth = BASE64DECMAP (ASC (Scrambled, ndx+3, 1))
RET = ret & Chr (((4) and 255) + ((second) and 3)
)
RET = ret & Chr ((second *) and 255) + ((third \ 4) and
15))
RET = ret & Chr ((third *) and 255) + (fourth and 63))
NDX = ndx + 4
Loop
' Check for stragglers, would be 2 or 3 characters
If Ndx < Reallen Then
i = BASE64DECMAP (ASC (Scrambled, ndx+0, 1))
Second = BASE64DECMAP (ASC (Scrambled, ndx+1, 1))
RET = ret & Chr (((4) and 255) + ((second) and 3)
)
If Reallen MOD 4 = 3 Then
Third = Base64decmap (ASC (mid scrambled,ndx+2,1))
RET = ret & Chr ((second *) and 255) + ((third \ 4) and
15))
End If
End If

Base64decode = RET
End FUNCTION

' Initialize
Call Initcodecs

' Testing code
' Dim inp, Encode
' INP = Request. QueryString ("Input")
' Encode = Base64Encode (INP)
' Response.Write ' encoded value = ' & Encode & NL
' Response.Write ' decoded value = ' & Base64decode (encode) & NL
%>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.