[置顶] 使用python对cocos2dx的手游图片资源进行加密
2016-09-08 15:29
721 查看
使用python对cocos2dx的手游图片资源进行加密
导言
由于项目的需要,要对游戏的图片资源进行加密,目前比较常用的做法应该是使用TexturePacker的加密功能。但因为我们的整个打包流程都是python脚本控制,自动化完成的,如果要使用TexturePacker的加密功能,就要把TexturePacker的命令行工具集成到整个自动化打包工具中。考虑到工作量会比较大,所以决定采用在图片压缩后自动遍历assets目录下的所有的图片并使用python自动进行字节流加密的解决方案。缺点是加密方式比较简单,易于破解。步骤
1.编写python加密模块
加密模块的大致思路就是以2进制可读的方式打开图片资源文件,然后遍历字节流,对每个字节进行一定的转化,我采用的是异或一个随机的值,然后在整个文件的头部加上三个字节的标识,用来标识这个图片是否进行了加密处理。然后第四个字节保存加密的密钥。最后将二进制数据流写入一个新的图片文件,同时删除老的图片文件。图片的加密过程到此就结束了。######################################## # __encrypt__.py # 功能:对png、jpg进行加密处理 # 作者:刘翰男 # 创建:2016-09-01 ######################################## import os import sys import random print("__encrypt__.py") #随机生成密匙random.randint(1, 255) encrypt_key = random.randint(1, 255) #加密过的标示,两个字节 have_encrypt_first_flag = 0x12 have_encrypt_second_flag = 0x34 have_encrypt_third_flag = 0x56 #images_path是需要加密的图片的绝对路径 def encrypt(images_path): f = open(images_path, 'rb') filedata = f.read() filesize = f.tell() f.close() file_byte_array = bytearray(filedata) #判断是否加密过 if(have_encrypt_first_flag == file_byte_array[0] and have_encrypt_second_flag == file_byte_array[1] and have_encrypt_third_flag == file_byte_array[2]): print('this png has encrypt before!') else: encrypt_file_byte_array = bytearray(0) encrypt_file_byte_array.append(have_encrypt_first_flag) encrypt_file_byte_array.append(have_encrypt_second_flag) encrypt_file_byte_array.append(have_encrypt_third_flag) encrypt_file_byte_array.append(encrypt_key) for byte in file_byte_array: encrypt_bype = byte ^ encrypt_key encrypt_file_byte_array.append(encrypt_bype) workdir = os.path.split(images_path)[0] os.remove(images_path) f2 = open(images_path,'wb') f2.write(encrypt_file_byte_array) f2.close() #判断是否是png和jpg图片 def judge_is_png_or_jpg(file_name): suffix = os.path.splitext(file_name)[1] if suffix== '.png' or suffix == '.jpg' or suffix == '.PNG' or suffix == '.JPG': return True else: return False
2.修改cocos2dx引擎
我们知道,所有的资源文件,都要先从磁盘读到内存。我们要修改的就是读取的这个过程。打开cocos2dx引擎源码文件CCImage.cpp,找到initWithImageFile(const std::string& path)这个方法。修改前的源代码如下:bool Image::initWithImageFile(const std::string& path) { bool ret = false; _filePath = FileUtils::getInstance()->fullPathForFilename(path); Data data = FileUtils::getInstance()->getDataFromFile(_filePath); if (!data.isNull()) { auto bin = data.getBytes(); auto size = data.getSize(); ret = initWithImageData(data.getBytes() + encrypt_head_lengh, data.getSize()); } return ret; }
这个方法是读取图片文件,然后获取byte数据流,并用这些数据初始化形成图片,现在我们要做的就是在获取到这些二进制数据之后,进行解密。
1. 获取前三个字节,如果是加密标识的话,表明这个图片已经进行了,加密处理,在此则要进行解密处理。
2. 取第四个字节,也就是对当前这个图片进行加密时的密钥,拿这个密钥进行解密。
3. 用解密后的字节流初始化形成图片。
修改后的源代码如下:
bool Image::initWithImageFile(const std::string& path) { bool ret = false; _filePath = FileUtils::getInstance()->fullPathForFilename(path); Data data = FileUtils::getInstance()->getDataFromFile(_filePath); if (!data.isNull()) { auto bin = data.getBytes(); auto size = data.getSize(); unsigned char have_encrypt_first_flag = *bin; unsigned char have_encrypt_second_flag = *(bin + 1); unsigned char have_encrypt_third_flag = *(bin + 2); unsigned char encrypt_key = 0; unsigned int encrypt_head_flag_lengh = 3; unsigned int encrypt_head_lengh = 4; if (have_encrypt_first_flag == 0x12 && have_encrypt_second_flag == 0x34 && have_encrypt_third_flag == 0x56) { encrypt_key = *(bin + encrypt_head_flag_lengh); auto *cur = bin + encrypt_head_lengh; auto count = size - encrypt_head_lengh; while (count) { *cur ^= encrypt_key; ++cur; --count; } ret = initWithImageData(data.getBytes() + encrypt_head_lengh, data.getSize()); } else { ret = initWithImageData(data.getBytes(), data.getSize()); } } return ret; }
PS:小知识
int a = 0x1121; int b = 0x21; int c = a^b; 再用c^b会发现等于a。
相关文章推荐
- 使用python对cocos2dx的手游图片资源进行加密
- cocos2dx 3.0 使用TexturePacker对图片资源加密
- 使用DES对资源进行加密解密
- cocos2dx-lua or quick 2dx 中的图片资源加密
- 【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】
- 使用python进行图像处理-调整图片大小
- Cocos2d-x—使用TexturePacker对图片进行打包、加密
- 使用ImageMagick进行图片缩放、合成与裁剪(js+python)
- Cocos2d-x—使用TexturePacker对图片进行打包、加密
- python使用M2Crypto模块进行AES加密
- 使用python进行图像处理-调整图片大小
- 使用python进行加密解密AES算法
- Cocos2d-x—使用TexturePacker对图片进行打包、加密
- Cocos2d-x笔记(六)—使用TexturePacker对图片进行打包、加密
- 【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】
- 使用Python进行AES加密和解密的示例代码
- Python中使用hashlib库进行加密
- Cocos2d-x—使用TexturePacker对图片进行打包、加密
- Cocos2d-x—使用TexturePacker对图片进行打包、加密
- 【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】