<?php
namespace lib;
class DesEncrypt {
//密钥
private $secretKey;
/*
* 构造方法
*/
public function __construct($secretKey = '') {
$this->secretKey = $secretKey;
$this->pkcSecretKey();
}
/**
* 加密
*/
public function encrypt($data) {
$data = mb_convert_encoding($data, 'UTF-8');
$encry = openssl_encrypt($data, 'DES-ECB', $this->secretKey, OPENSSL_RAW_DATA);
return base64_encode($encry);
}
/**
* 解密
*/
public function decrypt($data) {
$decry = openssl_decrypt(base64_decode($data), 'DES-ECB', $this->secretKey, OPENSSL_RAW_DATA);
return $decry;
}
/**
* 填充密钥
*/
private function pkcSecretKey() {
$key = $this->secretKey;
$len = strlen($key);
if (null == $key || $len < 8) {
$key = str_pad($key, 8, '0', STR_PAD_RIGHT);
}
if ($len > 8) {
$key = substr($key, 0, 8);
}
$key = mb_convert_encoding($key, 'UTF-8');
$this->secretKey = $key;
}
}
thinkphp自定义DES加密解密类
于 2022-01-09 18:00:37 首次发布