I posted similar question yesterday. but another problem occurred.
PHP code, encrypt MCRYPT_RIJNDAEL_256
<?PHP
define('CIPHER_KEY', '3b4b9507e624be58dec7a465d8ed8c37'); //32byte
//define('CIPHER_KEY', '1111111111111111');
define('IV', '2222222222222222');
echo "<xmp>";
$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_256, $sKey, $sStr, MCRYPT_MODE_CFB, $sIV);
return bin2hex($sCipher);
}
function getDecrypt($sStr, $sKey=CIPHER_KEY, $sIV=IV )
{
$sDecipher = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sKey, pack('H*', $sStr), MCRYPT_MODE_CFB, $sIV);
return $sDecipher;
}
?>
and decrypt in java.
import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
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 = "8ff50f19";
String keyString = "3b4b9507e624be58dec7a465d8ed8c37";
String initialVectorParam = "2222222222222222";
SecretKeySpec key = new SecretKeySpec(keyString.getBytes(), "AES");
IvParameterSpec initalVector = new IvParameterSpec(initialVectorParam.getBytes());
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
/*
SecretKey aesKey;
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
aesKey = keyGen.generateKey();
System.out.println("aesKey:"+bytes2Hex(aesKey.getEncoded()));
System.out.println(aesKey.getFormat());
/////////////// 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 = hex2byte(cookie);
byte[] decryptedValue = cipher.doFinal(encrypted);
System.out.println(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();
}
}
I aleady overwrite "local_policy.jar" and "US_export_policy.jar" and no more key length exception.
expected output is "test"
but "B?J" printed.
and initial vector set to 32byte (ex:22222222222222222222222222222222)
"java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long" exception occured.
please resolve this problem.
I am sorry that I can't speak English well.
thank you.