Skip to main content

AES problem - encrypt in PHP, decrypt in JAVA

1 reply [Last post]
throat
Offline
Joined: 2008-09-01

PHP code
<?PHP
define('CIPHER_KEY', '1111111111111111');
define('IV', '2222222222222222');

echo "";
$a= getEncrypt("test");
echo "\n";
echo $a;
echo "\n";
echo getDecrypt($a);

function getEncrypt($sStr, $sKey=CIPHER_KEY, $sIV=IV )
{
$sCipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sKey, $sStr, MCRYPT_MODE_CFB, $sIV);
return bin2hex($sCipher);
}

function getDecrypt($sStr, $sKey=CIPHER_KEY, $sIV=IV )
{
$sDecipher = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey, pack('H*', $sStr), MCRYPT_MODE_CFB, $sIV);

return $sDecipher;
}
?>

=========== result ==========
469bfdbd <--- encrypted value
test <---- expected result

JAVA code

import java.math.BigInteger;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesTest {
public static void main(String[] args) throws Exception{
String orignal = "test";
String cookie = "469bfdbd";
String keyString = "1111111111111111";
String initialVectorParam = "2222222222222222";

SecretKeySpec key = new SecretKeySpec(keyString.getBytes(), "AES");
IvParameterSpec initalVector = new IvParameterSpec(initialVectorParam.getBytes());

Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");

/////////////// encrypt /////////////////
cipher.init(Cipher.ENCRYPT_MODE, key, initalVector);
byte[] test = cipher.doFinal(orignal.getBytes());
System.out.println(bytes2Hex(test));

/////////////// decrypt /////////////////
cipher.init(Cipher.DECRYPT_MODE, key, initalVector);
// byte[] encrypted = new BigInteger(1,cookie.getBytes()).toByteArray();
// byte[] encrypted = new BigInteger(cookie,16).toByteArray();
byte[] encrypted = hex2byte(cookie);
byte[] decryptedValue = cipher.doFinal(encrypted);

System.out.println("key:"+new String(key.getEncoded()));
System.out.println("IV:"+new String(initalVector.getIV()));
System.out.println("encrypted:"+new String(encrypted));
System.out.println("decrypted:"+new String(decryptedValue));
}

private static byte[] hex2byte(String s){
if(s == null) return null;
int l = s.length();
if(l%2 == 1) return null;
byte[] b = new byte[l/2];
for(int i = 0 ; i < l/2 ;i++) {
b[i] = (byte)Integer.parseInt(s.substring(i*2,i*2+2),16);
}
return b;
}

private static String byte2Hex(byte b) {
String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
int nb = b & 0xFF;
int i_1 = (nb >> 4) & 0xF;
int i_2 = nb & 0xF;
return HEX_DIGITS[i_1] + HEX_DIGITS[i_2];
}

private static String bytes2Hex(byte[] b) {
StringBuffer sb = new StringBuffer(b.length * 2);
for (int x = 0; x < b.length; x++) {
sb.append(byte2Hex(b[x]));
}
return sb.toString();
}
}

expected result is "test", but "tT9" printed.....
only first charactor is correct. "test"->"t***", "abce"->"a***"

what is problem?
thank you.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the_mars
Offline
Joined: 2008-10-03

I had similar problems when trying to transfer encrypted data from PHP to Java. At the end I could solve it and I have written a blog post for it, including example PHP and Java code: http://the-mars.blogspot.com/2008/10/php-encrypt-and-java-decrypt.html