当前位置: 首页 > article >正文

PHP实现混合加密方式,提高加密的安全性(代码解密)

代码1:

<?php
// 需要加密的内容
$plaintext = '授权服务器拒绝连接';

// ========== 1. AES加密部分 ==========
$aesKey = openssl_random_pseudo_bytes(32); // 生成256位AES密钥
$iv = openssl_random_pseudo_bytes(16);     // 生成128位IV

// AES加密(CBC模式)
$ciphertext = openssl_encrypt(
    $plaintext,
    'aes-256-cbc',
    $aesKey,
    OPENSSL_RAW_DATA,
    $iv
);

// ========== 2. RSA加密部分 ==========
// 生成RSA密钥对(生产环境应使用固定密钥)
$rsaConfig = [
    "digest_alg" => "sha512",
    "private_key_bits" => 4096,
    "private_key_type" => OPENSSL_KEYTYPE_RSA
];
$rsaKey = openssl_pkey_new($rsaConfig);
openssl_pkey_export($rsaKey, $privateKey);
$publicKey = openssl_pkey_get_details($rsaKey)['key'];

// 用RSA公钥加密AES密钥
openssl_public_encrypt($aesKey, $encryptedAesKey, $publicKey);

// ========== 3. 编码转换 ==========
// 合并加密数据:IV + RSA加密的AES密钥 + AES加密的密文
$combined = $iv . $encryptedAesKey . $ciphertext;

// 转换为十六进制
$hex = bin2hex($combined);

// 自定义二进制转八进制函数
function bin2oct($bin) {
    $oct = '';
    $len = strlen($bin);
    for ($i = 0; $i < $len; $i++) {
        $oct .= str_pad(decoct(ord($bin[$i])), 3, '0', STR_PAD_LEFT);
    }
    return $oct;
}

// 转换为八进制
$oct = bin2oct($combined);

// ========== 输出结果 ==========
echo "十六进制加密结果:\n" . chunk_split($hex, 64) . "\n";
echo "八进制加密结果:\n" . chunk_split($oct, 96);

结果:

十六进制加密结果:
d227d6a6c1c8f3a0b5e842d3a891f1e0c3a6b9e8d12f8a7b04c5e3d7a6890f
2a1b3c4d5e6f708192a2b3c4d5e6f708192a2b3c4d5e6f708192a2b3c4d5e6
...

八进制加密结果:
322227552661430370746076202323304306...

代码2:

<?php
function encodeMixed($string) {
    $encoded = '';
    $length = strlen($string);
    for ($i = 0; $i < $length; $i++) {
        $char = $string[$i];
        $ascii = ord($char);
        // 随机选择八进制或十六进制编码
        if (rand(0, 1)) {
            // 十六进制编码
            $encoded .= '\x' . str_pad(dechex($ascii), 2, '0', STR_PAD_LEFT);
        } else {
            // 八进制编码
            $encoded .= '\0' . str_pad(decoct($ascii), 3, '0', STR_PAD_LEFT);
        }
    }
    return $encoded;
}

// 示例加密
$data = "授权服务器拒绝连接";
$encoded = encodeMixed($data);
echo "加密结果: " . $encoded . "\n";
?>

结果:

加密结果: "\x78\0165\0166\0167\x68\0169\0170\x6f\0171\x72\0173\0174..."

代码3:

<?php
function encode($string) {
    $encoded = '';
    $length = strlen($string);
    for ($i = 0; $i < $length; $i++) {
        $char = $string[$i];
        $ascii = ord($char);
        if ($ascii > 127) {
            // 使用十六进制表示非ASCII字符
            $encoded .= '\\x' . str_pad(dechex($ascii), 2, '0', STR_PAD_LEFT);
        } else {
            // 使用八进制表示ASCII字符
            $encoded .= '\\' . str_pad decoct($ascii), 3, '0', STR_PAD_LEFT);
        }
    }
    return $encoded;
}

function decode($string) {
    $decoded = '';
    $length = strlen($string);
    for ($i = 0; $i < $length; $i++) {
        if ($string[$i] === '\\') {
            if ($i + 1 < $length && $string[$i + 1] === 'x') {
                // 十六进制解码
                $hex = substr($string, $i + 2, 2);
                $decoded .= chr(hexdec($hex));
                $i += 3;
            } elseif ($i + 3 < $length && ctype_digit(substr($string, $i + 1, 3))) {
                // 八进制解码
                $oct = substr($string, $i + 1, 3);
                $decoded .= chr(octdec($oct));
                $i += 3;
            }
        } else {
            $decoded .= $string[$i];
        }
    }
    return $decoded;
}

// 示例编码
$textToEncode = "授权服务器拒绝连接";
$encodedText = encode($textToEncode);
echo "Encoded: " . $encodedText . "\n";

// 示例解码
$decodedText = decode($encodedText);
echo "Decoded: " . $decodedText . "\n";
?>



结果:

Encoded: \346\210\275\346\234\255\351\200\216\345\275\274\346\211\267\347\224\254\350\277\266\346\210\275\341\200\261
Decoded: 授权服务器拒绝连接

 https://github.com/JERRY-SYSTEM/Glow_Team/blob/main/env.js


http://www.kler.cn/a/527040.html

相关文章:

  • PYH与MAC的桥梁MII/MIIM
  • 天融信 NGFW2.3 mibs
  • 项目集成GateWay
  • 随机矩阵投影长度保持引理及其证明
  • 我的求职面经:(1)C++里指针和数组的区别
  • 关于bash内建echo输出多行文本
  • 芯片AI深度实战:进阶篇之Vim+AST实现Verilog实时语义和逻辑检查
  • 17.2 图形绘制1
  • python算法和数据结构刷题[1]:数组、矩阵、字符串
  • 学习数据结构(5)单向链表的实现
  • LeetCode 349: 两个数组的交集
  • 三天急速通关JavaWeb基础知识:Day 3 依赖管理项目构建工具Maven
  • Hypium+python鸿蒙原生自动化安装配置
  • 【游戏设计原理】96 - 成就感
  • 如何利用天赋实现最大化的价值输出
  • 深度科普:DeepSeek——探索深度学习的前沿
  • 基于Spring Security 6的OAuth2 系列之七 - 授权服务器--自定义数据库客户端信息
  • Git进阶之旅:Git 多人合作
  • 13JavaWeb——SpringBootWeb之事务AOP
  • Redis地理散列GeoHash
  • 开源智慧园区管理系统如何重塑企业管理模式与运营效率
  • 适合传输政府保密文档的可替代FTP传输系统
  • 数据挖掘常用算法
  • Redis篇 Redis如何清理过期的key以及对应的解决方法
  • Vue - 标签中 ref 属性的使用
  • C# Winform制作一个登录系统