专栏首页游戏杂谈webgame开发中的文件加密

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 条评论
登录 后参与评论

相关文章

  • AS3给图片加水印文字

    通过FileReference获取选中的图片,调用它的load方法,将图片存入到内存中。使用Loader(flash.display.Loader)类的loa...

    meteoric
  • xml文件转二进制文件的AIR

    游戏开发中,有很多XML可能是很大的,比如一个任务配置文件,可能就接近2M(当然全部文件放在一个XML里面本身就有问题,比较好的做法就是分等级分隔XML),这...

    meteoric
  • AIR中的文件系统控件

     写AIR的过程中,可能会用到文件系统的相关控件。比如选择某一目录,浏览文件列表等。

    meteoric
  • win10 uwp 使用 asp dotnet core 做图床服务器客户端 服务器端客户端

    本文告诉大家如何在 UWP 做客户端和 asp dotnet core 做服务器端来做一个图床工具

    林德熙
  • 初学者 熟悉基本centos6.5命令 以及 如何搭建SVN+http访问

    5、当执行这个命令的时候 会出现提示 y/n 是否下载。你写上y就可以了。 然后当出现 complete! 这个就安装成功了!(这个截图我就不拿了。)

    斯文的程序
  • 如何部署 svn 服务

    Subversion是一个开源版本控制系统。它可以帮助您跟踪文件和文件夹的集合。每次更改,添加或删除使用Subversion管理的文件或文件夹时,都会将这些更改...

    Mr.Crypto
  • 03 Linux下的SVN服务器搭建

    公司周报采用SVN格式提交,服务器在内网,有一天突发奇想,能不能在个人的云服务器搭建SVN环境,保存一些个人资料,这样不是随时随地都可以访问了么!!!

    lovingwork
  • Apache配置SVN 之后用户认证失败

    在完成所有svn的apache配置值,checkout的时候,仍然报错: (13)Permission denied: Could not open pass...

    luxixing
  • Python 技术篇-判断指定路径下的文件是否处于打开状态或占用状态实例演示

    说明: 如果是打开状态,会报如下错误:PermissionError: [Errno 13] Permission denied: 'C:\\Users\\A...

    小蓝枣
  • svnmanager安装

    apt-get update apt-get install apache2 subversion libapache2-svn apt-get insta...

    苦咖啡

扫码关注云+社区

领取腾讯云代金券