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; } }
CoderGames.com - casual and non-casual games for PC computer
December 1, 2010
Useful to know, thanks!
cambiatablog
December 1, 2010
You’re welcome! / Jonas
anders
December 23, 2010
seems good althought the class file is no longer online
http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/
anyone got it ?
alsprog
February 10, 2011
here You can download full package:
http://www.laziomatica.com/web/wp-content/uploads/2009/01/flashcrypt.zip
Sebastian Salzgeber
September 23, 2011
Is this a secure way to exchange Client-Data with Server-Data or is it exploitable?
nresnas
March 16, 2012
Thank you very much It’s very useful
be.boulevart.as3.security.Base64 — works! !!!!!!!!!!!!!!!!!!!!!!!!!!!
———————————————————————————————-
mx.utils.Base64Encoder — wrong result!!!!!!!!!!!!!!
com.dynamicflash.util.Base64 — wrong result !!!!!!!!!!!!!!!!!!!!!!!
com.hurlant.util.Base64 — wrong result!!!!!!!!!!!!!!!!!!!!!!!!
Claudio
September 9, 2012
Thank you very much for sharing!
I’ve tried many AS3/PHP scripts but these are the only one that actually work!
GuyOxford
December 16, 2012
Thanks for posting this. All very new to me so I don’t know how to install the package alsprog posted in FlashBuilder. Can anyone help?
Luxman Wickramasinghe
January 21, 2013
I didn’t bother installing base64. Instead I created a new actionsctipt class (right-click on src folder, select new-> action script class), copied the content and pasted into the class. removed the text on top leaving package only. it worked.
BobsMyUncle
September 7, 2013
The code is old school and may require some minor updates. However, the implementation is beautifully simple and for old school Macromedia developers that dislike the flex people moving into as3 (or just flash ide type people), the code will find a new home. I much prefer it over, as3core and as3crypt. ps the zip from laziomatica.com, the as files in the com folder are no good. Instead use the be.boulevart like the post does. This is my favorite as3 encryption library.