当前位置:首页 > PHP

php实现playfair

2026-02-14 19:34:12PHP

实现 Playfair 加密算法的 PHP 代码

Playfair 是一种经典的对称加密算法,基于 5x5 的字母矩阵进行加密和解密。以下是一个完整的 PHP 实现示例:

<?php
class PlayfairCipher {
    private $key;
    private $keySquare;

    public function __construct($key) {
        $this->key = strtoupper($key);
        $this->prepareKeySquare();
    }

    private function prepareKeySquare() {
        $key = str_replace('J', 'I', $this->key);
        $key = preg_replace('/[^A-Z]/', '', $key);
        $key = str_split($key);
        $alphabet = range('A', 'Z');
        unset($alphabet[array_search('J', $alphabet)]);

        $this->keySquare = array_unique(array_merge($key, $alphabet));
        $this->keySquare = array_values($this->keySquare);
    }

    private function getPosition($char) {
        $index = array_search($char, $this->keySquare);
        return ['row' => (int)($index / 5), 'col' => $index % 5];
    }

    private function prepareText($text) {
        $text = strtoupper($text);
        $text = preg_replace('/[^A-Z]/', '', $text);
        $text = str_replace('J', 'I', $text);

        $result = '';
        $len = strlen($text);
        for ($i = 0; $i < $len; $i += 2) {
            $a = $text[$i];
            $b = ($i + 1 < $len) ? $text[$i + 1] : 'X';
            if ($a == $b) {
                $b = 'X';
                $i--;
            }
            $result .= $a . $b;
        }
        return $result;
    }

    public function encrypt($plaintext) {
        $text = $this->prepareText($plaintext);
        $ciphertext = '';

        for ($i = 0; $i < strlen($text); $i += 2) {
            $a = $text[$i];
            $b = $text[$i + 1];
            $posA = $this->getPosition($a);
            $posB = $this->getPosition($b);

            if ($posA['row'] == $posB['row']) {
                $ciphertext .= $this->keySquare[$posA['row'] * 5 + (($posA['col'] + 1) % 5)];
                $ciphertext .= $this->keySquare[$posB['row'] * 5 + (($posB['col'] + 1) % 5)];
            } elseif ($posA['col'] == $posB['col']) {
                $ciphertext .= $this->keySquare[(($posA['row'] + 1) % 5) * 5 + $posA['col']];
                $ciphertext .= $this->keySquare[(($posB['row'] + 1) % 5) * 5 + $posB['col']];
            } else {
                $ciphertext .= $this->keySquare[$posA['row'] * 5 + $posB['col']];
                $ciphertext .= $this->keySquare[$posB['row'] * 5 + $posA['col']];
            }
        }
        return $ciphertext;
    }

    public function decrypt($ciphertext) {
        $text = $this->prepareText($ciphertext);
        $plaintext = '';

        for ($i = 0; $i < strlen($text); $i += 2) {
            $a = $text[$i];
            $b = $text[$i + 1];
            $posA = $this->getPosition($a);
            $posB = $this->getPosition($b);

            if ($posA['row'] == $posB['row']) {
                $plaintext .= $this->keySquare[$posA['row'] * 5 + (($posA['col'] - 1 + 5) % 5)];
                $plaintext .= $this->keySquare[$posB['row'] * 5 + (($posB['col'] - 1 + 5) % 5)];
            } elseif ($posA['col'] == $posB['col']) {
                $plaintext .= $this->keySquare[(($posA['row'] - 1 + 5) % 5) * 5 + $posA['col']];
                $plaintext .= $this->keySquare[(($posB['row'] - 1 + 5) % 5) * 5 + $posB['col']];
            } else {
                $plaintext .= $this->keySquare[$posA['row'] * 5 + $posB['col']];
                $plaintext .= $this->keySquare[$posB['row'] * 5 + $posA['col']];
            }
        }
        return $plaintext;
    }
}

// 使用示例
$key = "MONARCHY";
$cipher = new PlayfairCipher($key);

$plaintext = "HELLO WORLD";
$encrypted = $cipher->encrypt($plaintext);
$decrypted = $cipher->decrypt($encrypted);

echo "Plaintext: $plaintext\n";
echo "Encrypted: $encrypted\n";
echo "Decrypted: $decrypted\n";
?>

Playfair 算法关键点说明

加密矩阵构建时,字母 'J' 通常被替换为 'I',矩阵由密钥和剩余字母填充,确保每个字母只出现一次。

php实现playfair

明文预处理阶段,需要将文本转换为大写,去除非字母字符,处理重复字母对(插入 'X'),并确保文本长度为偶数。

加密规则分为三种情况:同行字母右移、同列字母下移、矩形对角交换。解密则是相反方向移动。

php实现playfair

使用注意事项

密钥应选择足够长度且易记的单词或短语,避免使用包含大量重复字母的密钥。

明文中的 'J' 会被自动转换为 'I',解密后需要根据上下文判断是否需要还原为 'J'。

对于短文本或特殊字符,可能需要在加密前后进行额外处理以确保数据完整性。

标签: phpplayfair
分享给朋友:

相关文章

php实现表单

php实现表单

PHP 表单基础实现 创建一个简单的 PHP 表单需要 HTML 表单结构和 PHP 处理脚本。以下是一个完整的示例: HTML 表单部分 (form.html) <form action=…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注意…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数: $q…

php搜索功能实现的

php搜索功能实现的

PHP 搜索功能实现方法 数据库搜索实现 使用 SQL 的 LIKE 语句进行模糊匹配,适用于小型网站和简单搜索需求。 $searchTerm = $_GET['search']; $query =…

无限分类实现 php

无限分类实现 php

无限分类的实现方法 无限分类通常用于构建多层级结构的数据,如商品分类、部门架构等。以下是几种常见的实现方式: 邻接列表模式(Adjacency List) 邻接列表是最简单的实现方式,通过在每个记录…

php 实现评论

php 实现评论

实现 PHP 评论功能 数据库设计 创建数据库表存储评论数据,通常包含以下字段: id:评论唯一标识 user_id:用户ID content:评论内容 created_at:评论时间 parent…