webgame开发中的文件加密

一般的webgame中都会对资源、消息进行加密,这里只是简单记录一下对文件的加密过程。

上图为实际项目中所使用的加密工具(较为简单的一个air项目)

输入加密key +  需要加密的文件  --> 加密 –> 将加密后的文件保存至另一目录(后缀名视自己的项目的规则进行修改)

实现步骤:

1、读取文件(flash.filesystem.File),获取文件流(flash.filesystem.FileStream),获取文件的二进制流(flash.util.ByteArray)

2、对二进制数据进行加密(混淆一下)

3、保存加密后的二进制数据

1: var file:File = new File(path);

       2: //file.isDirectory == false && file.exists

       3:  

       4: var fs:FileStream = new FileStream();

       5: var bytes:ByteArray = new ByteArray();

       6: fs.open(file, FileMode.READ);   //只读模式

       7: fs.position = 0;

       8: fs.readBytes(bytes, 0, fs.bytesAvailable);

       9: fs.close();

      10:  

      11:  

      12: var tempFileName:String = "xxx"; //要保存的文件完整路径

      13: var tempFile:File = new File(tempFileName);

      14: if (tempFile.exists)

      15: {

      16:     tempFile.deleteFile();

      17:     

      18:     tempFile =  new File(tempFileName); 

      19: }

      20:  

      21: var tempFS:FileStream = new FileStream();

      22: tempFS.open(tempFile, FileMode.WRITE);

      23: tempFS.writeBytes(encrypt(bytes));   //加密数据

      24: tempFS.close();

      25:  

      26:  

      27: //encrypt..

      28: var pos:int = 0;

      29: var outByteArray:ByteArray = new ByteArray();

      30: var key:String = StringUtil.trim(keyTxt.text);

      31:  

      32: for (var i:int = 0, len:int = byte.length; i < len; i++)

      33: {

      34:    //todo...

      35:     

      36:     outByteArray.writeByte(byte[i] + //.....);

      37: }

      38:  

      39: return outByteArray;

主要的示例代码(加密方法已被隐去>_<):

1: <?xml version="1.0" encoding="utf-8"?>

       2: <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"

       3:          horizontalScrollPolicy="off" verticalScrollPolicy="off">

       4:     

       5:     <mx:Script>

       6:         <![CDATA[

       7:             import mx.controls.Alert;

       8:             import mx.utils.StringUtil;

       9:             

      10:             private var filePath:String;

      11:             private var directoryPath:String;

      12:             

      13:             private function encryptHandler():void

      14:             {

      15:                 if (!filePath && !directoryPath)

      16:                 {

      17:                     Alert.show("请先选择要加密的文件或目录", "错误");

      18:                     return ;

      19:                 }

      20:                 

      21:                 var key:String = StringUtil.trim(keyTxt.text);

      22:                 

      23:                 if (key == "")

      24:                 {

      25:                     Alert.show("加密KEY不能为空", "错误");

      26:                     return ;

      27:                 }

      28:                 

      29:                 encryptBtn.enabled = false;

      30:                 

      31:                 if (!(!filePath))

      32:                 {

      33:                     encryptFileHandler(filePath);

      34:                     

      35:                     Alert.show("文件“"+filePath+"”加密完成", "温馨提示");

      36:                 }

      37:                 else if (!(!directoryPath))

      38:                 {

      39:                     encryptDirectoryHandler(directoryPath);

      40:                     

      41:                     Alert.show("目录“"+directoryPath+"”文件加密完成", "温馨提示");

      42:                 }

      43:                 

      44:                 outputTxt.validateNow();

      45:                 

      46:                 encryptBtn.enabled = true;

      47:             }

      48:             

      49:             private function getTypeFilter():FileFilter

      50:             {

      51:                 var str:String = "*.swf;*.jpg;*.png;*.xml;*.zip";

      52:                 

      53:                 var filter:FileFilter = new FileFilter("资源("+str+")", str);

      54:                 

      55:                 return filter;

      56:             }

      57:             

      58:             private function selectFileHandler():void

      59:             {

      60:                 var file:File = new File();

      61:                 file.addEventListener(Event.SELECT, selectFileCallback);

      62:                 file.browseForOpen("请选择一个文件", [getTypeFilter()]);

      63:             }

      64:             

      65:             private function selectFileCallback(evt:Event):void

      66:             {

      67:                 clear();

      68:                 

      69:                 var file:File = File(evt.target);

      70:                 file.removeEventListener(Event.SELECT, selectFileCallback);

      71:                 

      72:                 filePath = file.nativePath;

      73:                 

      74:                 inputTxt.htmlText = "选择的文件路径:" + filePath;

      75:             }

      76:             

      77:             private function selectDirectoryHandler():void

      78:             {

      79:                 var file:File = new File();

      80:                 file.addEventListener(Event.SELECT, selectDirectoryCallback);

      81:                 file.browseForDirectory("请选择一个目录");

      82:             }

      83:             

      84:             private function selectDirectoryCallback(evt:Event):void

      85:             {

      86:                 clear();

      87:                 

      88:                 directoryPath = File(evt.target).nativePath;

      89:                 

      90:                 inputTxt.htmlText = "选择的目录路径:" + directoryPath;

      91:                 

      92:                 File(evt.target).removeEventListener(Event.SELECT, selectDirectoryCallback);

      93:             }

      94:             

      95:             private function getEncryptSuffix(fileType:String):String

      96:             {

      97:                 var typeConfig:Object = {

      98:                     'swf' : 's',

      99:                     'jpg' : 'j',

     100:                     'png' : 'p',

     101:                     'xml' : 'x',

     102:                     'zip' : 'z'

     103:                 };

     104:                 

     105:                 if (!typeConfig[fileType])

     106:                 {

     107:                     return fileType;

     108:                 }

     109:                 

     110:                 return typeConfig[fileType];

     111:             }

     112:             

     113:             private function clear():void

     114:             {

     115:                 inputTxt.htmlText = "";

     116:                 outputTxt.htmlText = "";

     117:                 

     118:                 filePath = null;

     119:                 directoryPath = null;

     120:             }

     121:             

     122:             private function encryptFileHandler(path:String):void

     123:             {

     124:                 var file:File = new File(path);

     125:                 

     126:                 if (file.isDirectory == false && file.exists)

     127:                 {

     128:                     maskPanel.visible = true;

     129:                     

     130:                     var fs:FileStream = new FileStream();

     131:                     var bytes:ByteArray = new ByteArray();

     132:                     

     133:                     fs.open(file, FileMode.READ);

     134:                     fs.position = 0;

     135:                     fs.readBytes(bytes, 0, fs.bytesAvailable);

     136:                     fs.close();

     137:                     

     138:                     loadFileCompleteHandler(file, bytes);

     139:                     

     140:                     maskPanel.visible = false;

     141:                 }

     142:                 else

     143:                 {

     144:                     outputTxt.htmlText += "<br>【error】" + path + "不是一个正确的文件路径!";

     145:                 }

     146:             }

     147:             

     148:             private function encryptDirectoryHandler(path:String):void

     149:             {

     150:                 var file:File = new File(path);

     151:                 

     152:                 if (file.exists && file.isDirectory)

     153:                 {

     154:                     var fileList:Array = file.getDirectoryListing();

     155:                     var typeArr:Array = ["swf", "zip", "xml", "jpg", "png"];

     156:                     

     157:                     for (var i:int = 0, len:int = fileList.length; i < len; i++)

     158:                     {

     159:                         var tempFile:File = File(fileList[i]);

     160:                         var tempFilePath:String = tempFile.nativePath;

     161:                         

     162:                         if (tempFile.isDirectory == false)

     163:                         {

     164:                             if (typeArr.indexOf(tempFile.extension) > -1)

     165:                             {

     166:                                 encryptFileHandler(tempFilePath);    

     167:                             }

     168:                             else

     169:                             {

     170:                                 outputTxt.htmlText += "<br><font color='#ff0000'>【skip】跳过文件:"+tempFilePath+"</font>";                        

     171:                             }

     172:                         }

     173:                         else if (tempFile.name != ".svn")

     174:                         {

     175:                             encryptDirectoryHandler(tempFilePath);

     176:                         }

     177:                     }

     178:                 }

     179:                 else

     180:                 {

     181:                     outputTxt.htmlText += "<br>【error】" + path + "不是一个正确的目录路径!";

     182:                 }

     183:             }

     184:             

     185:             private function loadFileCompleteHandler(file:File, bytes:ByteArray):void

     186:             {

     187:                 outputTxt.htmlText += "<br>开始加密文件" + file.nativePath;

     188:                 

     189:                 var tempFileName:String = file.parent.nativePath + "\\" + file.name.replace(new RegExp(file.extension + "$"), "") + getEncryptSuffix(file.extension);

     190:                 

     191:                 tempFileName = tempFileName.replace(/\\abc\\/, '\\encrypt_abc\\abc\\');

     192:                 

     193:                 var tempFile:File = new File(tempFileName);

     194:                 

     195:                 if (tempFile.exists)

     196:                 {

     197:                     tempFile.deleteFile();

     198:                     

     199:                     tempFile =  new File(tempFileName); 

     200:                 }

     201:                 

     202:                 var tempFS:FileStream = new FileStream();

     203:                 tempFS.open(tempFile, FileMode.WRITE);

     204:                 tempFS.writeBytes(encrypt(bytes));

     205:                 tempFS.close();

     206:             }

     207:             

     208:             private function encrypt(byte:ByteArray):ByteArray

     209:             {

     210:                 var pos:int = 0;

     211:                 var outByteArray:ByteArray = new ByteArray();

     212:                 var key:String = StringUtil.trim(keyTxt.text);

     213:                 //todo...               

     214:                 

     215:                 return outByteArray;

     216:             }

     217:             

     218:         ]]>

     219:     </mx:Script>

     220:     

     221:     <mx:VBox width="100%" height="100%" 

     222:              horizontalScrollPolicy="off" verticalScrollPolicy="off"

     223:              paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10">

     224:         

     225:         <mx:HBox width="100%" height="50" horizontalScrollPolicy="off" verticalScrollPolicy="off"

     226:                  verticalAlign="middle">

     227:             

     228:             <mx:Label text="加密KEY" />

     229:             <mx:TextInput  id="keyTxt" width="300" text="abc123" />

     230:             <mx:Button label="加密" id="encryptBtn" click="encryptHandler()" />

     231:             <mx:Spacer width="100%" />

     232:             

     233:             <mx:Button id="selectFileBtn" click="selectFileHandler()" label="选择文件" />

     234:             <mx:Button id="selectDirectoryBtn" click="selectDirectoryHandler()" label="选择文件夹" />

     235:             

     236:         </mx:HBox>

     237:         

     238:         <mx:HBox width="100%" height="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off">

     239:             

     240:             <mx:TextArea id="inputTxt" width="100%" height="480" leading="5" letterSpacing="1"

     241:                          borderColor="#eeeeee" borderStyle="solid" 

     242:                          paddingTop="5" paddingRight="5" paddingLeft="5" paddingBottom="5" />

     243:             

     244:             <mx:TextArea id="outputTxt" width="100%" height="480" leading="5" letterSpacing="1"

     245:                          borderColor="#eeeeee" borderStyle="solid" 

     246:                          verticalScrollPolicy="auto"

     247:                          paddingTop="5" paddingRight="5" paddingLeft="5" paddingBottom="5" />

     248:             

     249:         </mx:HBox>

     250:     

     251:     </mx:VBox>

     252:     

     253:     <mx:Canvas width="100%" height="100%" backgroundColor="#000000" backgroundAlpha=".3" visible="false" id="maskPanel">

     254:         <mx:HBox width="130" height="50" horizontalCenter="0" verticalCenter="0" 

     255:                  backgroundColor="#ffffff" verticalAlign="middle" horizontalAlign="center">

     256:             <mx:Text horizontalCenter="0" verticalCenter="0" text="正在处理..." fontSize="16" fontWeight="bold" color="#3399cc"  />            

     257:         </mx:HBox>

     258:     </mx:Canvas>

     259:     

     260: </mx:Canvas>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏pangguoming

Java 解析Excel文件为JSON

Excel转Json的需求 反正我对SSM基本不会的情况下来到现在这家公司,都是90后,感觉很好。第二天就给我开发任务,就是把用户上传的Excel文件转成JSO...

5955
来自专栏Java技术分享

Servlet是如何实现MVC的?

Servlet是一种服务器端的编程语言,是J2EE中比较关键的组成部分,Servlet技术的推出,扩展了Java语言在服务器端开发的功能,巩固了Java语言在服...

2658
来自专栏闻道于事

JavaWeb(五)Filter过滤器

Filter过滤器 Fileter介绍 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理...

4826
来自专栏xingoo, 一个梦想做发明家的程序员

【手把手教你全文检索】Apache Lucene初探

PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语。那小博猪就简单介绍一下这一周的学习历程,仅供各位程...

26610
来自专栏Java与Android技术栈

RxJava 线程模型分析

RxJava的被观察者在使用操作符时可以利用线程调度器--Scheduler来切换线程,例如

3172
来自专栏java技术学习之道

Spring AOP原理分析一次看懂

1734
来自专栏一枝花算不算浪漫

[javaweb]Java过滤器与包装设计模式的实用案例.

4039
来自专栏小鄧子的技术博客专栏

All RxJava - 为Retrofit添加重试

在我们的日常开发中离不开I/O操作,尤其是网络请求,但并不是所有的请求都是可信赖的,因此我们必须为APP添加请求重试功能。

3141
来自专栏Jaycekon

深入浅出Redis-Spring整合Redis

概述:    在之前的博客中,有提到过Redis 在服务端的一些相关知识,今天主要讲一下Java 整合Redis的相关内容。    下面是Jedis 的相关依赖...

3679
来自专栏jeremy的技术点滴

《Network Programming with Go》阅读重点备忘(一)

3907

扫码关注云+社区

领取腾讯云代金券