Write a simple encryption and decryption class with ASP ....

Source: Internet
Author: User
Tags base64 chr
Encryption | Decrypt class Base64class
REM Const
Dim sbase_64_characters ' Conversion Code
Dim lenstring ' Computes the length of a string
Dim icount ' Counter
Dim ReturnValue ' return Value
Dim Tempchar ' Cached characters
Dim tempstring ' Cached String
Dim paramstring ' Parameter String
Dim Temhex ' Cache Cache hex
Dim Templow ' Cache Low
Dim Temphigh ' Cache High
Dim mod3string '
Dim mod4string '
Dim Tempbinary '
Dim Tempbyteone '
Dim Tempbytetwo '
Dim Tempbytethree '
Dim Tempbytefour '
Dim Tempsavebitsone '
Dim Tempsavebitstwo '
'********************************************
' Begin initializing Class
'********************************************
Private Sub Class_Initialize ()
Sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/" End Sub
'********************************************
' End Initialization class '
********************************************

'********************************************
' Begin destroying class
'********************************************
Private Sub Class_Terminate ()
Sbase_64_characters= "" End Sub
'********************************************
' End Destroy class
'********************************************

'********************************************
' Begin BASE64 encoding an ANSI-encoded string
'********************************************
Public Function Encode (paramstring)
Tempstring= ""
Returnvalue= ""
Lenstring=len (paramstring)
If Lenstring<1 Then
Encode=returnvalue
Else
Mod3string=lenstring MoD 3
' The number of complements is for ease of calculation.
If Mod3string>0 Then
Lenstring=lenstring+3-mod3string
Lenstring=lenstring-3
End If
' *************************begin
For Icount=1 to lenstring step 3
Tempbinary = Mid (paramstring, icount, 3)
' Response.Write tempbinary
Tempbyteone= ASC (Mid (tempbinary, 1, 1)): Tempsavebitsone = Tempbyteone and 3
Tempbytetwo = ASC (Mid (Tempbinary, 2, 1)): Tempsavebitstwo = Tempbytetwo and 15
Tempchar = ASC (Mid (Tempbinary, 3, 1))
Tempbyteone = Mid (Sbase_64_characters, (Tempbyteone and 252) \ 4) + 1, 1)
Tempbytetwo = Mid (Sbase_64_characters, (Tempbytetwo and R) and &AMP;HFF) + 1, 1)
Tempbytethree = Mid (Sbase_64_characters, (Tempchar and) Or (Tempsavebitstwo * 4) and &AMP;HFF) + 1, 1)
Tempbytefour = Mid (Sbase_64_characters, (Tempchar and 63) + 1, 1)
tempstring = Tempbyteone & tempbytetwo & tempbytethree & tempbytefour Returnvalue=returnvalue & TempStrin G Next
' *************************end
' *************************begin processing the last remaining few characters
If Mod3string>0 Then
Tempbinary = Mid (paramstring, icount, mod3string)
If Mod3string=1 Then
tempstring = tempbinary & Chr ($) & Chr ($) & CHR (64) ' with the @ number of digits
else tempstring = tempbinary & Chr ($) & CHR (64) ' complement digits with @
End If
Returnvalue=returnvalue & TempString
End If
' *************************end processing the last remaining few characters
Encode=returnvalue End If End function
'********************************************
' End BASE64 encoding an ANSI-encoded string
'********************************************

'********************************************
' End converts a Base64 encoded string into an ANSI-encoded string
'********************************************
Public Function Decode (paramstring)
Tempstring= ""
Returnvalue= ""
Lenstring=len (paramstring)
If Lenstring<1 Then
Decode=returnvalue
Else
Mod4string=lenstring MoD 4
If mod4string >0 then ' string length should be a multiple of 4
Decode=returnvalue
Else ' begin to judge whether the @ number
If Mid (paramstring, lenString-1, 1) = "@" Then
mod4string=2
End If
If Mid (paramstring, lenString-2, 1) = "@" Then
Mod4string=1
End If
' End judgment is not the @ number
If Mod4string>0 Then
Lenstring=lenstring-4
End If
' ******************************begin
For Icount=1 to lenstring step 4
tempstring = Mid (paramstring, icount, 4)
Tempbyteone = InStr (Sbase_64_characters, Mid (tempstring, 1, 1))-1
Tempbytetwo = InStr (Sbase_64_characters, Mid (tempstring, 2, 1))-1
Tempbytethree = InStr (Sbase_64_characters, Mid (tempstring, 3, 1))-1
Tempbytefour = InStr (Sbase_64_characters, Mid (tempstring, 4, 1))-1
Tempbyteone = Chr ((tempbytetwo) or (Tempbyteone * 4) and &AMP;HFF) Tempbytetwo = "" & Chr ((Tempbytethre E and) \ 4) Or (Tempbytetwo *) and &AMP;HFF)
Tempbytethree = Chr ((Tempbytethree and 3) *) and &AMP;HFF) Or (Tempbytefour and



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.