Trying to convert this Actionscript function to PHP

ryanmm

Member
Messages
38
Reaction score
0
Points
6
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:
//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, $i, 1);
		$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($i, 6); // 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($charCodeBin, 2);
		//Re-encode it using a defined, internal table, and output it:
   		//RnLpc2hp
        $nntexto += $codeKey.substr($charCode, 1);   
    }   
    return ($nntexto);   
}
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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.
 

ryanmm

Member
Messages
38
Reaction score
0
Points
6
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.


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.

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:
Top