# mcrypt problem



## TechGuy (Feb 12, 1999)

Trying to run a program in PHP that uses mcrypt, but getting the following error. Ever hear of this before? If not, can you find anything about it? I sure can't..... 



> Warning: mcrypt_generic_init(): Key size too large


----------



## pyritechips (Jun 3, 2002)

Hi Mike:

I don't know what I'm talking about so you can ignore me if you want!  

Is there a 56 key size limit on your mcrypt? I did a little searching and there is a new version with a 66 key limit? Just a thought.


----------



## IMM (Feb 1, 2002)

What does the phpinfo.php page say?
Is mcrypt is properly installed (compiled into php)
what version of php?

What does the get key size return?
http://www.phpfreaks.com/phpmanual/page/function.mcrypt-generic-init.html


----------



## TechGuy (Feb 12, 1999)

Yup, mcrypt is compiled (it was giving me an unhandled function before  )... Here's out of my phpinfo():

mcrypt support enabled 
version >= 2.4.x 
Supported ciphers cast-128 gost rijndael-128 twofish arcfour cast-256 loki97 rijndael-192 saferplus wake blowfish-compat des rijndael-256 serpent xtea blowfish enigma rc2 tripledes 
Supported modes cbc cfb ctr ecb ncfb nofb ofb stream 

Directive - Local Value - Master Value 
mcrypt.algorithms_dir - no value - no value 
mcrypt.modes_dir - no value - no value


Oh, and it's PHP Version 4.3.3 (I was advised against 4.3.4, but I can try it!)


----------



## TechGuy (Feb 12, 1999)

> _Originally posted by IMM:_
> *What does the get key size return?
> http://www.phpfreaks.com/phpmanual/page/function.mcrypt-generic-init.html *


How do I properly call it? It wants a resource td as the parameter, but I have no idea what that is... without, I get:



> Warning: Wrong parameter count for mcrypt_enc_get_key_size() in /home/billing/public_html/test.php on line 3


----------



## Guy (Feb 19, 1999)

Have you tried looking at the example here?:
http://ca2.php.net/manual/en/function.mcrypt-module-open.php


----------



## Gibble (Oct 10, 2001)

Mike, here's some handy wrapper functions for mcrypt 


```
<? 
//$input - stuff to encrypt 
//$key - the secret key to use

function _mencrypt($input,$key){ 
    $input = str_replace("\n","",$input); 
    $input = str_replace("\t","",$input); 
    $input = str_replace("\r","",$input); 
    $key = substr(md5($key),0,24); 
    $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); 
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); 
    mcrypt_generic_init ($td, $key, $iv); 
    $encrypted_data = mcrypt_generic ($td, $input); 
    mcrypt_generic_deinit ($td); 
    mcrypt_module_close ($td); 
    return trim(chop(base64_encode($encrypted_data))); 
}

//$input - stuff to decrypt 
//$key - the secret key to use

function _mdecrypt($input,$key){ 
    $input = str_replace("\n","",$input); 
    $input = str_replace("\t","",$input); 
    $input = str_replace("\r","",$input); 
    $input = trim(chop(base64_decode($input))); 
    $td = mcrypt_module_open ('tripledes', '', 'ecb', ''); 
    $key = substr(md5($key),0,24); 
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND); 
    mcrypt_generic_init ($td, $key, $iv); 
    $decrypted_data = mdecrypt_generic ($td, $input); 
    mcrypt_generic_deinit ($td); 
    mcrypt_module_close ($td); 
    return trim(chop($decrypted_data)); 
    } 
?>
```
ps.

```
$key = substr(md5($key),0,24)
```
this make sure the secret key is the max length, the longer the key the harder to break it.


----------



## TechGuy (Feb 12, 1999)

I'm not actually trying to write the php program, just get this one to work! It's encrypted PHP, though, so I can't read it... but everyone else uses the code with no problem. It's got to be a setting on this server.


----------



## Gibble (Oct 10, 2001)

oh...


----------



## brendandonhu (Jul 8, 2002)

Maybe the script uses a different mcrypt cipher....there are lots of them, maybe it needs one you don't have.

Also, is the script encrypted with Zend, ionCube, MMCache, or maybe a different encoder? Some of these may have probs with mcrypt.

And just in case--can we see the full phpinfo?


----------



## TechGuy (Feb 12, 1999)

http://taskforce.cermaktech.com/phpinfo.php

And you're right, brendan -- it's encrypted with ionCube. I don't think they're interested in giving me the decrypted version, though.


----------



## brendandonhu (Jul 8, 2002)

I would reinstall the ionCube loader, as well as recompile PHP with --with-mhash just in case the script needs that. Is this just a script someone wrote or is it commercially available?


----------



## TechGuy (Feb 12, 1999)

I might try reloading ionCube.... Already tried recompiling PHP, and if you check that phpinfo page again with mhash, same result...


----------



## brendandonhu (Jul 8, 2002)

Thats odd, could it be a problem with the script? Maybe try it on a diff. host with a working mcrypt before messing with the server. I don't think either of my hosts have ionCube installed, but I'm sure you have a server to use, you do run a webhost, right?


----------



## brendandonhu (Jul 8, 2002)

I don't think the problem is mcrypt. The script you are installing also needs cURL, which I don't see in phpinfo.
http://php.net/curl

Looks like you need to recompile PHP one more time


----------



## TechGuy (Feb 12, 1999)

Just tried reloading ionCube, but no luck. *sigh* I don't know what else to try. I might just clear out the whole darn thing and try over, but that'll take some work as I already have data in this system. Thanks for trying. I'll let you know what happens when I get around to it.


----------



## brendandonhu (Jul 8, 2002)

Did you try recompiling PHP with cURL support?


----------



## TechGuy (Feb 12, 1999)

Yup, sure did. Same thing. I'm thinking it's gotta be something in the database... I dunno. I wanna try with a clean database and see what happens, ya know? But then it'll be a pain to bring the old data over (particular that which is encrypted)...


----------

