+ Reply to Thread
Results 1 to 4 of 4

Thread: Trying to convert this Actionscript function to PHP

  1. #1
    ryanmm is offline x10Hosting Member ryanmm is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    36

    Trying to convert this Actionscript function to PHP

    I'm trying to create a php version of this actionscript code because I need to encrypt and decrypt data between flash and php. I got almost halfway through it, but the part about creating 6 bit numbers from 8 bit binary confused me a bit.

    Code:
    //original Actionscript
    String.prototype.sixBitEncode = function () {   
        var ntexto = "";   
        var nntexto = "";   
        var codeKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"   
        var charCode, charCodeBin, charChar;   
        for (i=0; i< this.length; i++) {   
            charCode = this.charCodeAt(i) & 255; // char code   
            charCodeBin = ("00000000" + charCode.toString(2)).substr(-8,8); // char code in binary   
            ntexto += charCodeBin;   
        }   
        for (i=0; i< ntexto.length; i+=6) {   
            charCodeBin = ntexto.substr(i, 6); // char code in binary, 6 bits   
            if (charCodeBin.length < 6) charCodeBin = (charCodeBin+"000000").substr(0,6);   
            charCode = parseInt(charCodeBin, 2);   
            nntexto += codeKey.substr(charCode, 1);   
        }   
        return (nntexto);   
    }
    
    
    
    String.prototype.sixBitDecode = function () {   
        var ntexto = "";   
        var nntexto = "";   
        var codeKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"   
        var charCode, charCodeBin;   
        for (i=0; i<this.length; i++) {   
            charCode = codeKey.indexOf(this.substr(i,1)); // char index   
            charCodeBin = ("000000" + charCode.toString(2)).substr(-6,6); // char index in binary, 6 bits   
            ntexto += charCodeBin;   
        }   
        for (i=0; i< ntexto.length; i+=8) {   
            charCodeBin = ntexto.substr(i, 8); // char code in binary   
            charCode = parseInt(charCodeBin, 2);   
            charChar = String.fromCharCode(charCode);   
            nntexto += charChar;   
        }   
        return (nntexto);   
    }
    PHP Code:
    //my php conversion
    function sixBitEncode($msg
    {   
        
    $ntexto '';   
        
    $nntexto '';   
        
    $codeKey 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';   
        
    $charCode
        
    $charCodeBin
        
    $charChar;   
        for (
    $i=0$i strlen($msg); $i++) 
        {   
                            
    //Break string into string char codes:
               //70 114 233 115 104 105
            
    $charCode substr($string$i1);
            
    $charCode ord($charCode);
            
    //Transform this into 8-bit binary numbers:
            //01000110 01110010 11101001 01110011 01101000 01101001
            
    $charCodeBin decbin($charCode);   
            
    //Concatenate the values:
               //010001100111001011101001011100110110100001101001
               
    $ntexto += $charCodeBin;   
        }   
        for (
    $i=0$i strlen(ntexto); $i+=6
        {   
            
    //Break it into groups of 6 bits, completing with zeros if needed:
               //010001 100111 001011 101001 011100 110110 100001 101001
            
    $charCodeBin $ntexto.substr($i6); // char code in binary, 6 bits   
            
    if ($charCodeBin.length 6$charCodeBin = ($charCodeBin+"000000").substr(0,6);   
            
    //Create new char codes from it:
               //17 39 11 41 28 54 33 41
            //ord()
            
    $charCode parseInt($charCodeBin2);
            
    //Re-encode it using a defined, internal table, and output it:
               //RnLpc2hp
            
    $nntexto += $codeKey.substr($charCode1);   
        }   
        return (
    $nntexto);   

    Last edited by ryanmm; 03-06-2011 at 10:28 PM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Trying to convert this Actionscript function to PHP

    base64_encode( $string )

    PHP already has a function that does 97% of the work.
    It uses / and + as the 63 and 64th symbol instead of - and _ . It also pads the result with =
    You can use strtr to adjust those differences.
    Nothing is always absolutely so.

  3. #3
    ryanmm is offline x10Hosting Member ryanmm is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    36

    Re: Trying to convert this Actionscript function to PHP

    Well, would that function work the EXACT same way as the actionscript does? you say it uses different symbols, that would probably be a problem. It needs to be the exact same in both PHP and actionscript or else it wont work, obviously.

    ---------- Post added at 06:23 PM ---------- Previous post was at 06:20 PM ----------

    I think I really just need to understand the following two lines:
    Code:
    //Break string into string char codes: 
    //70 114 233 115 104 105 
    charCode = this.charCodeAt(i) & 255;
    //Transform this into 8-bit binary numbers: 
    //01000110 01110010 11101001 01110011 01101000 01101001
    charCodeBin = ("00000000" + charCode.toString(2)).substr(-8,8);
    this seems like a weird integer to binary function, dont most function use %2 recursively until the product is = 1, and then loop back through?

    And I have no idea what the '&' is being used for in the first line here, 'this.charCodeAt(i) & 255' ???
    Last edited by ryanmm; 03-07-2011 at 12:25 PM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Trying to convert this Actionscript function to PHP

    Quote Originally Posted by ryanmm View Post
    Well, would that function work the EXACT same way as the actionscript does? you say it uses different symbols, that would probably be a problem. It needs to be the exact same in both PHP and actionscript or else it wont work, obviously.
    As Descalzo says, the scheme is the same as Base64 with the exceptions he notes. Replace the "+" and "/" and drop the '=' from the Base64 encoded data and you have the eight-to-six encoding in the Actionscript code. Better yet, replace the custom encoding with Base64 in the Actionscript, if possible; there are plenty of AS implementations online.


    Quote Originally Posted by ryanmm View Post
    Code:
    //Break string into string char codes: 
    //70 114 233 115 104 105 
    charCode = this.charCodeAt(i) & 255;
    [...]
    And I have no idea what the '&' is being used for in the first line here, 'this.charCodeAt(i) & 255' ???
    It gets around character encoding & sign issues by stripping higher bits. If String treats characters as octets, it's unnecessary (since the output of charCodeAt will be a byte), and if it treats characters as code points, it's wrong (e.g. "\u2264" will become "\x64" ("d") rather than "≤" ). If characters were signed (so char code 0xF7 < 0 or 0xFFFD < 0), it would convert them to unsigned, but (according to Adobe's documents), they're not.

    Quote Originally Posted by ryanmm View Post
    Code:
    //Transform this into 8-bit binary numbers: 
    //01000110 01110010 11101001 01110011 01101000 01101001
    charCodeBin = ("00000000" + charCode.toString(2)).substr(-8,8);
    this seems like a weird integer to binary function, dont most function use %2 recursively until the product is = 1, and then loop back through?
    It's inefficient and simpler than working with each individual bit, but not too weird. More efficient would be to use bitwise operations:
    Code:
    /*
      Convert from 3 eight-bit numbers to 4 six-bit numbers.
      Preconditions: 
        * octets.length == 2
        * abs(octets[i]) < 0x100, for i in 0..2
      Postcondition: 
        * result.length == 3
        * abs(result[i]) < 0x40, for i in 0..3
     */
    function eightToSix(octets) {
        return [
            (octets[0] >> 2) & 0x3F,
            ((octets[0] << 4) & 0x30) | ((octets[1] >> 4) & 0xF),
            ((octets[1] << 2) & 0x3C) | ((octets[2] >> 6) & 0x3),
            octets[2] &0x3F
        ];
    }
    
    /* Inverse of 'eightToSix' */
    function sixToEight(sextets) {
        return [
    	(sextets[0] << 2) | (sextets[1] >> 4),
    	((sextets[1] << 4) & 0xF0) | (sextets[2] >> 2),
    	((sextets[2] << 6) & 0xC0) | sextets[3],
        ];
    }
    Last edited by misson; 03-08-2011 at 03:48 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. actionscript + XML
    By rvr1982 in forum Programming Help
    Replies: 4
    Last Post: 10-14-2009, 12:27 PM
  2. ActionScript 3
    By Wolfhaze in forum Programming Help
    Replies: 1
    Last Post: 02-17-2009, 06:06 PM
  3. I need help with actionscript 3.0
    By sdc151 in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 05-06-2008, 12:29 PM
  4. PHP - Actionscript
    By manila in forum Introductions
    Replies: 1
    Last Post: 12-25-2007, 01:45 AM
  5. actionscript help!
    By Joker Boy in forum Off Topic
    Replies: 3
    Last Post: 11-27-2006, 11:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers