Files
yuyin-php/application/common/yun/util/DesUtil.php

60 lines
1.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\yun\util;
class DesUtil
{
/**
* 密钥向量
* @var string
*/
private $des3key;
/**
* 混淆向量
* @var string|null
*/
private $iv;
/**
* 构造传递⼆个已经进⾏base64_encode的KEY与IV
*
* @param string $des3key
* @param string $iv
*/
function __construct($des3key, $iv = null)
{
$this->des3key = $des3key;
$this->iv = $iv;
}
/**
* 加密
* @param <type> $value
* @return <type>
*/
public function encrypt($value)
{
$iv = substr($this->des3key, 0, 8);
$ret = openssl_encrypt($value, 'DES-EDE3-CBC', $this->des3key, 0, $iv);
if (false === $ret) {
return openssl_error_string();
}
return $ret;
}
/**
* 解密
* @param <type> $value
* @return <type>
*/
public function decrypt($value)
{
$iv = substr($this->des3key, 0, 8);
$ret = openssl_decrypt($value, 'DES-EDE3-CBC', $this->des3key, 0, $iv);
if (false === $ret) {
return openssl_error_string();
}
return $ret;
}
}