ivinco/crypto-ff3

PHP上的FF3格式保持加密

1.1 2024-05-16 16:42 UTC

This package is not auto-updated.

Last update: 2024-09-19 18:28:41 UTC


README

概览

FF3Cipher是FPE(格式保持加密)算法的PHP实现。

与其他PHP实现不同,此库能够使用多字节字符字典。

随着数据保护需求的增长和PHP在Web应用程序中的广泛应用,本项目旨在填补这一差距,使开发者能够在PHP应用程序中轻松使用FF3加密。

安装

安装FF3Cipher推荐使用Composer

composer require ivinco/crypto-ff3

确保您的项目composer.json和相关锁文件已更新。

用法

基本加密和解密

use Ivinco\Crypto\FF3Cipher;

$key = "EF4359D8D580AA4F7F036D6F04FC6A94"; // Your encryption key
$tweak = "D8E7920AFA330A73"; // Your tweak

$cipher = new FF3Cipher($key, $tweak);

$plaintext = "1234567890";
$ciphertext = $cipher->encrypt($plaintext);
echo "Ciphertext: " . $ciphertext . PHP_EOL;

$decrypted = $cipher->decrypt($ciphertext);
echo "Decrypted: " . $decrypted . PHP_EOL;

使用自定义字母表

在某些场景中,您可能想使用非标准字符。此库支持使用自定义字母表进行加密和解密

$alphabet = "abcdefghijklmnopqrstuvwxyz"; // Custom alphabet
$cipher = FF3Cipher::withCustomAlphabet($key, $tweak, $alphabet);

$plaintext = "wfmwlrorcd";
$ciphertext = $cipher->encrypt($plaintext);
echo "Ciphertext: " . $ciphertext . PHP_EOL; // ywowehycyd

$decrypted = $cipher->decrypt($ciphertext); // wfmwlrorcd
echo "Decrypted: " . $decrypted . PHP_EOL;

测试

此库附带单元测试。

通过Composer安装所需包

composer install

执行PHPUnit

./vendor/bin/phpunit

链接

有关其他语言的FF3实现,您可以参考

有关FF3的更多信息,您可以参考