Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Equivalent AES 128 bit in PHP
  2. String key = "1234567890123456";
  3.     String source = "The quick brown fox jumped over the lazy dog";
  4.  
  5.     byte[] raw = key.getBytes();
  6.     SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  7.  
  8.     // Instantiate the cipher
  9.     Cipher cipher = Cipher.getInstance("AES");
  10.  
  11.     cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  12.  
  13.     byte[] encrypted = cipher.doFinal(source.getBytes());
  14.     System.out.println(new String(Base64.encodeBase64(encrypted)));
  15.        
  16. <?php
  17. function encrypt($str, $key){
  18.  $block = mcrypt_get_block_size('des', 'ecb');
  19.  $pad = $block - (strlen($str) % $block);
  20.  $str .= str_repeat(chr($pad), $pad);
  21.  return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB));
  22. }
  23.  
  24. function decrypt($str, $key)
  25. {
  26.  $str = base64_decode($str);
  27.  $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB);
  28.  $block = mcrypt_get_block_size('des', 'ecb');
  29.  $pad = ord($str[($len = strlen($str)) - 1]);
  30.  $len = strlen($str);
  31.  $pad = ord($str[$len-1]);
  32.  return substr($str, 0, strlen($str) - $pad);
  33. }
  34. %>