PHP对称加密算法(变形Unicode)

-

已有一个基于base64的变形加密算法https://blog.csdn.net/weixin_41827162/article/details/88744922  。但是应用过程中,需要避开接口关键词审查,服务器数据审查,就需要一个加密简单且不需要加密登记高的算法。

Unicode加密与解密+数字字母对照表,实现基于Unicode的变形加密算法。

对照表约难,加密等级越高。

-

<?php

/*
 *  《PHP对称加密算法-变形unicode》
 * */

namespace App\Http\Kit;

use Exception;

class SecretUnicode {

    /*
     * 缺点:体积变大几乎3倍
     *
     * 用于加密文章、url
     *
     * 2020-02-20
     * github.com/fyonecon
     *
     * 加密内容格式:字母、数字、中文等任意内容,并支持部分特殊符号
     * 加密:new SecretUnicode()->encode_unicode('');
     * 解密:new SecretUnicode()->decode_unicode('');
     *
     * */

    // Unicode编码替换对照表
    public function remix_unicode($string, $way=''){
        $replace = [ // PHP专用
            // must
            '\\u'=> 'x', // PHP解析专用

            '1' => 'e',
            '20'=> 'o',

            // other
            '3' => 'c',

        ];

        foreach ($replace as $true_alp => $stunt_alp){
            if ($way == 'encode'){
                $string = str_replace($true_alp, $stunt_alp, $string);
            }else if ($way == 'decode'){
                $string = str_replace($stunt_alp, $true_alp, $string);
            }
        }

        return $string;
    }

    // 解码Unicode
    public function decode_unicode($string='', $encoding='utf-8', $prefix='\\u', $postfix='') {

        $string = $this->remix_unicode($string, 'decode');

        if (!empty($string)){
            $arruni = explode($prefix, $string);
            $string = '';
            for($i = 1, $len = count($arruni); $i < $len; $i++) {
                if (strlen($postfix) > 0) {
                    $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
                }
                $temp = intval($arruni[$i]);
                $string .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
            }
        }

        $string = iconv('UCS-2', $encoding, $string);

        return $string;
    }

    // 将任意字母、数字、特殊符号、中文编成Unicode
    public function encode_unicode($string='', $encoding='utf-8', $prefix='\\u', $postfix='') {
        if (!empty($string)){
            $string = iconv($encoding, 'UCS-2', $string);
            $arrstr = str_split($string, 2);
            $string = '';
            for($i = 0, $len = count($arrstr); $i < $len; $i++) {
                $dec = hexdec(bin2hex($arrstr[$i]));
                $string .= $prefix . $dec . $postfix;
            }
        }

        $string = $this->remix_unicode($string, 'encode');

        return $string;
    }

}

-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值