TripleDES in Perl/PHP/ColdFusion TripleDES in Perl/PHP/ColdFusion php php

TripleDES in Perl/PHP/ColdFusion


The Perl's TripleDES should never be used. It does so many weird things and you are going to have fun.

Your first problem is that the keys in Perl are hex and you need to convert them into binary. Try this in PHP,

$theKey="123412341234123412341234";$key = pack('H*', str_pad($theKey, 16*3, '0'));$strEncodedEnc=base64_encode(mcrypt_ecb (MCRYPT_3DES, $key, $theString, MCRYPT_ENCRYPT));echo $strEncodedEnc, "\n";

The result is,

AYOF+kRtg239Mnyc8QIarw==

Then you have to pad it in a weird way. I forgot the details. You are lucky with this sample (it's 16 chars).


The Coldfusion Answer:

The first problem is that your key length is not correct for Triple DES. ZZ Coder correctly deduced that it needs to be padded to the correct length with 0's.

The next step is that the key needs to be converted to hex. To do this in CF, we have:

<cfset theKey="123412341234123412341234000000000000000000000000"><cfset encodedKey = ToBase64(BinaryDecode(theKey, "HEX"))>

The final step is that the result is not being padded either, so we need to specify this in the encryption algorithm in CF:

<cfset strEncodedEnc = Encrypt(theString, encodedKey, "DESEDE/ECB/NoPadding", "Base64")>

The resulting complete code:

<cfset theKey="123412341234123412341234000000000000000000000000"><cfset encodedKey = ToBase64(BinaryDecode(theKey, "HEX"))><cfset theString = "username=test123"><cfset strEncodedEnc = Encrypt(theString, encodedKey, "DESEDE/ECB/NoPadding", "Base64")><cfdump var="#strEncodedEnc#"><br>

results in:

AYOF+kRtg239Mnyc8QIarw==


I'll include the code below for anyone that happens to be working on CCBill upgrade (which sounds like the company referred to in the original post). The PHP functions below will match the output from CCBill's 3DES/TripleDES internal encryption as described in the documentation here:http://www.ccbill.com/cs/manuals/CCBill_Subscription_Upgrade_Users_Guide.pdf

//Encrypt String using 3DES Keyfunction encrypt($str,$key){    $hex_key = hexmod($key);    $bin_hex_key = pack('H*', str_pad($hex_key, 16*3, '0'));    //Pad string length to exact multiple of 8    $str = $str. str_repeat(' ',8-(strlen($str)%8) );       $out = base64_encode( mcrypt_ecb(MCRYPT_3DES, $bin_hex_key, $str, MCRYPT_ENCRYPT) );    //print_r('Key/Hex/Str: '.$key.' -> '.$hex_key.' -> '.$str.' -> '.$out,1);    return $out;}//Hex Modulus: Converts G-Z/g-z to 0-f (See @Jinyo's Post)//Necessary to match CCBill's Encryptionfunction hexmod($str){    //Convert G-Z & g-z to 0-f    $ascii_in  = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';    $ascii_out = '0123456789ABCDEF0123456789ABCDEF0123abcdef0123456789abcdef0123';    $hex_out = str_replace(str_split($ascii_in),str_split($ascii_out),$str);    return $hex_out;}$triple_des_key = 'ABCDEFGHIJKLMNOPQRSTUVWX'; // <!-- 24char 3DES Key$username_string = 'username=<username here>'; // Encrypt this string$encrypted_username = encrypt($username_string,$triple_des_key); // <-- Output