Archive

Archive for September, 2008

Cryptography with Delphi and PHP

September 2nd, 2008

We’re doing some pretty exciting stuff at work right now. Actually, we’ve been doing it for the past couple of years but only now starting to see the fruits of it all. What is it? well, I guess Microsoft would call it ‘Live’ (or something like that), and Google would call it, well, I’m not sure, gOS? or online documents, or something… We don’t have a name for it yet (maybe WizLiveOz) but it involves communicating between Delphi and PHP using XML. Most data is stored on the server and the Delphi client makes requests via XML on port 80 (HTTP). The returned XML is/can be stored locally but the great thing is, move the client to another machine and it gets all the data again. However, we need to encrypt all the XML to keep everything secret (of course). So, I googled this great Crypto site:

http://cityinthesky.co.uk/cryptography.html

Here’s a code snippet for the PHP side which decrypts the XML coming in from Delphi:

// *******************************************************************************
// Decrypt xml from client – START

$key = ‘lettherebedelphi’;
$iv = ‘andphpstoo12345′;

if ($XML <> ”) {
$encryptedcbc = ltrim($XML, “XML=”) ;
//    echo ‘<p>Got: ‘.$encryptedcbc . ‘<br>’;
$encryptedcbc2 = base64_decode($encryptedcbc) ;
$decryptedcbc = mcrypt_cbc(MCRYPT_RIJNDAEL_128,$key, $encryptedcbc2, MCRYPT_DECRYPT, $iv);
$output = rtrim($decryptedcbc, “\0″);

//    echo ‘<p>CBC decrypted: ‘.$output;
$XML = $output ;
}else
{
exit ;
}

// Decrypt xml from client – END
// *******************************************************************************

Delphi