Wednesday, November 20, 2013

PHP Equivalent for DESede/CBC/PKCS5Padding

Recently I tried porting an encryption implementation to PHP from Java.

It was not possible to touch the existing encryption/ decryption implementation So the requirement is to have the similar encryption implementation in PHP which will give same result as in Java.


The Java implementation is
    public String encryptTest(String message) throws Exception {
     final MessageDigest messageDigest = MessageDigest.getInstance("md5");
     String keyString = "AB12CD3EF4";
  final byte[] passwordDigest = messageDigest.digest(keyString.getBytes("utf-8"));
     final byte[] keyBytes = paddKeyString(passwordDigest);
     final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
     cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "DESede"), new IvParameterSpec(new byte[8]));
     final byte[] encryptedText = cipher.doFinal(message.getBytes("utf-8"));
     return Base64.encodeBase64String(encryptedText);
    }

 private byte[] paddKeyString(final byte[] passwordDigest) {
  final byte[] keyBytes = Arrays.copyOf(passwordDigest, 24);
  int pos = 16;
     for (int index = 0; index < 8;) {
      keyBytes[pos++] = keyBytes[index++];
     }
  return keyBytes;
 }

After couple of hours of trial and error , got the following PHP implementation which will produce the same result as the Java counterpart.
function encryptText_3des($plainText, $key) {
 $key = hash("md5", $key, TRUE); 
 for ($x=0;$x<8;$x++) {
  $key = $key.substr($key, $x, 1);
 }
 $padded = pkcs5_pad($plainText,
        mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
 $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
    return $encrypted;
}
Note: pkcs5_pad method is available here http://php.net/manual/en/ref.mcrypt.php
PHP is fun , I am going to give it little more time :)

Tuesday, November 12, 2013

Links for Configuring Apache , PHP in Windows with SSL


Installing Apache with PHP and SSL is relatively easy, But still there were couple of tricky steps. Following are the links I used for installing and configuring Apache with SSL, PHP (including cURL module).

Installing Apache in Windows

Remember to download the correct Apache binary. For example - the one I downloaded is 'httpd-2.2.25-win32-x86-openssl-0.9.8y.msi' as I required SSL also.
I choose to go with the default options.

If everything succeeds , you will be able to see "It Works!" when you try the URL "localhost" or "localhost:8080"

Installing PHP and configuring Apache

Follow this link http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml
Quick summary- following entries will need to be added to httpd.conf file 
  1. LoadModule php5_module c:/php/php5apache2_2.dll
  2. AddType application/x-httpd-php .php
  3. PHPIniDir "c:/php"
Create a sample php file PHPinfo.php in the htdocs folder with content 
<!DOCTYPE html>
<html>
<body>
<?php
PHPinfo();
?>
</body>
</html>
 Try the link 'http://localhost/PHPinfo.php', You will see the information about PHP if everything succeeds.
If you are planning to use cURL module remember to enable those lines in the php.ini file
e.g.- extension=php_curl.dll
Note: If this is not working , add 'c:\php' and 'c:\php\ext' to PATH environment variable.


For me things worked only after creating a symbolic link for 'C:\Program Files (x86)\Apache Software Foundation'.