Jack
Jack

Reputation: 387

How to increase the mcrypt-generic-init key size from 8 to 16?

When I run the encryption, I got an error: Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 16, max: 8

My Code:

 $size = mcrypt_get_block_size('des', 'ecb'); 
 $input = pkcs5_pad($input, $size); 

 $key = '*5hS7e6$23N#2m7s'; 
 $td = mcrypt_module_open('des', '', 'ecb', ''); 
 $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
 mcrypt_generic_init($td, $key, $iv); 
 $data = mcrypt_generic($td, $input); 
 mcrypt_generic_deinit($td); 
 mcrypt_module_close($td); 
 $data = base64_encode($data); 
 return $data; 

I am trying to write an encryption & decryption in between Java(code provided by Java developer) & PHP. But Java only support 16 characters.

Thanks.

Upvotes: 1

Views: 1462

Answers (2)

shanmuka  srinivas Ch
shanmuka srinivas Ch

Reputation: 11

$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'ecb'); 
 $input = pkcs5_pad($input, $size); 

 $key = '*5hS7e6$23N#2m7s'; 
 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'ecb', ''); 
 $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
 mcrypt_generic_init($td, $key, $iv); 
 $data = mcrypt_generic($td, $input); 
 mcrypt_generic_deinit($td); 
 mcrypt_module_close($td); 
 $data = base64_encode($data); 
 return $data; 

Please change

  1. if you want size '16' for that key has some size 'des' change to 'MCRYPT_RIJNDAEL_128'
  2. if you want size '32' for that key has large size 'des' change to 'MCRYPT_RIJNDAEL_256'.

try and have a fun

Upvotes: 1

Tomas
Tomas

Reputation: 1694

You can not. DES encryption supports key length 56bit (7 characters), block size is 64bit (8 characters). More characters is pointless. I do not know what operation java do with longer key but it is shortened somehow.

I would ask if the java program uses DES or triple-DES encryption. Triple-DES uses 168bit key length so you can use keys with 16 characters.

Some side note - DES is considered unsafe so I would expect that triple-DES is used.

Upvotes: 0

Related Questions