1 | <?php |
||
15 | class Crypto |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $cipher; |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $key; |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $iv; |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $certificate; |
||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $fingerprint; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $keyencrypted; |
||
41 | /** |
||
42 | * constants |
||
43 | */ |
||
44 | const AES_128_CBC = 'aes-128-cbc'; |
||
45 | const AES_128_CBF = 'aes-128-cfb'; |
||
46 | const AES_128_CBF1 = 'aes-128-cfb1'; |
||
47 | const AES_128_CBF8 = 'aes-128-cfb8'; |
||
48 | const AES_128_OFB = 'aes-128-ofb'; |
||
49 | const AES_192_CBC = 'aes-192-cbc'; |
||
50 | const AES_192_CBF = 'aes-192-cbf'; |
||
51 | const AES_192_CBF1 = 'aes-192-cbf1'; |
||
52 | const AES_192_CBF8 = 'aes-192-cbf8'; |
||
53 | const AES_192_OFB = 'aes-192-ofb'; |
||
54 | const AES_256_CBC = 'aes-256-cbc'; |
||
55 | const AES_256_CBF = 'aes-256-cbf'; |
||
56 | const AES_256_CBF1 = 'aes-256-cbf1'; |
||
57 | const AES_256_CBF8 = 'aes-256-cbf8'; |
||
58 | const AES_256_OFB = 'aes-256-ofb'; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * Recive cer content and convert to pem, get certificate fingerprint and |
||
63 | * creates a encryption key and initialization vector |
||
64 | * @param string $derdata certificate content in DER format (usual) |
||
65 | * @param string $cipher encoded cipher |
||
66 | */ |
||
67 | public function __construct($derdata, $cipher = self::AES_128_CBC) |
||
74 | |||
75 | /** |
||
76 | * Generate a key and initialization vector |
||
77 | * and encrypt this key with a certificate |
||
78 | * and combine with initialization vector in base64 format |
||
79 | * @return void |
||
80 | */ |
||
81 | public function keyGenerate() |
||
88 | |||
89 | /** |
||
90 | * Recover encryped key+iv in base64 |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getEncrypedKey():string |
||
97 | |||
98 | /** |
||
99 | * Recover Thumbprint (or fingerprint) from certificate |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getThumbprint():string |
||
106 | |||
107 | /** |
||
108 | * Recover used cipher |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getCipher():string |
||
115 | |||
116 | /** |
||
117 | * Return an array with KEY and IV used to encripy and decript |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getKeyAndIV():array |
||
124 | |||
125 | /** |
||
126 | * Encrypt message whith generate randon key and iv |
||
127 | * @param string $datamsg |
||
128 | * @return string |
||
129 | */ |
||
130 | public function encryptMsg($datamsg):string |
||
134 | |||
135 | /** |
||
136 | * Decrypt message with private key and randon key and iv |
||
137 | * @param string $cryptedmsg encrytped message |
||
138 | * @param string $privateKey in PEM format |
||
139 | * @param string $keyencrypted key+iv im base64 format |
||
140 | * @param string $cipher for encrypt and decrypt |
||
141 | * @return string |
||
142 | */ |
||
143 | public function decryptMsg($cryptedmsg, $privateKey, $keyencrypted, $cipher = self::AES_128_CBC):string |
||
155 | |||
156 | /** |
||
157 | * Decrypt message encripted with AES |
||
158 | * @param string $message |
||
159 | * @param string $cipher |
||
160 | * @param string $key |
||
161 | * @param string $iv |
||
162 | * @return string |
||
163 | */ |
||
164 | public function decryptAes($message, $cipher, $key, $iv):string |
||
168 | |||
169 | /** |
||
170 | * Recover certificate info |
||
171 | * @return array |
||
172 | */ |
||
173 | public function certificateInfo():array |
||
185 | |||
186 | /** |
||
187 | * Extract fingerprint from certificate |
||
188 | * @param string $certificate |
||
189 | * @return string |
||
190 | */ |
||
191 | protected function thumbprint($certificate):string |
||
195 | |||
196 | /** |
||
197 | * Encrypt key with certificate, add iv and converts to base64 |
||
198 | * @param string $key |
||
199 | * @param string $iv |
||
200 | * @return string |
||
201 | */ |
||
202 | protected function encryptkey($key, $iv):string |
||
207 | |||
208 | /** |
||
209 | * Converts certificate in DER format to PEM format |
||
210 | * @param string $derdata |
||
211 | * @return string |
||
212 | */ |
||
213 | protected function convertDERtoPEM($derdata):string |
||
220 | } |
||
221 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.