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)

This entry was posted in OOP, strings and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*