分享

Corona中文站

 SmingPro 2016-11-23

lua中对字符串进行Base64加密的函数。备份!

function str2base64(str)
 local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
 local s64 = ''

 while #str > 0 do -- iterate through string
  local bytes_num = 0 -- number of shifted bytes
  local buf = 0 -- input buffer

  for byte_cnt=1,3 do
   buf = (buf * 256)
   if #str > 0 then -- if string not empty, shift 1st byte to buf
    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
   b64char = math.fmod(math.floor(buf/262144), 64) + 1
   s64 = s64 .. string.sub(b64chars, b64char, b64char)
   buf = buf * 64
  end

  for fill_cnt=1,(3-bytes_num) do
   s64 = s64 .. '='
  end
 end

 return s64
end
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多