1

I am using encryption in java and performing decryption in php.
i am using following code for doing encryption in java.

String iv = "fedcba9876543210";
IvParameterSpec ivspec;
KeyGenerator keygen;
Key key;

ivspec = new IvParameterSpec(iv.getBytes());

keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
key = keygen.generateKey();

keyspec = new SecretKeySpec(key.getEncoded(), "AES"); 

Cipher cipher;
byte[] encrypted;

cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
encrypted = cipher.doFinal(padString(text).getBytes());

private String padString(String source) {
  char paddingChar = ' ';
  int size = 16;
  int padLength = size - source.length() % size;

  for (int i = 0; i < padLength; i++) {
    source += paddingChar;
  }

  return source;
}


and for decryption in php i am using this following code:

function decrypt($code, $key) {
  $key = $this->hex2bin($key);
  $code = $this->hex2bin($code);

  $td = mcrypt_module_open("rijndael-128", "", "cbc", "fedcba9876543210");

  mcrypt_generic_init($td, $key, CIPHER_IV);
  $decrypted = mdecrypt_generic($td, $code);

  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);

  return utf8_encode(trim($decrypted));
}

function hex2bin($hexdata) {
  $bindata = "";

  for ($i = 0; $i < strlen($hexdata); $i += 2) {
    $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  }

  return $bindata;
}


Encryption is working fine but flow is getting stopped during decryption at this function of php:

$td = mcrypt_module_open("rijndael-128", "", "cbc", "fedcba9876543210");

so what am i missing?

1
  • What does "getting stopped" mean? Any errors? Commented Apr 26, 2012 at 6:44

1 Answer 1

1

What is the error you are getting?

Do you have the mcrypt for PHP installed/compiled? Info here

Sign up to request clarification or add additional context in comments.

4 Comments

Hey, thanks for replying. i am not getting any error. Can i get libmcrypt-x.x.tar.gz for mac os john
when i echo before mcrypt_module_open() function it will print that. But if i echo after mcrypt_module_open(), it will not print anything.
Have you tried putting a die('test'); after it? If that doesn't work. You're throwing a fatal error (function not found) and you're not reporting it error_reporting(0)? Check your error log. You may not have mcrypt installed.
yes you are right. I have not installed mcrypt. But now i am facing new problem. I have posted question for the same. Can u please check the following url. stackoverflow.com/questions/10360762/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.