当前位置:网站首页>File decryption in webgame development

File decryption in webgame development

2022-06-26 11:56:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

The last one is about file encryption , This article records how to load and display pictures .

Because the picture file is encrypted , Has become a binary stream file ( A lot of crap ), Only use URLStream To load resources .

There is one caveat here :URLStream The read operation in is non blocking , Must be used before reading data bytesAvailable Property to determine whether enough data can be obtained , How not to get enough data , Will lead to EOFError abnormal .

Main idea :

1、 Load encrypted resources , Use URLStream

2、 Call decryption method , Get the decrypted data

3、 Use Loader Class loadBytes Method load resource

   1: var urlLoader:URLStream = new URLStream();
   2: var url:String = getEncryptURL(url);    // Through the resources that need to be loaded url, To get the address of the resource that needs to be loaded , Such as xx.png Actual will request xx.p
   3:  
   4: // monitor urlLoader Events 
   5: //Event.Complete、IOErrorEvent.IO_ERROR、ProgressEvent.PROGRESS、HTTPStatusEvent.HTTP_STATUS、Event.OPEN
   6:  
   7: var req:URLRequest = new URLRequest(url);
   8:  
   9: urlLoadedCount = 0;
  10:  
  11: urlLoader.load(req);
  12:  
  13:  
  14:  
  15:  
  16:  
  17: //onCompleteHandler In the method 
  18: var byte:ByteArray;
  19: var byteLen:int;
  20:  
  21: try 
  22: {
  23:     byteLen = urlLoader.bytesAvailable;
  24: }
  25: catch(e:Error)
  26: {
  27:     if (urlLoadedCount++ > 3)
  28:     {
  29:         return ;
  30:     }
  31:     
  32:     //reload  Reload , Until more than 3 Time 
  33:     
  34:     return ;
  35: }
  36:  
  37: urlLoadedCount = 0;
  38:  
  39: byte = new ByteArray();
  40: urlLoader.readBytes(byte, 0, byteLen);
  41: byte = EncryptUtils.DeEncrypt(byte); // Decrypt the data , It corresponds to the encryption method in the previous article 
  42: byte.position = 0;
  43:  
  44: // Start loading pictures 
  45: var loader = new Loader();
  46:  
  47: // monitor Event.Complete event 
  48: loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadImgCompleteHandler);
  49:  
  50: var context:LoaderContext = new LoaderContext();
  51: context.applicationDomain = ApplicationDomain.currentDomain;
  52: context.securityDomain = SecurityDomain.currentDomain;
  53:  
  54: loader.loadBytes(byte, context);
  55:  
  56:  
  57: //loadImgCompleteHandler Method 
  58:  here loader.content Have been to Bitmap The class , Use addChild You can finish loading the picture 
  59:  
  60:  
  61: // If you want to load xml、txt And so on , This method is not required , Use byte.readUTFBytes(byte.bytesAvailable) You can get the decrypted string 
  62:  

More reference :

flash.display.Loader>>

flash.display.Bitmap>>

flash.net.URLStream>>

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/133958.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261150531352.html