Base64 addition and decryption function in ASP

Source: Internet
Author: User
Tags base64 chr

 

<% OPTION EXPLICIT Const BASE_64_MAP_INIT = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/" Dim NL ' Zero based arrays Dim Base64encmap (+) Dim base64decmap (127) ' must be called before using anything else public SUB I Nitcodecs () ' init vars nl = ' <P> ' & chr & chr ' Setup base ' Dim max, idx max = Len (base_64_map_in IT) for idx = 0 to Max-1 ' one based string base64encmap (idx) = Mid (Base_64_map_init, IDX + 1, 1) next-to-idx = 0 to Ma X-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, first, second, third by3 = (Len (p Lain)/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 () ret = ret & Base64encmap ((First * 16) and () + ((SECOND/16) anD) ret = ret & Base64encmap (((Second * 4) and + ((THIRD/64) and 3)) ret = ret & Base64encmap (third and ndx = ndx + 3 loop if by3 < Len (plain) then first = ASC (Mid (Plain, ndx+0, 1)) ret = ret & Base64encmap ((f IRST/4) if (Len (plain) MOD 3) = 2 Then second = ASC (Mid (Plain, ndx+1, 1)) ret = ret & Base64encmap (((Firs T * +) + ((SECOND/16) and)) ret = ret & Base64encmap ((((second * 4) and) ELSE ret = ret & Bas E64encmap (First *) and () ret = ret & "=" End If ret = ret & "=" End If Base64Encode = Ret end FUNCTION ' de Code base encoded string public FUNCTION Base64decode (scrambled) if Len (scrambled) = 0 Then Base64decode = "" Exit func tion 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 = Base64 Decmap (ASC (Mid (scrambled, ndx+0, 1))) Second = Base64decmap (ASC (Scrambled, ndx+1, 1)) Third = Base64decmap (ASC (Mid (Scrambled, ndx+2, 1))) Fou Rth = Base64decmap (ASC (Mid (Scrambled, ndx+3, 1)) ret = ret & Chr ((First * 4) and 255) + ((SECOND/16) and 3) RET = RET & Chr (((second *) and 255) + ((THIRD/4) and ()) ret = ret & Chr (((third *) and 255) + (fourth an D)) Ndx = ndx + 4 loop if ndx < Reallen then first = BASE64DECMAP (ASC (Mid (Scrambled, ndx+0, 1))) Second = Base64dec MAP (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 *) and 255) + ((THIRD/4) and 1 5) End If End If Base64decode = Ret End FUNCTION ' Initialize call Initcodecs ' Testing code Dim InP, Encode ' INP = req Uest. QueryString ("input") encode = Base64Encode (INP) Response.Write "Encoded value =" & Encode & nl Response.Write "De Coded value = "&amP 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.