首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 實作 RC4 加密演算法:對稱加密與解密完整代碼

     編輯:PHP 相關知識

RC4 是經典的流式加密演算法,雖已知安全性弱點(不建議用於新系統),但理解其原理對維護舊系統、學習密碼學仍有價值。本文提供標準 PHP 實作。

RC4 核心特性

  • 對稱加密:加密與解密使用相同函數
  • 流式密碼:逐位元組加密,資料長度不變
  • 金鑰長度:1-256 bytes
  • 已知弱點:初始關鍵字偏移、相關性攻擊,TLS/SSL 已禁用

標準 PHP 實作

<?php
/**
 * RC4 加密/解密(對稱操作)
 * 
 * @param string $key   加密金鑰 (1-256 位元組)
 * @param string $data  明文或密文
 * @return string       密文或明文
 */
function rc4(string $key, string $data): string {
    if ($key === '' || $data === '') return $data;

    $keyLength = strlen($key);
    $dataLength = strlen($data);

    // 1. 初始化 S-box (0-255)
    $s = range(0, 255);
    $keyBytes = [];
    for ($i = 0; $i < 256; $i++) {
        $keyBytes[$i] = ord($key[$i % $keyLength]);
    }

    // 2. KSA: Key Scheduling Algorithm (打亂 S-box)
    $j = 0;
    for ($i = 0; $i < 256; $i++) {
        $j = ($j + $s[$i] + $keyBytes[$i]) % 256;
        [$s[$i], $s[$j]] = [$s[$j], $s[$i]];
    }

    // 3. PRGA: Pseudo-Random Generation Algorithm (產生金鑰流並加密)
    $i = $j = 0;
    $result = '';
    for ($k = 0; $k < $dataLength; $k++) {
        $i = ($i + 1) % 256;
        $j = ($j + $s[$i]) % 256;
        [$s[$i], $s[$j]] = [$s[$j], $s[$i]];
        
        $keystreamByte = $s[($s[$i] + $s[$j]) % 256];
        $result .= chr(ord($data[$k]) ^ $keystreamByte);
    }

    return $result;
}

// 使用範例
$key = 'SecretKey123';
$plaintext = 'Hello, RC4 Encryption!';

$ciphertext = rc4($key, $plaintext);
$decrypted = rc4($key, $ciphertext); // 相同函數解密

echo "明文: $plaintext\n";
echo "密文: " . bin2hex($ciphertext) . "\n"; // 顯示十六進位
echo "解密: $decrypted\n";
echo "驗證: " . ($plaintext === $decrypted ? '成功' : '失敗') . "\n";
?>

安全性改良版:RC4-drop[n]

丟棄前 N 位元組金鑰流(建議 N ≥ 768,通常 1024 或 3072)可緩解初始偏移弱點:

function rc4Drop(string $key, string $data, int $drop = 1024): string {
    // ... KSA 相同 ...
    
    // PRGA: 先丟棄 $drop 位元組
    $i = $j = 0;
    for ($k = 0; $k < $drop; $k++) {
        $i = ($i + 1) % 256;
        $j = ($j + $s[$i]) % 256;
        [$s[$i], $s[$j]] = [$s[$j], $s[$i]];
    }
    
    // 正式加密
    $result = '';
    for ($k = 0; $k < strlen($data); $k++) {
        $i = ($i + 1) % 256;
        $j = ($j + $s[$i]) % 256;
        [$s[$i], $s[$j]] = [$s[$j], $s[$i]];
        $result .= chr(ord($data[$k]) ^ $s[($s[$i] + $s[$j]) % 256]);
    }
    return $result;
}
?>

現代替代方案建議

演算法PHP 實作用途
AES-256-GCMopenssl_encrypt($data, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag)機密資料加密(推薦)
ChaCha20-Poly1305sodium_crypto_aead_chacha20poly1305_ietf_encrypt()高效能、抗旁路攻擊
XChaCha20-Poly1305sodium_crypto_aead_xchacha20poly1305_ietf_encrypt()長隨機 Nonce 安全

完整 AES-GCM 範例(生產環境標準)

<?php
function aesEncrypt(string $data, string $key): string {
    $iv = random_bytes(12); // GCM 建議 96-bit IV
    $ciphertext = openssl_encrypt($data, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag, '', 16);
    return base64_encode($iv . $tag . $ciphertext);
}

function aesDecrypt(string $payload, string $key): ?string {
    $data = base64_decode($payload);
    if (strlen($data) < 12 + 16) return null;
    
    $iv = substr($data, 0, 12);
    $tag = substr($data, 12, 16);
    $ciphertext = substr($data, 28);
    
    return openssl_decrypt($ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag, '', 16);
}
?>
⚠️ 生產環境禁用 RC4
RC4 已被 IETF (RFC 7465) 禁止用於 TLS。新系統請使用 AES-GCM 或 ChaCha20-Poly1305 (libsodium/openssl)。本文僅供教學、舊系統維護參考。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved