Pages

Sunday, January 15, 2012

AS3: Cryptography Methods

Few cryptography methods.
package engine 
{
 /**
  * @file CCryptography.as
  * @author Abhishek Kumar
  */
 
 import com.hurlant.util.Hex;
 import com.hurlant.util.Base64;
 
 public class CCryptography
 {
  
  public function CCryptography() 
  {
   
  }
  
  public static function encrypt(iMsg:String):String
  {
   return Hex.fromString(Base64.encode(XOR(iMsg)));
  }
  
  public static function decrypt(iMsg:String):String
  {
   return XOR(Base64.decode(Hex.toString(iMsg)));
  }
  
  private static function XOR(source:String):String 
  {
      var key:String = "HjRUPxRjRUPxRdVDYFdVDFS0HjRUPxRdVDYF6PkFBK3YlXjc";
      var result:String = new String();
      for (var i:Number = 0; i < source.length; i++) 
      {
   if (i > (key.length - 1)) 
   {
       key += key;
   }
   result += String.fromCharCode(source.charCodeAt(i) ^ key.charCodeAt(i));
      }
      return result;
  }
  
 }

}

No comments:

Post a Comment