2011. 9. 9. 10:18

AES 128Bit encryption between Java and PHP

AES 128Bit encryption between Java and PHP

Posted in Java by schneimi on November 25, 2008

Here is how AES encryption works between Java and PHP using the mcrypt module in PHP.

This is mainly a quick summary of the 4-part tutorial at: http://propaso.com/blog/?cat=6

  • Generate Key in Java
  • 01 String iv = "fedcba9876543210";
    02 IvParameterSpec ivspec;
    03 KeyGenerator keygen;
    04 Key key;
    05  
    06 ivspec = new IvParameterSpec(iv.getBytes());
    07  
    08 keygen = KeyGenerator.getInstance("AES");
    09 keygen.init(128);
    10 key = keygen.generateKey();
    11  
    12 keyspec = new SecretKeySpec(key.getEncoded(), "AES");
  • Encryption in Java
  • 1 Cipher cipher;
    2 byte[] encrypted;
    3  
    4 cipher = Cipher.getInstance("AES/CBC/NoPadding");
    5 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
    6 encrypted = cipher.doFinal(padString(text).getBytes());
  • Decryption in Java
  • 1 Cipher cipher;
    2 byte[] decrypted;
    3  
    4 cipher = Cipher.getInstance("AES/CBC/NoPadding");
    5 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
    6 decrypted = cipher.doFinal(hexToBytes(code));
  • Encryption in PHP
  • 01 function encrypt($str$key) {
    02   $key $this->hex2bin($key);   
    03  
    04   $td = mcrypt_module_open("rijndael-128""""cbc","fedcba9876543210");
    05  
    06   mcrypt_generic_init($td$key, CIPHER_IV);
    07   $encrypted = mcrypt_generic($td$str);
    08  
    09   mcrypt_generic_deinit($td);
    10   mcrypt_module_close($td);
    11  
    12   return bin2hex($encrypted);
    13 }
  • Decryption in PHP
  • 01 function decrypt($code$key) {
    02   $key $this->hex2bin($key);
    03   $code $this->hex2bin($code);
    04  
    05   $td = mcrypt_module_open("rijndael-128""""cbc","fedcba9876543210");
    06  
    07   mcrypt_generic_init($td$key, CIPHER_IV);
    08   $decrypted = mdecrypt_generic($td$code);
    09  
    10   mcrypt_generic_deinit($td);
    11   mcrypt_module_close($td);
    12  
    13   return utf8_encode(trim($decrypted));
    14 }
  • Additional functions in Java
  • 01 private byte[] hexToBytes(String hex) {
    02   String HEXINDEX = "0123456789abcdef";
    03   int l = hex.length() / 2;
    04   byte data[] = new byte[l];
    05   int j = 0;
    06  
    07   for (int i = 0; i < l; i++) {
    08     char c = hex.charAt(j++);
    09     int n, b;
    10  
    11     n = HEXINDEX.indexOf(c);
    12     b = (n & 0xf) << 4;
    13     c = hex.charAt(j++);
    14     n = HEXINDEX.indexOf(c);
    15     b += (n & 0xf);
    16     data[i] = (byte) b;
    17   }
    18  
    19   return data;
    20 }
    21  
    22 private String padString(String source) {
    23   char paddingChar = ' ';
    24   int size = 16;
    25   int padLength = size - source.length() % size;
    26  
    27   for (int i = 0; i < padLength; i++) {
    28     source += paddingChar;
    29   }
    30  
    31   return source;
    32 }
  • Additional functions in PHP
  • 1 function hex2bin($hexdata) {
    2   $bindata "";
    3  
    4   for ($i = 0; $i strlen($hexdata); $i += 2) {
    5     $bindata .= chr(hexdec(substr($hexdata$i, 2)));
    6   }
    7  
    8   return $bindata;
    9 }