首页 php教程 php手册 lua 与 php 通过AES数据加密进行通讯

lua 与 php 通过AES数据加密进行通讯

Jun 06, 2016 pm 07:54 PM
aes lua php 加密 数据 进行 通讯 通过

最近公司有款《围住神经猫》的微信小游戏火爆的不行!公司又决定开发一系列的神经猫的小游戏,于是,我被拉过来了。 后来使用cocos-2dx 开发一款小游戏,客户端用的是lua脚本,为了服务器与客户端交互的安全性,我们决定对API接口 传输的JSON数据进行加密、

最近公司有款《围住神经猫》的微信小游戏火爆的不行!公司又决定开发一系列的神经猫的小游戏,于是,我被拉过来了。

后来使用cocos-2dx 开发一款小游戏,客户端用的是lua脚本,为了服务器与客户端交互的安全性,我们决定对API接口

传输的JSON数据进行加密、解密。一般情况就是客户端加密,服务器段进行解密:

lua客户端使用的是一个纯lua写的库:aeslua,下载地址:http://luaforge.net/projects/aeslua/

但是该库是有问题的:用该库加密解密是没有问题的,但是跟PHP通讯就存在问题了,因为该库加密后base64之后的

字符串PHP是无法解密的!为了这个问题,我查阅了好多资料,终于找到某个国外大神的解决办法:

http://chainans.blogspot.com/2012/09/working-with-lua-encryption.html(可能有些同学无法FQ,故把原文贴出来如下:)

Working with Lua encryption

Recently working with Corona SDK, I start to need some standard encryption/decryption algorithm in Lua. To start with, actually, it has rather small number of developers comparing to the Objective-C which I have been working with. Meaning that there are fewer 3rd party librarys you can rely upon. Luckily, I found one called AESLua which has some code to start. From there, my objective is to make a way to securely passing data between my client and server. (php on server-side) In fact, from what I'd read, my method is not very secure but it is better than nothing. Just for my reference, here are the list of issues along the way


Edited: Tested with iPhone 4... Input cipher text of 1280 characters. Take around 25 seconds. Unacceptable speed for general uses.


1) It requires Lua 5.2 feature which does not seem to be in Corona

Solution: Download LuaBit v0.4 and integrate it... You will need to make a mapping to allow API call to the proper place

2) Next you need to get Base64 library -- grab it here https://gist.github.com/2563975 -- It initially made to allow passing it over the URL (using '-' and '_' instead of '+' and '/') So, I change them to the latter one.

3) For AESLua, by default, it uses AES-128, CBC, some kind of random padding

http://www.unsw.adfa.edu.au/~lpb/src/AEScalc/AEScalc.html

http://www.tools4noobs.com/online_tools/decrypt/

Here are the things to do

3.1) In pwInKey function, comment the line out

 password = ciphermode.encryptString(pwBytes, password, ciphermode.encryptCBC);

3.2) In util.padByteString function, change it to

    local paddingLength = math.ceil(#data/16)*16 - #data;

    local padding = "";

    local paddingValue = string.char ( paddingLength )  -- PKCS7 padding

    for i=1,paddingLength do

padding = padding .. paddingValue;-- PKCS7 padding

    end 


    return data .. padding;


4) Set up web server for testing, you will need php / mcrypt mod to test.

5) Creating a php for testing... here is a code

Now, my plain text below is "1234567890123456ss@#%de".



$data = 'dXzNDNxckOrb7uz2ON0AAJp4BXgkYewblTNWBSAQSEw=';

$key128 = '1234567890123456';

$iv =  '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0';


echo mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key128, base64_decode($data), MCRYPT_MODE_CBC, $iv)



?>



That's it. The encryption backward to client machine should be a piece of cake. =)


*** By using these library, the user should be aware of the fact that Lua's performance is still far from native code. You may not want to use this algorithm to encrypt a large volume of data.


按照他的办法,一切都OK了。但是有以下几点需要说明以下:(本人摸索的)

1.利用CBC模式加密的字符串的key必须是16位,否则PHP无法解密!

2.明文字符串的必须把key作为前缀加进去

3.上面文章中没有把unpack函数写出来,本人查阅了一些资料,补充了,否则aeslua无法正常解密了!

util.lua中的下面这个函数改为如下:

function public.unpadByteString(data)
    local padLength = tonum((string.byte(data, #data)));
    return string.sub(data,1, #data-padLength)   --unpack
end

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和时间

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

CakePHP 文件上传

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

讨论 CakePHP

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

CakePHP 创建验证器

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

您如何在PHP中解析和处理HTML/XML?

See all articles