Simple encrypt/decrypt in AS3 and Php – Base64 blues

Posted on August 24, 2010

10


Needed the possibility to handle simple string encryption/decryption the other day — handling the same data in both php and As3. Just a little bit of key salting, and some Base64 encryption.
No problem except for the Base64 part. When trying this out, only one of my Base64 libraries gave the same result as the php base64_encode/base64_decode functions:

  • mx.utils.Base64Encoder — wrong result
  • com.dynamicflash.util.Base64 — wrong result
  • com.hurlant.util.Base64 — wrong result
  • be.boulevart.as3.security.Base64 — works! 🙂

Haven’t had time to dive into it yet, but there seems to be different interpretations of the more obscure character codes, causing the different results.

The be.boulevart.as3.security.Base64 class is developed by Sven Dens (modelled after an Aardwulf Systems solution) and can be found here.

So here are my simple enctyption solutions:

Simplecrypt.as

package se.cambiata.utils.crypt {
	
	// The Base64 class is developed by Sven Dens, and can be found here: 
	// http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/
	import be.boulevart.as3.security.Base64;
	
	public class Simplecrypt {
		static public function encrypt(str:String, key:String = '%key&'):String {
			var result:String = '';
			for (var i:int = 0; i < str.length; i++) {
				var char:String = str.substr(i, 1);
				var keychar:String = key.substr((i % key.length) - 1, 1);
				var ordChar:int = char.charCodeAt(0);
				var ordKeychar:int = keychar.charCodeAt(0);
				var sum:int = ordChar + ordKeychar;
				char = String.fromCharCode(sum);
				result = result + char;
			}
			return Base64.encode(result);
		}

		static public function decrypt(str:String, key:String = '%key&'):String {
			var result:String = '';
			var str:String = Base64.decode(str);
			
			for (var i:int = 0; i < str.length; i++) {
				var char:String = str.substr(i, 1);
				var keychar:String = key.substr((i % key.length) - 1, 1);
				var ordChar:int = char.charCodeAt(0);
				var ordKeychar:int = keychar.charCodeAt(0);
				var sum:int = ordChar - ordKeychar;
				char = String.fromCharCode(sum);
				result = result + char;
			}
			return result;
		}
	}
}

Simplecrypt.php

<?php
class Simplecrypt {

	function encrypt($string, $key='%key&') {
		$result = '';
		for($i=0; $i<strlen($string); $i++) {
			$char = substr($string, $i, 1);
			$keychar = substr($key, ($i % strlen($key))-1, 1);
			$ordChar = ord($char);
			$ordKeychar = ord($keychar);
			$sum = $ordChar + $ordKeychar;
			$char = chr($sum);
			$result.=$char;
		}
		return base64_encode($result);
	}

	function decrypt($string, $key='%key&') {
		$result = '';
		$string = base64_decode($string);
		for($i=0; $i<strlen($string); $i++) {
			$char = substr($string, $i, 1);
			$keychar = substr($key, ($i % strlen($key))-1, 1);
			$ordChar = ord($char);
			$ordKeychar = ord($keychar);
			$sum = $ordChar - $ordKeychar;
			$char = chr($sum);
			$result.=$char;
		}
		return $result;
	}

}
Advertisements
Posted in: Actionscript 3, PHP