0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mcrypt非推奨による暗号化復号化導入

Posted at

php7.2でmcryptが非推奨

PEAR::Crypt_Blowfishを使っていると警告が溢れてくるようになったので対策。

必要なライブラリを探す

Composerで入れられるパッケージがあるっぽいので試す

webiny/crypt

# 足りないライブラリを追加
sudo apt install php-xml php-mbstring
# Composerに追加
composer require webiny/crypt
# ↓Composerにpathが通ってないなら↓
# php composer.phar require webiny/crypt

サンプルコード

require __DIR__ . '/vendor/autoload.php';

class MyClass
{
        use Webiny\Component\Crypt\CryptTrait;
        function myMethod(){
                $key = 'aqwsedrfgthyjuikolp';
                $text = 'money-money-money';
                $encrypted = $this->crypt()->encrypt($text, $key);
                try{
                        $decrypted = $this->crypt()->decrypt($encrypted, $key);
                        $decryptedBad = $this->crypt()->decrypt($decrypted, 'bad-bad-bad');
                } catch (\Exception $e) {
                        var_dump($e->getMessage());
                }
                var_dump($encrypted);
//                string(64) "HhgH~省略~vaYz/"
                var_dump($decrypted);
//                string(17) "money-money-money"
                var_dump($decryptedBad);
//                bool(false)
        }
}

$m = new MyClass();
$m->myMethod();

とりあえず動いた
古いblowfishをデコードできるかは別途要検証…

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?