-1

I am using MCrypt.php

index.php

include("MCrypt.php");

$crypter = new MCrypt();

MCrypt.php exists in same directory.

when I run index.php I get

Fatal error: Class 'MCrypt' not found in /home/username/project/index.php on line 5

UPDATE: I have tried including just MCrypt.php but got the same error. I tried renaming to .class.php and got the same error.

class MCrypt {

    private $iv = 'fedcba9876543210'; #Same as in JAVA
    private $key = '0123456789abcdef'; #Same as in JAVA

    function __construct() {

    }

    function encrypt($str) {
        $iv = $this->iv;

        $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

        mcrypt_generic_init($td, $this->key, $iv);
        $str = $this->padString($str);
        $encrypted = mcrypt_generic($td, $str);

        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return base64_encode($encrypted);
    }

    function decrypt($code) {
        $code = base64_decode($code);
        $iv = $this->iv;

        $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

        mcrypt_generic_init($td, $this->key, $iv);
        $decrypted = mdecrypt_generic($td, $code);

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

        return utf8_encode(trim($decrypted));
    }

    protected function hex2bin($hexdata) {
        $bindata = '';

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

        return $bindata;
    }

    private function padString($source) {
        $paddingChar = ' ';
        $size = 16;
        $x = strlen($source) % $size;
        $padLength = $size - $x;

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

        return $source;
    }

}
KJW
  • 15,035
  • 47
  • 137
  • 243

2 Answers2

1

Including instead of MCrypt.class.php, the file is MCrypt.php if the file is called MCrypt.php.

Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
1

your php file has to start with the php opening tag. the first line, before the class, has to be <?php

like this:

<?php
    class MCrypt {
        ...
davogotland
  • 2,718
  • 1
  • 15
  • 19