mcrypt_encrypt():mcrypt初始化失败


mcrypt_encrypt(): Mcrypt initialisation failed

我下载了示例Laravel 5项目,并尝试在本地机器上进行测试。但是我可以看到如下错误:

User.php第115行出现错误异常:
mcrypt_encrypt():mcrypt初始化失败

1. in User.php line 115
2. at HandleExceptions->handleError('4096', 'mcrypt_encrypt(): Mcrypt     initialisation failed', 'C:'wamp'www'noj'app'Models'User.php', '115',     array('pure_string' => 'asdfasdf', 'USER_PASSWORD_SALT' => null, 'key' => '', 'iv_size' => '8', 'iv' => 'ЎZo'S '))
3. at mcrypt_encrypt('blowfish', '', 'asdfasdf', 'ecb', 'ЎZo'S ') in User.php line 115
4. at User::encrypthash('asdfasdf') in Registrar.php line 36
5. at Registrar->create(array('_token' => '7oIiiOAtUu4Quc949u7SmtcDlI5a0zo0AEJgBA3S', 'name' => 'Bob', 'email' => 'bob@example.com', 'password' => 'asdfasdf', 'password_confirmation' => 'asdfasdf')) in AuthenticatesAndRegistersUsers.php line 50
6. at AuthController->postRegister(object(Request))</li>
...

用户.php

        ...  
108.    public static function encrypthash($pure_string) {
109.        //xulianwhocreatenoj
110.        $USER_PASSWORD_SALT = env('USER_PASSWORD_SALT');
111.        $key = pack('H*', $USER_PASSWORD_SALT);
112.        $pure_string = trim($pure_string);
113.        $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
114.        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
115.        $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
116.        return base64_encode($encrypted_string);
117.    }
        ...

我对PHP和mcrypt_encrypt()函数都没有深入的了解。请帮我摆脱这个。

第115行的问题是加密密钥只是一个空字符串。

$key是空的,因为有两行,你有:

$USER_PASSWORD_SALT = env('USER_PASSWORD_SALT');

问题是env('USER_PASSWORD_SALT')是空的,所以没有盐。如果你解决了这个问题,你的功能就会发挥作用。

在命令行(应位于Laravel项目根目录上)上运行php artisan key:generate以刷新应用程序密钥。

然后清除缓存和会话,然后重试。