cryptal  latest
Cryptography Abstraction Layer
CBC.php
1 <?php
2 
4 
7 
11 class CBC implements AsymmetricModeInterface
12 {
14  protected $cipher;
15 
17  protected $iv;
18 
19  public function __construct(CryptoInterface $cipher, $iv, $tagLength)
20  {
21  $ivSize = strlen($iv);
22  $blockSize = $cipher->getBlockSize();
23  if ($ivSize !== $blockSize) {
24  throw new \Exception("Invalid IV size (got $ivSize bytes; should be $blockSize)");
25  }
26 
27  $this->cipher = $cipher;
28  $this->iv = $iv;
29  }
30 
31  public function encrypt($data, $context)
32  {
33  $data ^= $this->iv;
34  $res = $this->cipher->encrypt('', $data);
35  $this->iv = $res;
36  return $res;
37  }
38 
39  public function decrypt($data, $context)
40  {
41  $res = $this->cipher->decrypt('', $data) ^ $this->iv;
42  $this->iv = $data;
43  return $res;
44  }
45 }
encrypt($data, $context)
Definition: CBC.php:31
decrypt($data, $context)
Definition: CBC.php:39
__construct(CryptoInterface $cipher, $iv, $tagLength)
Definition: CBC.php:19
$iv
Initialization Vector.
Definition: CBC.php:17