在Laravel外部解密加密的值 [英] Decrypt encrypted value outside of Laravel

查看:240
本文介绍了在Laravel外部解密加密的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅使用PHP解密在Laravel之外使用Laravel 4 Encrypt类加密的字符串?

How can i decrypt a string which has been encrypted using the Laravel 4 Encrypt class, outside of Laravel, only with PHP?

推荐答案

Laravel Encrypter类使用Rijndael进行加密,该块大小为256位,由Mcrypt PHP扩展提供. Encrypter类使用两种简单的方法encrypt()decrypt()工作.

The Laravel Encrypter class uses Rijndael with a block size of 256 bit for encryption which is provided by the Mcrypt PHP extension. The Encrypter class works using two simple methods, encrypt() and decrypt().

以下示例:

<?php

$secret = Crypter::encrypt('some text here'); //encrypted

$decrypted_secret = Crypter::decrypt($secret); //decrypted

?>

由于您在Laravel之外询问如何操作:

加密和解密由加密器类完成. Laravel来源是公开的,这是相关的部分:

The encryption and decryption is done by the encrypter class. Laravel source is public and here's the relevant part:

<?php

    public function encrypt($value)
    {
        $iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());
        $value = base64_encode($this->padAndMcrypt($value, $iv));
        $mac = $this->hash($iv = base64_encode($iv), $value);

        return base64_encode(json_encode(compact('iv', 'value', 'mac')));
    }

    protected function padAndMcrypt($value, $iv)
    {
        $value = $this->addPadding(serialize($value));
        return mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv);
    }

    public function decrypt($payload)
    {
        $payload = $this->getJsonPayload($payload);
        $value = base64_decode($payload['value']);
        $iv = base64_decode($payload['iv']);
        return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));
    }

    protected function mcryptDecrypt($value, $iv)
    {
        return mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv);
    }

?>

有关文档和注释,请参见 Laravel源代码在GitHub上.

For documentation and comments, see Laravel source code on GitHub.

我希望这会有所帮助.

这篇关于在Laravel外部解密加密的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆