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

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

传输的JSON数据进行加密、解密。普通情况就是client加密,server段进行解密:

luaclient使用的是一个纯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 <- I don't know its name, IV = 0. I will change it into is AES-128, CBC, PKCS7 padding. Here are the website to test if our conversion is ok or not
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".

<?php


$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

lua 与 php 通过AES数据加密进行通讯的更多相关文章

  1. CMDB 数据加密 最终整合API验证+AES数据加密

    当CMDB运行在内网的时候,经过API验证的三关是没有问题的,但是如果运行在外网,有一个问题是,黑客截取后的访问速度比客户端快的时候还会造成数据泄露.为了解决这个问题,就要对数据进行加密 RSA加密 ...

  2. 使用openssl库实现RSA、AES数据加密

         openssl是可以很方便加密解密的库,可以使用它来对需要在网络中传输的数据加密.可以使用非对称加密:公钥加密,私钥解密.openssl提供了对RSA的支持,但RSA存在计算效率低的问题,所 ...

  3. [转]使用openssl库实现RSA、AES数据加密

    openssl是可以很方便加密解密的库,可以使用它来对需要在网络中传输的数据加密.可以使用非对称加密:公钥加密,私钥解密.openssl提供了对RSA的支持,但RSA存在计算效率低的问题,所以一般的做 ...

  4. xbee无线通讯终端

    无线数传电台支持DIGI全系列无线数传模块,包括有xbee/xbee PRO S1.xbee/xbee PRO S2C.xbee WIFI.xbee PRO S3B.xbee/xbee PRO SX( ...

  5. 我的iOS-App

    1.PocketConfidential(密保箱) 简介 保存账号密码等敏感信息. 应用技术: sqlite.sqlcipher加密.AES数据加密.GCD https://itunes.apple. ...

  6. 资产信息之收集资产代码流程,API的一个认证,数据库表的设计

    收集资产代码流程 1.起初我们些的代码是面条式的一堆的逻辑判断.   后来通过了不断的优化升级实现了一个3种方案都支持的CMDB系统,我们用哪种方案只需要在配置文件里修改一下设置就行了.   同时我们 ...

  7. SaltStack安装配置详解

    一.简介 Saltstack 比 Puppet 出来晚几年,是基于Python 开发的,也是基于 C/S 架构,服务端 master 和客户端 minions :Saltstack 和 Puppet ...

  8. CMDB资产采集笔记

    一.资产采集四种方式 1. Agent方式 API:Django接收数据并入库 程序:放置在每台服务器 应用场景:针对服务器较多的公司 步骤一: #执行本地命令的库 import subprocess ...

  9. [C++] WinAES的问题

    WinAES是个不错的windows CAPI封装. 如果C++程序需要和java的程序进行aes加解密通讯,那么WinAES的代码是有问题的. java的aes代码缺省不会设置IV而且采用ECB模式 ...

随机推荐

  1. IT第三天 - 数据类型、转换、Scanner使用

    IT第三天 上午 变量类型 1.6种数值类型:byte.short.int.long.float.double:其中byte是8个字节,short是16字节,int是32字节.long是64字节:日常 ...

  2. SQL查询各阶段的统计信息

            我们经常会遇到各种分类统计问题,须要将这些结果一次显示出来.这次老师提出的要求是我想看60分下面多少人.60~70多少人,70~80多少人.80~90多少人,90~100多少人.他们曾 ...

  3. ThinkPHP的验证码刷新显示和验证码显示不出来的原因

    1.应当这样<imp src='验证码路径' onclick="this.src='验证码路径?'+Math.random()">;如果后面不加Math.random( ...

  4. sql执行疑问

    遇到一个奇怪的事情,学习执行计划的时候写了这么一个sql来看执行计划的执行顺序, SELECT COUNT(*) FROM SIC84 WHERE AAC001 IN (SELECT AAC001 F ...

  5. bash有空格的文件名

    http://www.keakon.net/2011/10/20/bash%E4%B8%8B%E5%A4%84%E7%90%86%E5%8C%85%E5%90%AB%E7%A9%BA%E6%A0%BC ...

  6. 【集训笔记】【大数模板】特殊的数 【Catalan数】【HDOJ1133【HDOJ1134【HDOJ1130

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3324 http://blog.csdn.net/xymscau/artic ...

  7. Ubuntu服务器上SSH Server 的安装和设置

    网上有很多介绍在Ubuntu下开启SSH服务的文章,但大多数介绍的方法测试后都不太理想,均不能实现远程登录到Ubuntu上,最后分析原因是都没有真正开启ssh-server服务.最终成功的方法如下: ...

  8. jquey的 ajax请求的几种方式

    在jquery中,提供了集中方法来进行ajax操作 一.$.get(url,[data],[callback]) 向服务器发起get操作. 说明:url为请求地址,data为请求数据的列表(json对 ...

  9. [转]如何申请和管理一个sourceforge项目

    假如你没有贡献免费源代码的想法,就不用继续读本文了.:) 如果读者的e文不错,作者建议你直接阅读sourceforge的 howto文档,他们整理得非常周到详实.本文只是作者的使用经验,也许可以给与作 ...

  10. Python关键字yield详解以及Iterable 和Iterator区别

    迭代器(Iterator) 为了理解yield是什么,首先要明白生成器(generator)是什么,在讲生成器之前先说说迭代器(iterator),当创建一个列表(list)时,你可以逐个的读取每一项 ...