特别注意
Base64 主要不是加密,它主要的用途是把一些二进制数转成普通字符用于网络传输。由于一些二进制字符在传输协议中属于控制字符,不能直接传送需要转换一下就可以了
原理
将文件读入内存,由于读入内存的文件本质上是一个字节类型数组bufferA,新建一个bufferB,对数组A中连续的每三个字节值取出,将其扩充成四个字节,不够的高位补0,对于最后组不成三个字节的处理,少一个就补一个=,最多补两个==;
举例:加密“ace”,

ace转化为二进制为:‭01100001‬ ‭01100011‬ ‭01100101‬

转化为base64的四字节六位:011000 01‬‭0110 0011‬01 100101‬

那因为计算机是一字节八位的存数,所以高位补00后变为:00011000 0001‬‭0110 000011‬01 00100101‬

转化为十进制:24 22 13 37

查Base64对照表(默认版本RFC2045):
我们得到最终结果:YWNl
我们观察这个对照表,大小写的字母26*2 加上10个数字 加上两个特殊符号 + / 一共64个字符,因为Base64有效位只有六位,所以最大能表示的字符就为2的6次方64;
分享一个lua版本的base64源码:

local Base64 = {}
local string = string
Base64.__code = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',};
Base64.__decode = {}
for k,v in pairs(Base64.__code) doBase64.__decode[string.byte(v,1)] = k - 1
end
function Base64.encode(text)local len = string.len(text)local left = len % 3len = len - leftlocal res = {}local index  = 1for i = 1, len, 3 dolocal a = string.byte(text, i )local b = string.byte(text, i + 1)local c = string.byte(text, i + 2)-- num = a<<16 + b<<8 + clocal num = a * 65536 + b * 256 + c for j = 1, 4 do--tmp = num >> ((4 -j) * 6)local tmp = math.floor(num / (2 ^ ((4-j) * 6)))--curPos = tmp&0x3flocal curPos = tmp % 64 + 1res[index] = Base64.__code[curPos]index = index + 1endendif left == 1 thenBase64.__left1(res, index, text, len)elseif left == 2 thenBase64.__left2(res, index, text, len)        endreturn table.concat(res)
endfunction Base64.__left2(res, index, text, len)local num1 = string.byte(text, len + 1)num1 = num1 * 1024 --lshift 10 local num2 = string.byte(text, len + 2)num2 = num2 * 4 --lshift 2 local num = num1 + num2local tmp1 = math.floor(num / 4096) --rShift 12local curPos = tmp1 % 64 + 1res[index] = Base64.__code[curPos]local tmp2 = math.floor(num / 64)curPos = tmp2 % 64 + 1res[index + 1] = Base64.__code[curPos]curPos = num % 64 + 1res[index + 2] = Base64.__code[curPos]res[index + 3] = "="
endfunction Base64.__left1(res, index,text, len)local num = string.byte(text, len + 1)num = num * 16 tmp = math.floor(num / 64)local curPos = tmp % 64 + 1res[index ] = Base64.__code[curPos]curPos = num % 64 + 1res[index + 1] = Base64.__code[curPos]res[index + 2] = "=" res[index + 3] = "="
endfunction Base64.decode(text)local len = string.len(text)local left = 0 if string.sub(text, len - 1) == "==" thenleft = 2 len = len - 4elseif string.sub(text, len) == "=" thenleft = 1len = len - 4endlocal res = {}local index = 1local decode = Base64.__decodefor i =1, len, 4 dolocal a = decode[string.byte(text,i    )] local b = decode[string.byte(text,i + 1)] local c = decode[string.byte(text,i + 2)] local d = decode[string.byte(text,i + 3)]--num = a<<18 + b<<12 + c<<6 + dlocal num = a * 262144 + b * 4096 + c * 64 + dlocal e = string.char(num % 256)num = math.floor(num / 256)local f = string.char(num % 256)num = math.floor(num / 256)res[index ] = string.char(num % 256)res[index + 1] = fres[index + 2] = eindex = index + 3endif left == 1 thenBase64.__decodeLeft1(res, index, text, len)elseif left == 2 thenBase64.__decodeLeft2(res, index, text, len)endreturn table.concat(res)
endfunction Base64.__decodeLeft1(res, index, text, len)local decode = Base64.__decodelocal a = decode[string.byte(text, len + 1)] local b = decode[string.byte(text, len + 2)] local c = decode[string.byte(text, len + 3)] local num = a * 4096 + b * 64 + clocal num1 = math.floor(num / 1024) % 256local num2 = math.floor(num / 4) % 256res[index] = string.char(num1)res[index + 1] = string.char(num2)
endfunction Base64.__decodeLeft2(res, index, text, len)local decode = Base64.__decodelocal a = decode[string.byte(text, len + 1)] local b = decode[string.byte(text, len + 2)]local num = a * 64 + bnum = math.floor(num / 16)res[index] = string.char(num)
end
return Base64

测试代码如下:

function Base64File(sourcepath,targetpath,isEncode)local file = io.open(sourcepath,"rb")if file then--print "发现文件\n"local files = file:read("*a");local result;if  isEncode == true thenresult = require("./Base64").encode(files);elseresult = require("./Base64").decode(files);endlocal tagetFile = io.open(targetpath, "wb")tagetFile:write(result);tagetFile:close();file:close();elseprint "没有找到文件"end
end--例子
--Base64File("./source.jpeg","./mnp.jpeg",true)
--Base64File("./mnp.jpeg","./mn.jpeg",false)
--Base64File("./test.lua","./testLua.lua",true);
Base64File("./testLua.lua","./testLuaNew.lua",false)

最后:标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。
为解决此问题,可采用一种用于URL的改进Base64编码,它不仅在末尾去掉填充的’='号,并将标准Base64中的“+”和“/”分别改成了“-”和“_”,这样就免去了在URL编解码和数据库存储时所要作的转换

lua版本base64加密和解密相关推荐

  1. Base64加密与解密使用(+拓展:盐值法)

    1.什么是Base64 Base64是一种直接利用64位可打印字符来表示二进制数据的算法,是一种比较常见的加密算法.JDK1.8版本提供了java.util.Base64的工具类,使用Base64提供 ...

  2. 浅析android手游lua脚本的加密与解密(番外篇之反编译的对抗)

    前言   去年在看雪论坛写了一篇<浅析android手游lua脚本的加密与解密>的精华文章,今年写一篇番外篇,将一些lua反编译对抗的内容整合一起,并以3个实例作为说明(包括2018腾讯游 ...

  3. php cookie 加密解密,php 使用base64加密、解密cookie的示例

    这篇文章主要为大家详细介绍了php 使用base64加密.解密cookie的示例,具有一定的参考价值,可以用来参考一下. 感兴趣的小伙伴,下面一起跟随512笔记的小编罗X来看看吧. 经测试代码如下: ...

  4. python中base64加密和解密

    base64加密和解密有点头疼,必须要记录下来,以后忘了也能再来复习下 当然啦,能一直记住是最好的-加油吧,少年(其实已经好老了) 操作环境 win10,python3 base的原理 这个还是别人讲 ...

  5. Javascript Base64加密与解密

    Base64加密与解密 Base64.js function Base64() {// private property_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWX ...

  6. shell脚本:base64加密、解密字符串并赋值给变量方法

    几经波折才能够实现使用 base64 加密.解密字符串并赋值给变量.网上绝大多数是使用 echo 管道实现 加密和解密字符串的输出. 代码如下: pwd=$( base64 -d <<&l ...

  7. java base64加密与解密

    目录 前言 一.base64加密与解密 1. base64有填充的编码与解码 2. base64无填充的编码与解码 二.MIME友好型base64加密与解密 前言 Base64 编码会将字符串编码得到 ...

  8. 浅析android手游lua脚本的加密与解密

    2018.05.02更新 这段时间在翻备份的硬盘,突然发现了以前的分析项目和代码,从里面提取了之前附件的内容,现在上传给大家,真是柳暗花明又一村啊.附件包括201703版本的梦幻手游里面提取的so文件 ...

  9. mysql base64 加密解密_烂泥:base64加密与解密

    本文由ilanniweb微信公众号提供友情赞助,首发于烂泥行天下 jenkins技术分享QQ群:571981257 一.什么是base64 base64是网络上最常见的用于传输8Bit字节码的编码方式 ...

最新文章

  1. python如何调用文件进行换位加密_Python换位密码
  2. mysql中concat函数的使用相关总结
  3. pycharm安装scrapy失败_Scrapy ——环境搭配与一个简单的例子
  4. JUnit中测试异常抛出的方法
  5. jQuery插件之ajaxFileUpload(异步上传图片并实时显示,并解决onchange后ajaxFileUpload失效问题)...
  6. 浅析商业银行“业务连续性管理体系”的构建
  7. 使用 Angular
  8. 【Spark Summit EU 2016】沃森媒体分析系统:从单租户Hadoop到3000租户Spark的架构演进...
  9. 【转】Sql Server 跨服务器连接
  10. fields and vector spaces
  11. 常见的概率论问题清单及其答案
  12. Android上图片文字识别
  13. html中文字放在图片下边,css图片下边怎么加字
  14. 从零到上亿用户,我是如何一步步优化MySQL数据库的?(建议收藏)
  15. 叉乘点乘混合运算公式_职测解题技巧:数学运算的35个基础公式
  16. 报错:实体名称必须紧跟在 '' 后面
  17. 阿里云短信业务SMS
  18. c语言printf输出语句_C语言中另一个printf()语句中的printf()语句
  19. 电脑游戏业编年史之一游戏的诞生
  20. 用MATLAB绘制国债NSS模型,Matlab在数字信号处理中的运用.ppt

热门文章

  1. java因子分析,SPSS统计分析全解析▶主成分分析与因子分析
  2. Studio 3T for MongoDB脚本-----两种方法
  3. Xcode9 xcodebuild export plist 配置
  4. 关于“联想”笔记本“无线网功能关闭”的问题的解决方案
  5. QQ音乐JS逆向爬虫,我用python全都爬!
  6. 如何恢复录音删除的录音文件_录音文件降噪?快使用GoldWave!
  7. 面试官反感的求职者(下)
  8. kettle的下载安装以及问题点
  9. MRO工业品采购如何降低成本?SCM供应链管理系统助力企业优化采购流程
  10. Python爬虫——Scrapy框架