facebook twitter hatena line email

「Php/可逆暗号」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「Pear::Crypt_Blowfishを使う ==install== pear install Crypt_Blowfish ==sample== <?php require_once 'Crypt/Blowfish.php'; $key = "testtest"; $text = "123...」)
 
行1: 行1:
 +
 +
==AES==
 +
==smaple==
 +
AESCrypt.php
 +
<pre>
 +
class AESCrypt
 +
{
 +
    private string $key;
 +
 +
    public function __construct(string $key)
 +
    {
 +
        // 32バイト(256bit)に正規化
 +
        $this->key = hash('sha256', $key, true);
 +
    }
 +
 +
    public function encrypt(string $plainText): string
 +
    {
 +
        $iv = random_bytes(12); // GCMは12バイト推奨
 +
        $tag = '';
 +
 +
        $cipherText = openssl_encrypt(
 +
            $plainText,
 +
            'aes-256-gcm',
 +
            $this->key,
 +
            OPENSSL_RAW_DATA,
 +
            $iv,
 +
            $tag
 +
        );
 +
 +
        // iv + tag + ciphertext をまとめてbase64
 +
        return base64_encode($iv . $tag . $cipherText);
 +
    }
 +
 +
    public function decrypt(string $encrypted): string|false
 +
    {
 +
        $data = base64_decode($encrypted);
 +
 +
        $iv  = substr($data, 0, 12);
 +
        $tag = substr($data, 12, 16);
 +
        $cipherText = substr($data, 28);
 +
 +
        return openssl_decrypt(
 +
            $cipherText,
 +
            'aes-256-gcm',
 +
            $this->key,
 +
            OPENSSL_RAW_DATA,
 +
            $iv,
 +
            $tag
 +
        );
 +
    }
 +
}
 +
</pre>
 +
 +
呼び出し
 +
<pre>
 +
$key = 'your-secret-key';
 +
$crypto = new AESCrypt($key);
 +
 +
// 暗号化
 +
$encrypted = $crypto->encrypt('hello world');
 +
 +
// 復号
 +
$decrypted = $crypto->decrypt($encrypted);
 +
 +
echo $encrypted . PHP_EOL;
 +
echo $decrypted . PHP_EOL;
 +
</pre>
 +
==Blowfish==
 
Pear::Crypt_Blowfishを使う
 
Pear::Crypt_Blowfishを使う
  
==install==
+
===install===
 
  pear install Crypt_Blowfish
 
  pear install Crypt_Blowfish
  
==sample==
+
===sample===
 
  <?php
 
  <?php
 
  require_once 'Crypt/Blowfish.php';
 
  require_once 'Crypt/Blowfish.php';

2026年4月24日 (金) 00:59時点における版

AES

smaple

AESCrypt.php

class AESCrypt
{
    private string $key;

    public function __construct(string $key)
    {
        // 32バイト(256bit)に正規化
        $this->key = hash('sha256', $key, true);
    }

    public function encrypt(string $plainText): string
    {
        $iv = random_bytes(12); // GCMは12バイト推奨
        $tag = '';

        $cipherText = openssl_encrypt(
            $plainText,
            'aes-256-gcm',
            $this->key,
            OPENSSL_RAW_DATA,
            $iv,
            $tag
        );

        // iv + tag + ciphertext をまとめてbase64
        return base64_encode($iv . $tag . $cipherText);
    }

    public function decrypt(string $encrypted): string|false
    {
        $data = base64_decode($encrypted);

        $iv  = substr($data, 0, 12);
        $tag = substr($data, 12, 16);
        $cipherText = substr($data, 28);

        return openssl_decrypt(
            $cipherText,
            'aes-256-gcm',
            $this->key,
            OPENSSL_RAW_DATA,
            $iv,
            $tag
        );
    }
}

呼び出し

$key = 'your-secret-key';
$crypto = new AESCrypt($key);

// 暗号化
$encrypted = $crypto->encrypt('hello world');

// 復号
$decrypted = $crypto->decrypt($encrypted);

echo $encrypted . PHP_EOL;
echo $decrypted . PHP_EOL;

Blowfish

Pear::Crypt_Blowfishを使う

install

pear install Crypt_Blowfish

sample

<?php
require_once 'Crypt/Blowfish.php';

$key = "testtest";
$text = "1234567890";

$blowfish = new Crypt_Blowfish($key);
$encrypt = base64_encode($blowfish->encrypt($text));
$decrypt = rtrim($blowfish->decrypt(base64_decode($encrypt)), "\0");

print $encrypt;
print "\n";
print $decrypt;
print "\n";