<?php
$plaintext = "Four score and seven years ago";
$cipher = MCRYPT_TRIPLEDES;
$mode = MCRYPT_MODE_ECB;
$rand_src = MCRYPT_DEV_RANDOM;
$password = 'Extra secret password';

print ("Plaintext: $plaintext\n");

// Ok, let's encrypt the data
$handle = mcrypt_module_open ($cipher, ' ' , $mode, ' ');
if ($handle)
	die ("Couldn't locate open mcrypt module for '$cipher', algorithm");
$iv_size = mcrypt_enc_get_iv_size ($handle);
$ivector = mcrypt_create_iv ($iv_size, $rand_src);
if (mcrypt_generic_init ($handle, $password, $ivector) == -1)
	die ("Error: mcrypt_generic_int() failed.");
$ciphertext = mcrypt_generic ($handle, $plaintext);
mcrypt_generic_end ($handle);

print ("Ciphertext: " . bin2hex ($ciphertext) . "\n");

// Now let's decrypt it
$handle = mcrypt_module_open ($cipher, ' ', $mode, ' ');
if (!$handle)
	die ("Couldn't locate open mcrypt module for '$cipher' algorithm");
if (mcrypt_generic_init ($handle, $password, $ivector) == -1)
	die ("Error: mcrypt_generic_init() failed also.");
	$plaintext = mdecrypt_generic ($handle, $ciphertext);
mcrypt_generic_end ($handle);

print ("Plaintext: $plaintext\n");

?>

This is the code using mcrypt but when i try at the server.It appear error like this Notice: Use of undefined constant MCRYPT_TRIPLEDES - assumed 'MCRYPT_TRIPLEDES' in C:\Program Files\Apache Group\Apache2\htdocs\encrypt.php on line 4

Notice: Use of undefined constant MCRYPT_MODE_ECB - assumed 'MCRYPT_MODE_ECB' in C:\Program Files\Apache Group\Apache2\htdocs\encrypt.php on line 5

Notice: Use of undefined constant MCRYPT_DEV_RANDOM - assumed 'MCRYPT_DEV_RANDOM' in C:\Program Files\Apache Group\Apache2\htdocs\encrypt.php on line 6
Plaintext: Four score and seven years ago
Fatal error: Call to undefined function: mcrypt_module_open() in C:\Program Files\Apache Group\Apache2\htdocs\encrypt.php on line 12

Please help me!!!!!

    Hi,

    download libmcrypt.dll from
    http://ftp.emini.dk/pub/php/win32/mcrypt/

    and copy the file to system32.

    Then enable the line

    extension=php_mcrypt.dll

    in php.ini and restart apache.

    EDIT: make sure that extension_dir in php.ini points to the directory with the php extensions.

    Thomas

      I donno how to enable extension==php_mcrypt.dll...... but i already copy libmcrypt in system 32. Can u explain me more clearly the next step to enable it?

        Ok,

        you need a valid php.ini file in the windows (or winnt) directory. If there is no such file then copy e.g. the php.ini-dist that comes with the php zip package to the windows (or winnt) directory and rename it to php.ini.

        Edit php.ini and modify the line

        extension_dir=....

        so that it points to the directory where the php extensions (pph_???.dll) are (e.g. extension_dir=c:\php\ext).

        Then search for a line looking like

        ;extension=php_mcrypt.dll

        and remove the ; at the beginning of the line.

        Then restart apache.

        Thomas

          i already do that but in my pc don't have extension php_mcrypt.dll (already try search for that extension) Can the extension download from the website. Where can I download the extension.Then how i want to point the extension since the extension does not exist in my pc....

            How did you install PHP ? with the installer ?

            Edit: Which version did you install ?

            Thomas

              i use php version 4.3.9. I download the one with windows installer.I just click ok for each step.

                It's already ok with mcrypt configuration as i just extract the version php 4.3.9 win32 . But just want to know why the output appear like this Plaintext: Four score and seven years ago Couldn't locate open mcrypt module for 'tripledes', algorithm. Is it got something wrong with my coding.

                <?php
                $plaintext = "Four score and seven years ago";
                $cipher = MCRYPT_TRIPLEDES;
                $mode = MCRYPT_MODE_ECB;
                $rand_src = MCRYPT_DEV_RANDOM;
                $password = 'Extra secret password';

                print ("Plaintext: $plaintext\n");
                
                // Ok, let's encrypt the data
                $handle = mcrypt_module_open ($cipher, ' ' , $mode, ' ');
                if ($handle)
                	die ("Couldn't locate open mcrypt module for '$cipher', algorithm");
                $iv_size = mcrypt_enc_get_iv_size ($handle);
                $ivector = mcrypt_create_iv ($iv_size, $rand_src);
                if (mcrypt_generic_init ($handle, $password, $ivector) == -1)
                	die ("Error: mcrypt_generic_int() failed.");
                $ciphertext = mcrypt_generic ($handle, $plaintext);
                mcrypt_generic_end ($handle);
                
                print ("Ciphertext: " . bin2hex ($ciphertext) . "\n");
                
                // Now let's decrypt it
                $handle = mcrypt_module_open ($cipher, ' ', $mode, ' ');
                if (!$handle)
                	die ("Couldn't locate open mcrypt module for '$cipher' algorithm");
                if (mcrypt_generic_init ($handle, $password, $ivector) == -1)
                	die ("Error: mcrypt_generic_init() failed also.");
                	$plaintext = mdecrypt_generic ($handle, $ciphertext);
                mcrypt_generic_end ($handle);
                
                print ("Plaintext: $plaintext\n");

                ?>

                  Which mcrypt version does a little phpinfo() script show ? That cipher is available with libmcrypt > 2.4.x only.

                  Thomas

                    Write a Reply...