1 Reply Latest reply on Dec 3, 2007 11:34 AM by eyewell

    Base 64 Encode/Decode

    jlmoller
      I consider sending clear text problematic when the data ends up as XML information. Converting the text Strings to Base 64 between PHP and Flex 2 (or anything else and Flex 2) eliminates issues with character text like <> and &'s. I've done a lot of XMLRPC coding and always sent text data values that are Base 64 format. The built in Flex 2 Base 64 functions make this easier. On the PHP side, you can use

      $b64StringVal = base64_encode ( $stringData );
      $stringData = base64_decode ( $b64StringVal );

      to convert.

      If you are using Java at th back end, look at:
      http://iharder.sourceforge.net/current/java/base64/

      Definitions in the CDATA area:

      import mx.utils.Base64Encoder;
      import mx.utils.Base64Decoder;
      import flash.utils.ByteArray;

      public function encodeB64(target:String) : String
      {
      var be:Base64Encoder = new Base64Encoder();
      be.encode(target);
      var encodedData:String = be.flush();
      return encodedData;
      }
      public function decodeB64(target:String) : String
      {
      var bd:Base64Decoder = new Base64Decoder();
      bd.decode(target);
      var decodedData:ByteArray = bd.drain();
      return decodedData.toString();
      }

      [Bindable] public var myData:String = new String();

      private function loginResult(evt:ResultEvent):void {
      var b64MyData:String = evt.result.MyData;
      myData=decodeB64(b64MyData);
      }

      In the mxml area:

      <mx:HTTPService id="loginPage" url=" http://localhost/xxx.php" useProxy="false" method="POST" />
      <mx:Model id="loginModel1">
      <root>
      <loginModel>"loginModel1"</loginModel>
      <username>{encodeB64(username.text)}</username>
      <password>{encodeB64(password.text)}</password>
      </root>
      </mx:Model>