Skip to main content
Known Participant
May 29, 2009
Question

PHP converted to CF

  • May 29, 2009
  • 1 reply
  • 1033 views

I am trying to convert this php to coldfusion, but I am not sure what some of it is doing... like the ^ operator.  Any help would be super awesome

PHP:

<?php
$account_key = "whoisit28";
$api_key = "12345";

$salted = $api_key . $account_key;
$hash = hash('sha1',$salted,true);
$saltedHash = substr($hash,0,16);
$iv = "OpenSSL for Ruby";

$user_data = array(
  "guid" => "1234",
  "expires" => "2009-05-29 20:02:40",
  "display_name" => "Richard White",
  "email" => "rich@acme.com",
  "url" => "http://acme.com/users/1234",
  "avatar_url" => "http://acme.com/users/1234/avatar.png"
);

$data = json_encode($user_data);

// double XOR first block
for ($i = 0; $i < 16; $i++)
{
$data[$i] = $data[$i] ^ $iv[$i];
}

$pad = 16 - (strlen($data) % 16);
$data = $data . str_repeat(chr($pad), $pad);
     
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');
mcrypt_generic_init($cipher, $saltedHash, $iv);
$encryptedData = mcrypt_generic($cipher,$data);
mcrypt_generic_deinit($cipher);

$encryptedData = urlencode(base64_encode($encryptedData));
?>

My CF so far:

<cfscript>
            account_key = "whoisit28";
            api_key = "12345";
           
            salted = api_key & account_key;
            hashed = toBase64(hash(salted,'SHA'));
            saltedHash = left(hashed,16);
            iv = "OpenSSL for Ruby";
            /*iv = arrayNew(1);
            for(i=1; i lte len(iv_list);i++){
                iv = mid(iv_list,i,1);
            }*/
           
            expires = dateadd("h",1,now());
            expires = dateformat(expires,"yyyy-mm-dd") &" "& timeformat(expires,"HH:mm:ss");
        </cfscript>

        <cfquery name="user" datasource="datasource">
            select contactid as guid, '#expires#' as expires, firstname + ' ' + lastname as display_name, email, 'http://www.mastercontrolcustomers.com' as url, '' as avatar_url
            from contacts
            where contactid = #contactid#
        </cfquery>
       
        <cfset data = serializeJSON(user)>
        <cfscript>
            for(i=1; i lte 16; i++){
                tmp1 = mid(iv,i,1);
                tmp2 = mid(data,i,1);
            }
        </cfscript>

    This topic has been closed for replies.

    1 reply

    Participating Frequently
    May 30, 2009

    The code seem to encrypt some json data with AES (formerly known as

    Rijndael). The ^ operator is a bit-wise operator and does exactly what

    the comment above it says: XOR.

    Mack

    whoisit28Author
    Known Participant
    June 1, 2009

    so would you say my code is doing the same thing as the PHP?

    Participating Frequently
    June 1, 2009

    Not all all.

    You're using a hash algorithm (SHA), the PHP code has an encryption

    algorithm (AES). The equivalent in CF of mcrypt with

    MCRYPT_RIJNDAEL_128 is AES.

    Mack