zoukankan      html  css  js  c++  java
  • 加密解密


    /**
    * api 加密接口,轻量级加密算法
    */
    function erp_api_encrypt($string) {

    $encryptKey = md5('test');
    $keyLen = strlen($encryptKey);

    $data = substr(md5($string.$encryptKey), 0, 8).$string;
    $dataLen = strlen($data);

    $rndkey = array();
    $box = array();
    $cipherText = "";

    for ($i = 0; $i < 256; $i++) {
    $rndkey[$i] = ord($encryptKey[$i % $keyLen]);
    $box[$i] = $i;
    }

    for ($i = 0, $j = 0; $i < 256; $i++) {
    $j = ($j + $box[$i] + $rndkey[$i]) % 256;
    $tmp = $box[$i];
    $box[$i] = $box[$j];
    $box[$j] = $tmp;
    }

    for ($i = 0, $j = 0, $k = 0; $i < $dataLen; $i++) {
    $k = ($k + 1) % 256;
    $j = ($j + $box[$k]) % 256;
    $tmp = $box[$k];
    $box[$k] = $box[$j];
    $box[$j] = $tmp;
    $cipherText .= chr(ord($data[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
    }

    return str_replace('=', '', base64_encode($cipherText));
    }

    /**
    * api 解密接口,轻量级解密算法
    */
    function erp_api_decrypt($cipherText) {

    $encryptKey = md5('test');
    $keyLen = strlen($encryptKey);

    $cipherText = base64_decode($cipherText);
    $textLen = strlen($cipherText);

    $rndkey = array();
    $box = array();
    $decryptText = "";

    for ($i = 0; $i < 256; $i++) {
    $rndkey[$i] = ord($encryptKey[$i % $keyLen]);
    $box[$i] = $i;
    }

    for ($i = 0, $j = 0; $i < 256; $i++) {
    $j = ($j + $box[$i] + $rndkey[$i]) % 256;
    $tmp = $box[$i];
    $box[$i] = $box[$j];
    $box[$j] = $tmp;
    }

    for ($i = 0, $j = 0, $k = 0; $i < $textLen; $i++) {
    $k = ($k + 1) % 256;
    $j = ($j + $box[$k]) % 256;
    $tmp = $box[$k];
    $box[$k] = $box[$j];
    $box[$j] = $tmp;
    $decryptText .= chr(ord($cipherText[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
    }

    if (substr($decryptText, 0, 8) == substr(md5(substr($decryptText, 8).$encryptKey), 0, 8)) {
    return substr($decryptText, 8);
    } else {
    return false;
    }
    }


    /**
    * api 加密接口,轻量级加密算法 key值不固定,copy erp_api_encrypt
    */
    function api_encrypt($string,$key='') {

    //$encryptKey = md5('test');
    $encryptKey = md5($key);
    $keyLen = strlen($encryptKey);

    $data = substr(md5($string.$encryptKey), 0, 8).$string;
    $dataLen = strlen($data);

    $rndkey = array();
    $box = array();
    $cipherText = "";

    for ($i = 0; $i < 256; $i++) {
    $rndkey[$i] = ord($encryptKey[$i % $keyLen]);
    $box[$i] = $i;
    }

    for ($i = 0, $j = 0; $i < 256; $i++) {
    $j = ($j + $box[$i] + $rndkey[$i]) % 256;
    $tmp = $box[$i];
    $box[$i] = $box[$j];
    $box[$j] = $tmp;
    }

    for ($i = 0, $j = 0, $k = 0; $i < $dataLen; $i++) {
    $k = ($k + 1) % 256;
    $j = ($j + $box[$k]) % 256;
    $tmp = $box[$k];
    $box[$k] = $box[$j];
    $box[$j] = $tmp;
    $cipherText .= chr(ord($data[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
    }

    return str_replace('=', '', base64_encode($cipherText));
    }

    /**
    * api 解密接口,轻量级解密算法 key值不固定,copy erp_api_decrypt
    */
    function api_decrypt($cipherText,$key='') {

    //$encryptKey = md5('test');
    $encryptKey = md5($key);
    $keyLen = strlen($encryptKey);

    $cipherText = base64_decode($cipherText);
    $textLen = strlen($cipherText);

    $rndkey = array();
    $box = array();
    $decryptText = "";

    for ($i = 0; $i < 256; $i++) {
    $rndkey[$i] = ord($encryptKey[$i % $keyLen]);
    $box[$i] = $i;
    }

    for ($i = 0, $j = 0; $i < 256; $i++) {
    $j = ($j + $box[$i] + $rndkey[$i]) % 256;
    $tmp = $box[$i];
    $box[$i] = $box[$j];
    $box[$j] = $tmp;
    }

    for ($i = 0, $j = 0, $k = 0; $i < $textLen; $i++) {
    $k = ($k + 1) % 256;
    $j = ($j + $box[$k]) % 256;
    $tmp = $box[$k];
    $box[$k] = $box[$j];
    $box[$j] = $tmp;
    $decryptText .= chr(ord($cipherText[$i]) ^ ($box[($box[$k] + $box[$j]) % 256]));
    }

    if (substr($decryptText, 0, 8) == substr(md5(substr($decryptText, 8).$encryptKey), 0, 8)) {
    return substr($decryptText, 8);
    } else {
    return false;
    }
    }
  • 相关阅读:
    手写vector
    Linux文件寻址算法:逻辑地址到物理地址的转换
    东北大学操作系统实验:进程同步和通信(生产者消费者问题模拟)
    操作系统页面置换算法之FIFO,LRU
    火车进站
    进程状态转换及其PCB的变化
    iOS ViewController里代码结构的规定
    mac下搭建https服务器
    openssl生成证书
    mac下 apacha tomcat相关操作
  • 原文地址:https://www.cnblogs.com/brady-wang/p/9908486.html
Copyright © 2011-2022 走看看