PHP简单对称加密算法,利用字母对照表加密解密

 自己写的一个php对称加密算法,比较简单。
 解密思路(加密思路反着来):还原字母对照表——找出144位密钥字符串——得到新的字母对照表——猜对照表还原字符串
                        ——用base64解密——完成!
// 破解可能性大概最低是A(10, 64/2)x64或者A(11, 40)*64种可能,最终加密效果主要可能性取决于字母对照表。

使用:

1. 填写私有密钥和容错盐值;

2.  加密:new secret()->encode('');
     解密:new secret()->decode('');

<?php

/*
 *  《PHP对称加密算法》
 * */
class secret {

    /*
     * php对称加密算法,根据字母对照表
     * 因为最终加密的字符串长度会x2,因此,加密场景适合加密字符串不是特别巨大的地方。
     * 代码一共有三处自定义地方,根据注释更改自定义内容。
     * 现有加密破解差不多需要万(亿)亿次。
     * 2019-03-23
     * github.com/fyonecon
     *
     * 加密内容格式:字母、数字、中文等任意内容,并支持部分特殊符号
     * 加密:new Secret()->encode('');
     * 解密:new Secret()->decode('');
     *
     * */

    private $secret_key = 'view2019'; // 自定义。 密钥:字母、数字、中文等任意内容
    private $salt = 19; // 自定义。 容错度:数字范围[0, 63]
    private $s = 1; // 不要更改

    protected $_replace = [ // 自定义。 建议使用不一样的[0]对应[1],密钥对照表
        // 35789       => 01246
        // dpqyzxrtA-Z => abcefghjklmnowsijuv
        ['=', 'aio1c'], // 必须
        ['/', 'gjen1'], // 必须
        ['+', 'hbua1'], // 必须
        ['3', 'gven2'],
        ['7', '61lon'],
        ['8', 'o60oi'],
        ['9', 'huws4'],
        ['L', 'aiihn'],
        ['M', 'lmvsk'],
        ['W', 'l0ieg'],
        ['z', 'a2lfj'],
        ['y', 'a4cbs'],
        ['d', 'e1ifg'],
        ['p', 'jaio0'],
    ];

    public function __construct(){
        if (empty($this->secret_key)){
            $this->secret_key = ceil(time()/100000)*100000; // 如果没有定义密钥则使用为期一天的可变密钥
        }
        $this->secret_key = md5($this->secret_key).sha1($this->secret_key).md5($this->salt).sha1($this->salt); // len 144

    }

    public function en_mix($str){
        $key_str = $this->secret_key;
        $len1 = strlen($key_str);
        $len2 = strlen($str);

        $new_array = [];
        if ($len1<=$len2){
            for ($i=0; $i<$len1; $i++){
                $new_array[] = $str[$i];
                if ($i>=$this->s){
                    $new_array[] = $key_str[$i];
                }

            }
        }else{
            for ($i=0; $i<$len2; $i++){
                $new_array[] = $str[$i];
                if ($i>=$this->s){
                    $new_array[] = $key_str[$i];
                }
            }
        }

        $string = implode('', $new_array);
        return $string;
    }

    public function de_mix($str){
        $new_array = [];
        for ($i=0; $i<strlen($str); $i++){
            if ($i <= $this->s){
                $new_array[] = $str[$i];

            }else{
                if ($i%2 != 0){
                    $new_array[] = $str[$i];
                }
            }

        }

        $string = implode('', $new_array);
        return $string;
    }

    public function to_string($data){ // 字符串变“数字 和 字母”字符串
        $new = base64_encode($data);
        $string = '';

        $replace = $this->_replace;

        for ($i=0; $i<count($replace); $i++){
            $string = str_replace(@$replace[$i][0], @$replace[$i][1], $new);
            $new = $string;
        }

        return $this->en_mix($string);
    }

    public function to_data($string){ // 数字 和 字母”字符串变“字符串
        //$old_string = $string;
        $old_string = $this->de_mix($string);
        $data = '';

        $replace = $this->_replace;

        for ($i=0; $i<count($replace); $i++){
            $data = str_replace(@$replace[$i][1], @$replace[$i][0], $old_string);
            $old_string = $data;
        }

        return  base64_decode($data);
    }

    public function make_key_value(){ // 生成一位加密对照表
        $string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; //自定义。 不需要修改内容,但是可以修改字符数字的顺序
        $new_string = $string.$string;
        $salt = $this->salt;

        if ($salt < 0 || $salt > strlen($string)){
            $salt = ceil(strlen($string)/3);
        }

        $array1 = [];
        $array2 = [];
        for ($m=0; $m<strlen($string); $m++){
            $array1[$string[$m]] = $new_string[$m+$salt];
            $array2[$new_string[$m+$salt]] = $string[$m];
        }

        return [$array1, $array2];
    }

    public function encode($data) {
        $new_data = $this->to_string($data);
        $replace = $this->make_key_value()[0];

        $new_data_array = [];
        for ($n=0; $n<strlen($new_data); $n++){
            $new_data_array[] = $new_data[$n];
        }

        $array = [];
        for ($i=0; $i<count($new_data_array); $i++){
            $key = $new_data_array[$i];
            $value = $replace[$key];
            $array[] = $value;
        }

        $string = implode('', $array);
        return trim($string);
    }

    public function decode($data) {
        $new_data = $data;
        $replace = $this->make_key_value()[1];

        $new_data_array = [];
        for ($n=0; $n<strlen($new_data); $n++){
            $new_data_array[] = $new_data[$n];
        }

        $array = [];
        for ($i=0; $i<count($new_data_array); $i++){
            $key = $new_data_array[$i];
            $value = $replace[$key];

            $array[] = $value;
        }

        $string = implode('', $array);
        $data = $this->to_data($string);
        return trim($data);
    }

}




// 测试算法
$secret = new secret();

$res1 = $secret->encode('=你好abc123n');
echo "加密:".$res1;

echo '<hr/>';

$res2 = $secret->decode($res1);
echo "解密:".$res2;



-

-

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了多种对称加密算法来进行加密解密操作。常见的对称加密算法有AES、DES和3DES等。你可以通过安装第三方库PyCrypto来使用这些算法对称加密算法使用同样的密钥进行加密解密,密钥是控制加密解密过程的指令。算法则是一组规则,规定了加密解密的具体过程。 要使用对称加密算法,首先需要安装PyCrypto库。然后,你可以选择适合你需求的对称加密算法,并根据其对应的API进行加密解密操作。 总结起来,Python对称加密解密算法的步骤如下: 1. 安装PyCrypto库。 2. 选择合适的对称加密算法,如AES、DES或3DES。 3. 根据算法的API,使用相应的密钥和明文进行加密操作。 4. 使用相同的密钥和密文进行解密操作。 需要注意的是,对称加密算法使用相同的密钥进行加密解密,因此需要保证密钥的安全性。另外,加密解密的过程需要对应的密钥,确保能够正确地进行解密操作。 希望以上信息能够帮助到你。如果有任何疑问,请随时追问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python - 密码加密解密](https://blog.csdn.net/weixin_44462773/article/details/128854517)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值