Completed
Push — master ( 4051f5...1a3481 )
by recca
02:04
created

Encrypter::decryptByPHP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PayumTW\Mypay;
4
5
use phpseclib\Crypt\AES;
6
7
class Encrypter
8
{
9
    /**
10
     * $cipher.
11
     *
12
     * @var \phpseclib\Crypt\AES
13
     */
14
    protected $cipher;
15
16
    /**
17
     * $key.
18
     *
19
     * @var string
20
     */
21
    protected $key;
22
23
    /**
24
     * __construct.
25
     *
26
     * @param string $key
27
     * @param \phpseclib\Crypt\AES $cipher
28
     */
29 3
    public function __construct($key, AES $cipher = null)
30
    {
31 3
        $this->key = $key;
32 3
        $this->cipher = $cipher ?: new AES(AES::MODE_CBC);
33 3
    }
34
35
    /**
36
     * setKey.
37
     *
38
     * @param string $key
39
     * @return static
40
     */
41 2
    public function setKey($key)
42
    {
43 2
        $this->key = $key;
44
45 2
        return $this;
46
    }
47
48
    /**
49
     * encrypt.
50
     *
51
     * @param array $params
52
     * @return string
53
     */
54 2
    public function encrypt($params)
55
    {
56 2
        $plaintext = json_encode($params);
57 2
        $this->cipher->setKey($this->key);
58 2
        $result = $this->cipher->encrypt($plaintext);
59 2
        $iv = $this->cipher->encryptIV;
60
61 2
        return base64_encode($iv.$result);
62
    }
63
64
    /**
65
     * encryptByPHP.
66
     *
67
     * @param array $params
68
     * @return string
69
     */
70 1
    public function encryptByPHP($params)
71
    {
72 1
        $plaintext = json_encode($params);
73 1
        $size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CBC);
74 1
        $iv = mcrypt_create_iv($size, MCRYPT_DEV_URANDOM);
75 1
        $padding = 16 - (strlen($plaintext) % 16);
76 1
        $plaintext .= str_repeat(chr($padding), $padding);
77 1
        $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $plaintext, MCRYPT_MODE_CBC, $iv);
78
79 1
        return base64_encode($iv.$ciphertext);
80
    }
81
82
    /**
83
     * decrypt.
84
     *
85
     * @param string $ciphertext
86
     * @return array
87
     */
88 1
    public function decrypt($ciphertext)
89
    {
90 1
        $encryptWithIV = base64_decode($ciphertext);
91 1
        $iv = substr($encryptWithIV, 0, 16);
92 1
        $ciphertext = substr($encryptWithIV, 16);
93 1
        $this->cipher->setKey($this->key);
94 1
        $this->cipher->setIV($iv);
95
96 1
        return json_decode($this->cipher->decrypt($ciphertext), true);
97
    }
98
99
    /**
100
     * decryptByPHP.
101
     *
102
     * @param string $ciphertext
103
     * @return array
104
     */
105 1
    public function decryptByPHP($ciphertext)
106
    {
107 1
        $encryptWithIV = base64_decode($ciphertext);
108 1
        $iv = substr($encryptWithIV, 0, 16);
109 1
        $ciphertext = substr($encryptWithIV, 16);
110
111 1
        return json_decode(
112 1
            $this->pkcs5Unpad(
113 1
                mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $ciphertext, MCRYPT_MODE_CBC, $iv)
114 1
            ),
115
            true
116 1
        );
117
    }
118
119
    /**
120
     * encryptRequest.
121
     *
122
     * @param string $storeUid
123
     * @param array $params
124
     * @param string $cmd
125
     * @param string $serviceName
126
     * @return array
127
     */
128 1
    public function encryptRequest($storeUid, $params, $cmd = 'api/orders', $serviceName = 'api')
129
    {
130
        return [
131 1
            'store_uid' => $storeUid,
132 1
            'service' => $this->encrypt([
133 1
                'service_name' => $serviceName,
134 1
                'cmd' => $cmd,
135 1
            ]),
136 1
            'encry_data' => $this->encrypt($params),
137 1
        ];
138
    }
139
140
    /**
141
     * pkcs5Unpad.
142
     *
143
     * @param string $plaintext
144
     * @return string
145
     */
146 1
    protected function pkcs5Unpad($plaintext)
147
    {
148 1
        $pad = ord($plaintext[strlen($plaintext) - 1]);
149 1
        if ($pad > strlen($plaintext)) {
150
            return false;
151
        }
152
153 1
        if (strspn($plaintext, chr($pad), strlen($plaintext) - $pad) != $pad) {
154
            return false;
155
        }
156
157 1
        return substr($plaintext, 0, -1 * $pad);
158
    }
159
}
160