Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
asymmetriccipher.php
1<?php
9
10abstract class AsymmetricCipher
11{
17 public function encrypt($data, $key): bool|string
18 {
19 $keyInfo = $this->getKeyInfo($key);
20 $chunkSize = $keyInfo['bits']/8 - 11;
21
22 $result = '';
23
24 foreach (str_split($data, $chunkSize) as $chunk)
25 {
26 $encryptedChunk = $this->doEncrypt($chunk, $key);
27
28 if ($encryptedChunk === false)
29 {
30 return false;
31 }
32
33 $result .= $encryptedChunk;
34 }
35
36 return base64_encode($result);
37 }
38
44 public function decrypt($data, $key): bool|string
45 {
46 $result = '';
47
48 $keyInfo = $this->getKeyInfo($key);
49 $blockSize = $keyInfo['bits']/8;
50
51 foreach(str_split(base64_decode($data), $blockSize) as $chunk)
52 {
53 $decryptedChunk = $this->doDecrypt($chunk, $key);
54
55 if ($decryptedChunk === false)
56 {
57 return false;
58 }
59
60 $result .= $decryptedChunk;
61 }
62
63 return $result;
64 }
65
71 abstract protected function doEncrypt($data, $key);
72
78 abstract protected function doDecrypt($data, $key);
79
84 abstract protected function getKeyInfo($key);
85}