local function encodeBase64(source_str)
local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local s64 = ''
local str = source_str while #str > 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 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 local function decodeBase64(str64)
local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local temp={}
for i=1,64 do
temp[string.sub(b64chars,i,i)] = i
end
temp['=']=0
local str=""
for i=1,#str64,4 do
if i>#str64 then
break
end
local data = 0
local str_count=0
for j=0,3 do
local str1=string.sub(str64,i+j,i+j)
if not temp[str1] then
return
end
if temp[str1] < 1 then
data = data * 64
else
data = data * 64 + temp[str1]-1
str_count = str_count + 1
end
end
for j=16,0,-8 do
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 = str_count - 1
end
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 str
end

lua之base64加密和解密算法。的更多相关文章

  1. 常见的加密和解密算法—BASE64

    一.BASE64加密和解密概述 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息.例如,在Java Persistence系 ...

  2. JAVA实现AES的加密和解密算法

    原文 JAVA实现AES的加密和解密算法 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import ja ...

  3. 常见的加密和解密算法—AES

    一.AES加密概述 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用 ...

  4. 常见的加密和解密算法—DES

    一.DES加密概述 DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并 ...

  5. 常见的加密和解密算法—MD5

    一.MD5加密概述 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 13 ...

  6. Java Base64加密、解密原理Java代码

    Java Base64加密.解密原理Java代码 转自:http://blog.csdn.net/songylwq/article/details/7578905 Base64是什么: Base64是 ...

  7. JS URL 使用base64加密与解密

    JS编码方式: <script type="text/javascript"> document.write(encodeURI("http://www.w3 ...

  8. DES的加密与解密算法(Python实现)

    DES的加密与解密算法(Python实现) 密码学实验:实现了DES的简单的加密和解密算法,DES算法的相关资料网上很多,这里不再赘述,仅仅贴出源代码给大家分享,源码中包含很多汉字注释,相信大家都是可 ...

  9. JS URL 使用base64加密与解密和MD5解密

    JS编码方式: <script type="text/javascript"> document.write(encodeURI("http://www.w3 ...

随机推荐

  1. PHP - 脚本退出(包括异常退出),执行指定代码

    之前做聊天室的时候有那么个需求就是当用户异常断线的时候就应该清除她的在线状态.因为当时对于flush不够了解,尝试了各种办法,好像都没办法在我们开发机上面执行相应的代码.后来知道是flush的原因.我 ...

  2. /etc/rc5.d/s991local: line25: eject:command not found错误

      使用虚拟机安装centos出现错误,原因是我使用的镜像是最小级别的,没有图形化界面,只有终端窗口 有人用vmware安装minimal centos报错/etc/rc5.d/s99local : ...

  3. spring 采用编程式事务

    1.getCurrentSession()与openSession()的区别? * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession() ...

  4. AttributeUsage

    [AttributeUsage] System.AttributeUsage声明一个Attribute的使用范围与使用原则. AllowMultiple 和 Inherited 参数是可选的,所以此代 ...

  5. 117 FP页面无法查看 此错误是JDK8.0.0.0版本的一个BUG,会导致工单重复回写,

    用户表示117 FP页面无法查看,提示如下错误: 跟进: 1.进入FP服务器可看到以下错误 这个错误的框就表示FP的一个进程报错,自动断掉了,需要重新跑一次EXIT,INIT,PLAN,EXPORT, ...

  6. 简述 ascii、unicode、utf-8、gbk 的关系 (全网最全!!!)

    ascii 是最早美国用的标准信息交换码,把所有的字母的大小写,各种符号用 二进制来表示,共有256中,加入些拉丁文等字符,1bytes代表一个字符, Unicode是为了统一世界各国语言的不用,统一 ...

  7. php设置错误,错误记录

    //设置错误级别. error_reporting(E_ALL);  //显示所有错误 error_reporting(E_ALL&~E_NOTICE);  //显示所有错误但不显示提示级别的 ...

  8. c++ 迭代器模式(iterator)

    提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示.当你需要访问一个聚集对象,而且不管这些对象是什么都需要遍 历的时候,就应该考虑用迭代器模式.同时需要对聚集有多种方式遍历时,可以 ...

  9. Spring框架总结(五)

    自动装配(了解) 根据名称自动装配:autowire="byName" 自动去IOC容器中找与属性名同名的引用的对象,并自动注入 延续使用user.dao.service.acti ...

  10. java.lang.ClassCastException: com.liuyang.annocation.UserAction cannot be cast to com.liuyang.annocation2.UserAction at com.liuyang.annocation2.App.test

    java.lang.ClassCastException: com.liuyang.annocation.UserAction cannot be cast to com.liuyang.annoca ...