Issues (54)

src/Tools/DES.php (3 issues)

1
<?php
2
/**
3
 * 参考来源
4
 * http://www.mayanlong.com/archives/2018/des.html#comment-1147
5
 */
6
namespace tinymeng\Chinaums\Tools;
7
8
/**
9
 * openssl 实现的 DES 加密类,支持各种 PHP 版本
10
 */
11
class DES
12
{
13
    /**
14
     * @var string $method 加解密方法,可通过 openssl_get_cipher_methods() 获得
15
     */
16
    protected $method;
17
18
    /**
19
     * @var string $key 加解密的密钥
20
     */
21
    protected $key;
22
23
    /**
24
     * @var string $output 输出格式 无、base64、hex
25
     */
26
    protected $output;
27
28
    /**
29
     * @var string $iv 加解密的向量
30
     */
31
    protected $iv;
32
33
    /**
34
     * @var string $options
35
     */
36
    protected $options;
37
38
    // output 的类型
39
    const OUTPUT_NULL = '';
40
    const OUTPUT_BASE64 = 'base64';
41
    const OUTPUT_HEX = 'hex';
42
43
44
    /**
45
     * DES constructor.
46
     * @param string $key
47
     * @param string $method
48
     *      ECB DES-ECB、DES-EDE3 (为 ECB 模式时,$iv 为空即可)
49
     *      CBC DES-CBC、DES-EDE3-CBC、DESX-CBC
50
     *      CFB DES-CFB8、DES-EDE3-CFB8
51
     *      CTR
52
     *      OFB
53
     *
54
     * @param string $output
55
     *      base64、hex
56
     *
57
     * @param string $iv
58
     * @param int $options
59
     */
60
    public function __construct($key, $method = 'DES-ECB', $output = '', $iv = '', $options = OPENSSL_RAW_DATA | OPENSSL_NO_PADDING)
61
    {
62
        $this->key = $key;
63
        $this->method = $method;
64
        $this->output = $output;
65
        $this->iv = $iv;
66
        $this->options = $options;
67
    }
68
69
    /**
70
     * 加密
71
     *
72
     * @param $str
73
     * @return string
74
     */
75
    public function encrypt($str)
76
    {
77
        $str = $this->pkcsPadding($str, 8);
78
        $sign = openssl_encrypt($str, $this->method, $this->key, $this->options, $this->iv);
0 ignored issues
show
$this->options of type string is incompatible with the type integer expected by parameter $options of openssl_encrypt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        $sign = openssl_encrypt($str, $this->method, $this->key, /** @scrutinizer ignore-type */ $this->options, $this->iv);
Loading history...
79
80
        if ($this->output == self::OUTPUT_BASE64) {
81
            $sign = base64_encode($sign);
82
        } else if ($this->output == self::OUTPUT_HEX) {
83
            $sign = bin2hex($sign);
84
        }
85
86
        return $sign;
87
    }
88
89
    /**
90
     * 解密
91
     *
92
     * @param $encrypted
93
     * @return string
94
     */
95
    public function decrypt($encrypted)
96
    {
97
        if ($this->output == self::OUTPUT_BASE64) {
98
            $encrypted = base64_decode($encrypted);
99
        } else if ($this->output == self::OUTPUT_HEX) {
100
            $encrypted = hex2bin($encrypted);
101
        }
102
103
        $sign = @openssl_decrypt($encrypted, $this->method, $this->key, $this->options, $this->iv);
0 ignored issues
show
$this->options of type string is incompatible with the type integer expected by parameter $options of openssl_decrypt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $sign = @openssl_decrypt($encrypted, $this->method, $this->key, /** @scrutinizer ignore-type */ $this->options, $this->iv);
Loading history...
104
        $sign = $this->unPkcsPadding($sign);
105
        $sign = rtrim($sign);
106
        return $sign;
107
    }
108
109
    /**
110
     * 填充
111
     *
112
     * @param $str
113
     * @param $blocksize
114
     * @return string
115
     */
116
    private function pkcsPadding($str, $blocksize)
117
    {
118
        $pad = $blocksize - (strlen($str) % $blocksize);
119
        return $str . str_repeat(chr($pad), $pad);
120
    }
121
122
    /**
123
     * 去填充
124
     *
125
     * @param $str
126
     * @return string
127
     */
128
    private function unPkcsPadding($str)
129
    {
130
        $pad = ord($str[strlen($str) - 1]);
131
        if ($pad > strlen($str)) {
132
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
133
        }
134
        return substr($str, 0, -1 * $pad);
135
    }
136
137
}