[PHP] TripleDES + CBC + PKCS7 加密&解密代码

January 8, 2019 奇怪的代码

找了好久,终于找到了一个能用的代码。。。

<?php
class Cryptogram {
    /**
     * 使用3DES加密源数据
     * @param string $oriSource 源数据
     * @param string $key       密钥
     * @param string $defaultIV 加解密向量
     * @return string $result   密文
     */
    public function encryptByTDES($oriSource, $key, $defaultIV){
        $oriSource = $this->addPKCS7Padding($oriSource);
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($td, $key, $defaultIV);
        $result = mcrypt_generic($td, $oriSource);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $result;
    }
    
    
    /**
     * 使用3DES解密密文
     * @param string $encryptedData 密文
     * @param string $key           密钥
     * @param string $defaultIV     加解密向量
     * @return string $result       解密后的原文
     */
    public function decryptByTDES($encryptedData, $key, $defaultIV){
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($td, $key, $defaultIV);
        $result = mdecrypt_generic($td, $encryptedData);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $result = $this->stripPKSC7Padding($result);
        return $result;
    }
    
    
    /**
     * 为字符串添加PKCS7 Padding 
     * @param string $source    源字符串
     */
    private function addPKCS7Padding($source){
    $block = mcrypt_get_block_size('tripledes', 'cbc');
    $pad = $block - (strlen($source) % $block);
    if ($pad <= $block) {
        $char = chr($pad);
        $source .= str_repeat($char, $pad);
    }
    return $source;
    }
    
    
    /**
     * 去除字符串末尾的PKCS7 Padding
     * @param string $source    带有padding字符的字符串
     */
    public function stripPKSC7Padding($source){
        $block = mcrypt_get_block_size('tripledes', 'cbc');
    $char = substr($source, -1, 1);
    $num = ord($char);
    if($num > 8){
        return $source;
    }
    $len = strlen($source);
    for($i = $len - 1; $i >= $len - $num; $i--){
        if(ord(substr($source, $i, 1)) != $num){
            return $source;
        }
    }
    $source = substr($source, 0, -$num);
    return $source;
    }
}
?>

使用方法

<?php
$keyForTDES = "XXXXXXX";//加密所用密钥
$defaultIV = "XXXXXXX";//初始化向量IV
$data = "XXXXXXX";//加密内容
$helper = new Cryptogram();
$encrypted = base64_encode($helper->encryptByTDES($data, $keyForTDES, $defaultIV));
echo "encrypt(base64): {$encrypted},len: ",strlen($encrypted),"\r\n";
$decrypted = $helper->decryptByTDES(base64_decode($encrypted), $keyForTDES, $defaultIV);
echo "decrypt: {$encrypted},len: ",strlen($encrypted),"\r\n";
?>

原文链接 点我查看

添加新评论