PHP 數據加密工具類

PHP 數據加密工具類php

最近在寫一個須要加密傳輸數據的小項目,順便把用的加密方法封裝成一個工具類,其實就是按字節位把ASCII碼進行位運算json

加密解密工具類

<?php
/**
 * @date 2016-06-20
 * @author sallency
 * 數據傳輸加密工具類
 */

class DataSecurityTool
{
    private $key_main = [];
    private $key_sub  = [];

    public function __construct($key_main = "this is main key", $key_sub = "this is sub key")
    {
        $this->key_main = str_split($key_main);
        $this->key_sub  = str_split($key_sub);
    }

    /**
     * 加密函數
     * @param  [type] $context [description]
     * @return [type]          [description]
     */
    public function opEncryption($context)
    {
        $context_bytes = str_split($context);
        $context_len   = count($context_bytes);
        $cur_key_len   = count($this->key_main);

        if (empty($context_bytes)) {
            return null;
        }

        $context_encrypted = [];

        for ($i = 0; $i < $context_len; $i ++) {
            $context_encrypted[$i] = ord($context_bytes[$i] ^ $this->key_main[$i % $cur_key_len]);
        }

        $cur_key_len = count($this->key_sub);

        for ($i = 0; $i < $context_len ; $i ++) {
            $context_encrypted[$i] = $context_encrypted[$i] ^ ord($this->key_sub[$i % $cur_key_len]);
        }

        $data = array_map(function($v) {
            return chr($v);
        }, $context_encrypted);
        
        return base64_encode(implode($data));
    }

    /**
     * 解密函數
     * @param  [type] $context [description]
     * @return [type]          [description]
     */
    public function opDecryption($context)
    {
        $context = base64_decode($context);

        $context_bytes = str_split($context);
        $context_len = count($context_bytes);
        $cur_key_len = count($this->key_sub);

        if (empty($context_bytes)) {
            return null;
        }

        $context_encrypted = [];

        for ($i = 0; $i < $context_len; $i ++) {
            $context_encrypted[$i] = ord($context_bytes[$i] ^ $this->key_sub[$i % $cur_key_len]);
        }

        $cur_key_len = count($this->key_main);

        for ($i = 0; $i < $context_len; $i ++) {
            $context_encrypted[$i] = $context_encrypted[$i] ^ ord($this->key_main[$i % $cur_key_len]);
        }

        $data = array_map(function($char) {
            return chr($char);
        }, $context_encrypted);

        return implode("", $data);
    }
}

使用方法函數

<?php
//data for encrypt
$info = [
    "access_token" => "as2009_12_34",
    "name" => "個人",
    "age"  => 25,
    "sex"  => "male"
];
$json_data = json_encode($info, JSON_UNESCAPED_UNICODE);

$dataSec = new DataSecurityTool();

//encrypt
$encry_data = $dataSec->opEncryption($json_data);
echo $encry_data;

//decrypt
$decry_data = $dataSec->opDecryption($encry_data);
echo $decry_data;
?>

注意若是傳輸的是 json 字符串的時候編碼時json默認會將中文轉碼爲unicode編碼工具

若是是 php 5.4 能夠方便的調用 JSON_UNESCAPED_UNICODE 選項告訴 json_encode 不要只能轉碼this

若是是 php 5.4 如下的版本能夠利用 urlencode 作一些封裝編碼

解決json中文亂碼

<?php
/**
 * 經過 urlencode 將中文編碼
 * @param  array  $json_arr [description]
 * @return [type]           [description]
 */
function znJsonEncode(array $json_arr)
{
    $temp = array_map(function($row) {
        return urlencode($row);
    }, $json_arr);
    return json_encode($temp);
}

/**
 * 先 urldecode
 * @param  [type]  $json_str [description]
 * @param  boolean $arr      [description]
 * @return [type]            [description]
 */
function znJsonDecode($json_str, $arr = false)
{
    return json_decode(urldecode($json_str), $arr);
}

便可完成中文的傳輸和解碼加密

相關文章
相關標籤/搜索