lua 与 php 通过AES数据加密进行通讯
最近公司有款《围住神经猫》的微信小游戏火爆的不行!公司又决定开发一系列的神经猫的小游戏,于是,我被拉过来了。 后来使用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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
