LUA's base64 encryption and decryption algorithm.

Source: Internet
Author: User
Tags lua pow

Tag:group    decryption    mod   base   string    body   ast   else   byte   

Local function encodeBase64 (source_str) Local b64chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/' local s64 = ' local str = SOURCE_STR while #st            R > 0 do local bytes_num = 0 Local buf = 0 for byte_cnt=1,3 do buf = (BUF * 256)                If #str > 0 Then buf = buf + string.byte (str, 1, 1) str = string.sub (str, 2) Bytes_num = bytes_num + 1 end end for Group_cnt=1, (bytes_num+1) do local b64ch AR = Math.fmod (Math.floor (buf/262144), + 1 S64 = s64).            String.sub (B64chars, B64char, B64char) buf = buf * "End for Fill_cnt=1" (3-bytes_num) do S64 = S64. ' = ' End end return s64endlocal function decodeBase64 (str64) Local b64chars = ' Abcdefghijklmnopqrstuvwxyzab cdefghijklmnopqrstuvwxyz0123456789+/' local temp={} for i=1,64 do Temp[string.sub (b64chars,i,i)] = i enD temp[' = ']=0 local str= "" For I=1, #str64, 4 does if i> #str64 then break end Loca L data = 0 Local str_count=0 for j=0,3 does local str1=string.sub (STR64,I+J,I+J) if not            TEMP[STR1] then return to end If TEMP[STR1] < 1 then data = data * 64        else data = data * + temp[str1]-1 Str_count = str_count + 1 End End for j=16,0,-8 does if str_count > 0 then str=str. String.char (Math.floor (Data/math.pow (2,j))) Data=math.mod (Data,math.pow (2,j)) Str_count = St    R_count-1 end end Local last = Tonumber (String.byte (str, String.len (str), String.len (str))) If last = = 0 Then str = string.sub (str, 1, String.len (str)-1) End return Strend

Lua's base64 encryption and decryption algorithm.

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: 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.