first commit

This commit is contained in:
annnj-company
2026-04-17 18:29:53 +08:00
parent e49fa5a215
commit 130c1026c4
5615 changed files with 1639145 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
<?php
namespace app\lib;
class DesCrypto
{
private $key;
private $iv;
/**
* 构造函数(集成固定密钥和向量)
*/
public function __construct($key, $iv)
{
// 使用您提供的固定密钥
$this->key = $this->padToLength($key, 8);
// 使用您提供的固定向量
$this->iv = $this->padToLength($iv, 8);
}
/**
* DES加密CBC模式 + PKCS5Padding
* @param string $data 待加密数据
* @return string Base64编码的加密结果
*/
public function encrypt($data)
{
// 添加PKCS5填充
$blockSize = 8;
$pad = $blockSize - (strlen($data) % $blockSize);
$data .= str_repeat(chr($pad), $pad);
// 执行加密
$encrypted = openssl_encrypt(
$data,
'DES-CBC',
$this->key,
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING,
$this->iv
);
return base64_encode($encrypted);
}
/**
* DES解密
* @param string $base64Data Base64编码的加密数据
* @return string 解密后的原始数据
*/
public function decrypt($base64Data)
{
$encrypted = base64_decode($base64Data);
// 执行解密
$decrypted = openssl_decrypt(
$encrypted,
'DES-CBC',
$this->key,
OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING,
$this->iv
);
// 移除PKCS5填充
$pad = ord($decrypted[strlen($decrypted) - 1]);
return substr($decrypted, 0, -$pad);
}
/**
* 辅助方法:填充/截断字符串到指定长度
* @param string $str 输入字符串
* @param int $length 目标长度
* @return string 处理后的字符串
*/
private function padToLength($str, $length)
{
if (strlen($str) > $length) {
return substr($str, 0, $length);
}
return str_pad($str, $length, "\0");
}
}