Category Archives: OOP

Crackable String Encoding

Actionscript:
  1. var encode:Object = new Object();
  2. var decode:Object = new Object();
  3. var a:Array = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
  4.  
  5. // change this for different encoded results
  6. var offset:int = 10;
  7.  
  8. for (var i:int = 0; i<a.length; i++){
  9.     var index:int = (a.length - i - offset) % a.length;
  10.     encode[a[i]] = a[index];
  11.     decode[a[index]] = a[i];
  12. }
  13.  
  14. function encodeString(str:String):String{
  15.     return map(str, encode);
  16. }
  17.  
  18. function decodeString(str:String):String{
  19.     return map(str, decode);
  20. }
  21.  
  22. function map(str:String, smode:Object):String{
  23.     var n:String = "";
  24.     for (var i:int = 0; i<str.length; i++){
  25.         var char:String = str.charAt(i);
  26.         var en:String = smode[char];
  27.         if (en){
  28.             n += en;
  29.         }else{
  30.             n += char;
  31.         }
  32.     }
  33.     return n;
  34. }
  35.  
  36. // test out the functions
  37.  
  38. var input:String = "This is a regular string";
  39.  
  40. trace(input);
  41.  
  42. var encoded:String = encodeString(input);
  43.  
  44. trace("encoded: ", encoded);
  45.  
  46. trace("decoded: ",decodeString(encoded));
  47.  
  48. /*
  49. outputs:
  50.  
  51. This is a regular string
  52. encoded:  gSRH1RH1Z1IVTFOZI1HGIRMT
  53. decoded:  This is a regular string
  54.  
  55. */

The above demos an intentionally simple string encoding technique.

This is a technique I use if I need to encode strings but don't care if someone figures out what the string value actually is. For me, this is more common than needing hard/impossible to crack string encoding algorithms. A good example is an e-card... a url for an ecard could look like this:

www.birthdaycardthing.com/?name=joe&age=32

or it could look like this:

www.birthdaycardthing.com/?i=brx&x=5p

I wrapped this snippet up into a class and made a few minor tweaks. The class is called SimpleCipher it has two static methods and one static property...

Actionscript:
  1. import com.actionsnippet.utils.SimpleCipher;
  2. // same as offset above, but can be set at any time
  3. // new encode and decode Objects will be calculated
  4. SimpleCipher.offset = 1;
  5. var input:String = "SimpleCipher encoding and decoding";
  6.              
  7. trace("input: ", input);
  8.              
  9. var encoded:String = SimpleCipher.encode(input);
  10.              
  11. trace("encoded: ", encoded);
  12. trace("decoded: ", SimpleCipher.decode(encoded));
  13. /*
  14. outputs:
  15. input:  SimpleCipher encoding and decoding
  16. encoded:  RhlokdBhogdq0dmbnchmf0 mc0cdbnchmf
  17. decoded:  SimpleCipher encoding and decoding
  18. */

Download SimpleCipher Class
(this class uses a static initializer... see yesterdays post)

Also posted in strings | Tagged , | Leave a comment

Static Constructor / Initializer

Actionscript:
  1. package{
  2.    
  3.     public class Lookup{
  4.        
  5.         private static var _random:Array;
  6.         // static initializer
  7.         {
  8.             trace("I am a static initializer for " + Lookup);
  9.             init();
  10.         }
  11.        
  12.         private static function init():void{
  13.              _random = new Array();
  14.              // generate some filler values
  15.              for (var i:int = 0; i<100; i++){
  16.                 _random.push(Math.random() * i);
  17.              }
  18.         }
  19.        
  20.         public static function get random():Array{
  21.             return _random.concat();
  22.         }
  23.     }
  24. }

...and to test out the above class:

Actionscript:
  1. package {
  2.     import flash.display.Sprite;
  3.  
  4.     public class Main extends Sprite{
  5.         public function Main(){
  6.            trace(Lookup.random[50]);
  7.         }
  8.     }
  9. }
  10. /*
  11. outputs:
  12. I am a static initializer for [class Lookup]
  13. 5.4258062043227255
  14. */

The above shows how to use static initializers. I've found this to be very handy... in this case we create an array of random values....

Tomorrow I'll actually post a real class where I make use of this... the Lookup class in this case is really just filler code....

Posted in OOP | Tagged , | 1 Comment

Snippet Template (imports)

Actionscript:
  1. package {
  2.     import adobe.utils.*;
  3.     import flash.accessibility.*;
  4.     import flash.display.*;
  5.     import flash.errors.*;
  6.     import flash.events.*;
  7.     import flash.external.*;
  8.     import flash.filters.*;
  9.     import flash.geom.*;
  10.     import flash.media.*;
  11.     import flash.net.*;
  12.     import flash.printing.*;
  13.     import flash.profiler.*;
  14.     import flash.sampler.*;
  15.     import flash.system.*;
  16.     import flash.text.*;
  17.     import flash.ui.*;
  18.     import flash.utils.*;
  19.     import flash.xml.*;
  20.    
  21.     dynamic public class Snippet extends MovieClip {
  22.         public function Snippet() {
  23.              // paste your snippet here (functions and all)
  24.         }
  25.     }
  26. }

This snippet imports all flash packages and is dynamic... you can copy actionsnippet code into the constructor of this file if you use Flex, FlashDevelop, TextMate etc... I tested it with a bunch of snippets and it seems to work nicely.

When I first teach classes in AS3 this is the template I use:

Actionscript:
  1. package{
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.      
  5.     public class Main extends Sprite{
  6.         // etc...
  7.     }
  8. }

Display and events cover a lot of ground..... next one I find myself adding is flash.geom, followed by flash.net... I'd say those are my top 4 most frequently used packages.... I do lots of text layout in the Flash IDE, otherwise flash.text would be in there....

Also posted in dynamic, misc, timeline | Tagged , | 4 Comments

Static Stage and More…

Actionscript:
  1. package {
  2.     import flash.display.*;
  3.  
  4.     public class Main extends MovieClip{
  5.    
  6.         private static var _display:MovieClip;
  7.         private static var _stage:Stage;
  8.        
  9.         // use getters instead of public static vars so they
  10.         // cannot be reset outside of Main
  11.         public static function get display():MovieClip{
  12.             return _display;
  13.         }
  14.        
  15.         public static function get stage():Stage{
  16.             return _stage;
  17.         }
  18.        
  19.         public function Main(){
  20.            Main._display = this;
  21.            Main._stage = stage;
  22.            
  23.            // test out the static references
  24.            var t:Test = new Test();
  25.         }
  26.     }
  27. }
  28.  
  29. class Test{
  30.     public function Test(){
  31.         // test out the static references
  32.         with(Main.display.graphics){
  33.             lineStyle(1, 0xFF0000);
  34.             for (var i:int = 0; i<100; i++){
  35.                 lineTo(Math.random() * Main.stage.stageWidth,
  36.                        Math.random() * Main.stage.stageHeight);
  37.             }
  38.         }
  39.     }
  40. }

This snippet creates two private static variables that reference the stage and the main timeline/document class. It then uses getters to regulate the use of these static vars so that they cannot be reset from outside the Main class.

Sometimes the amount of extra coding you need to do to maintain a valid stage reference can be cumbersome... similarly, passing references of the document class all around your app can be annoying. If you don't mind using two global vars in your app... this trick can come in handy.

What's nice about using getters here is that if someone tries to do this:

Actionscript:
  1. Main.display = new MovieClip();

they'll get an error... in flex, you even see that little red ex pop up next to this line of code if you write it :) ... that wouldn't happen with a public static var....

Also posted in DisplayObject, variables | Tagged , , | Leave a comment