Ravi Desai
Ravi Desai

Reputation: 81

Decryption function not called in php

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?

Upvotes: 1

Views: 196

Answers (1)

St. John Johnson
St. John Johnson

Reputation: 6660

What is the error you are getting?

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

Upvotes: 1

Related Questions