ASP base64 and Decryption function code _ Application Skills

Source: Internet
Author: User
Tags base64 chr
Copy Code code as follows:

<%

Sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"
Sbase_64_characters = Strunicode2ansi (sbase_64_characters)

Function Strunicodelen (ascontents)
' Calculates the length of the ANSI encoding of a Unicode string
Ascontents1= "a" &ascontents
Len1=len (ASCONTENTS1)
K=0
For I=1 to Len1
ASC1=ASC (Mid (ascontents1,i,1))
If Asc1<0 then ASC1=65536+ASC1
If asc1>255 Then
K=k+2
Else
K=k+1
End If
Next
Strunicodelen=k-1
End Function

Function Strunicode2ansi (ascontents)
' Converts a Unicode-encoded string into an ANSI-encoded string
Strunicode2ansi= ""
Len1=len (ascontents)
For I=1 to Len1
Varchar=mid (ascontents,i,1)
VARASC=ASC (varchar)
If Varasc<0 then varasc=varasc+65536
If varasc>255 Then
Varhex=hex (VARASC)
Varlow=left (varhex,2)
Varhigh=right (varhex,2)
Strunicode2ansi=strunicode2ansi & ChrB ("&h" & Varlow) & ChrB ("&h" & Varhigh)
Else
Strunicode2ansi=strunicode2ansi & ChrB (VARASC)
End If
Next
End Function

Function Stransi2unicode (ascontents)
' Converts an ANSI-encoded string into a Unicode-encoded string
Stransi2unicode = ""
Len1=lenb (ascontents)
If Len1=0 then Exit function
For I=1 to Len1
VARCHAR=MIDB (ascontents,i,1)
VARASC=ASCB (varchar)
If VARASC > 127 Then
Stransi2unicode = Stransi2unicode & Chr (AscW (MidB (ascontents,i+1,1) & varchar))
I=i+1
Else
Stransi2unicode = Stransi2unicode & Chr (VARASC)
End If
Next
End Function

Function Base64Encode (ascontents)
' BASE64 encoding an ANSI-encoded string
' Ascontents should be an ANSI-encoded string (the binary string can also)
Dim lnposition
Dim Lsresult
Dim Char1
Dim CHAR2
Dim CHAR3
Dim CHAR4
Dim Byte1
Dim Byte2
Dim Byte3
Dim SaveBits1
Dim SaveBits2
Dim lsgroupbinary
Dim LSGROUP64
Dim M4,len1,len2

Len1=lenb (ascontents)
If Len1<1 Then
Base64encode= ""
Exit Function
End If

M3=len1 Mod 3
If M3 > 0 Then ascontents = ascontents & String (3-m3, ChrB (0))
' The number of complements is for ease of calculation.

IF m3 > 0 THEN
len1=len1+ (3-M3)
Len2=len1-3
Else
Len2=len1
End If

Lsresult = ""

For lnposition = 1 to Len2 step 3
LSGROUP64 = ""
Lsgroupbinary = Midb (ascontents, lnposition, 3)

Byte1 = ASCB (Midb (lsgroupbinary, 1, 1)): SaveBits1 = Byte1 and 3
Byte2 = ASCB (Midb (Lsgroupbinary, 2, 1)): SaveBits2 = Byte2 and 15
Byte3 = ASCB (Midb (Lsgroupbinary, 3, 1))

Char1 = Midb (Sbase_64_characters, (Byte1 and 252) \ 4) + 1, 1)
CHAR2 = Midb (Sbase_64_characters, ((Byte2 and +)/SaveBits1) + 1, 1)
CHAR3 = Midb (Sbase_64_characters, ((Byte3 and), or (SAVEBITS2 * 4) and &AMP;HFF) + 1, 1)
CHAR4 = Midb (Sbase_64_characters, (Byte3 and 63) + 1, 1)
LSGROUP64 = Char1 & Char2 & Char3 & CHAR4

Lsresult = Lsresult & LSGROUP64
Next

' Handle the last remaining few characters
If M3 > 0 Then
LSGROUP64 = ""
Lsgroupbinary = Midb (ascontents, len2+1, 3)

Byte1 = ASCB (Midb (lsgroupbinary, 1, 1)): SaveBits1 = Byte1 and 3
Byte2 = ASCB (Midb (Lsgroupbinary, 2, 1)): SaveBits2 = Byte2 and 15
Byte3 = ASCB (Midb (Lsgroupbinary, 3, 1))

Char1 = Midb (Sbase_64_characters, (Byte1 and 252) \ 4) + 1, 1)
CHAR2 = Midb (Sbase_64_characters, ((Byte2 and +)/SaveBits1) + 1, 1)
CHAR3 = Midb (Sbase_64_characters, ((Byte3 and), or (SAVEBITS2 * 4) and &AMP;HFF) + 1, 1)

If M3=1 Then
LSGROUP64 = Char1 & Char2 & ChrB (The) & ChrB (61) ' With the = number complement the number of digits
Else
LSGROUP64 = Char1 & Char2 & Char3 & ChrB (61) ' complement the number of digits with the = number
End If

Lsresult = Lsresult & LSGROUP64
End If

Base64Encode = Lsresult

End Function


Function Base64decode (ascontents)
' Converts a Base64 encoded string into an ANSI-encoded string
' Ascontents should also be an ANSI-encoded string (binary strings can also)
Dim Lsresult
Dim lnposition
Dim LsGroup64, Lsgroupbinary
Dim Char1, Char2, Char3, CHAR4
Dim Byte1, Byte2, Byte3
Dim M4,len1,len2

len1= Lenb (ascontents)
M4 = Len1 Mod 4

If len1 < 1 or M4 > 0 Then
' String length should be a multiple of 4
Base64decode = ""
Exit Function
End If

' Judge if the last one is not = #
' Judge if the second-lowest is not = #
' Here M4 represents the last remaining number of characters that need to be handled separately
If MidB (ascontents, len1, 1) = ChrB (then) m4=3
If MidB (ascontents, len1-1, 1) = ChrB (then) m4=2

If m4 = 0 Then
Len2=len1
Else
Len2=len1-4
End If

For lnposition = 1 to Len2 step 4
Lsgroupbinary = ""
LSGROUP64 = Midb (ascontents, lnposition, 4)
Char1 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 1, 1))-1
CHAR2 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 2, 1))-1
CHAR3 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 3, 1))-1
CHAR4 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 4, 1))-1
Byte1 = CHRB ((Char2) Or (CHAR1 * 4) and &AMP;HFF)
Byte2 = lsgroupbinary & Chrb ((CHAR3 and) \ 4) Or (CHAR2 *) and &AMP;HFF)
Byte3 = CHRB ((CHAR3 and 3) *) and &AMP;HFF) Or (CHAR4 and 63))
Lsgroupbinary = Byte1 & Byte2 & Byte3

Lsresult = Lsresult & lsgroupbinary
Next

' Handle the last remaining few characters
If M4 > 0 Then
Lsgroupbinary = ""
LSGROUP64 = Midb (ascontents, Len2+1, M4) & ChrB (in) ' Chr () =a, converted to 0
If M4=2 then ' complements 4 bits, is for easy calculation
LSGROUP64 = lsGroup64 & ChrB (65)
End If
Char1 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 1, 1))-1
CHAR2 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 2, 1))-1
CHAR3 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 3, 1))-1
CHAR4 = INSTRB (Sbase_64_characters, Midb (LSGROUP64, 4, 1))-1
Byte1 = CHRB ((Char2) Or (CHAR1 * 4) and &AMP;HFF)
Byte2 = lsgroupbinary & Chrb ((CHAR3 and) \ 4) Or (CHAR2 *) and &AMP;HFF)
Byte3 = CHRB ((CHAR3 and 3) *) and &AMP;HFF) Or (CHAR4 and 63))

If m4=2 Then
Lsgroupbinary = Byte1
ElseIf M4=3 Then
Lsgroupbinary = Byte1 & Byte2
End If

Lsresult = Lsresult & lsgroupbinary
End If

Base64decode = Lsresult

End Function
%>

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.