Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions Encryptors/AES256CBCEncryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace VMelnik\DoctrineEncryptBundle\Encryptors;

/**
* Class for AES256 with CBC Mode encryption
*
* @author Dody Suria Wijaya <dodysw@gmail.com>
*/
class AES256CBCEncryptor implements EncryptorInterface {

/**
* @var string
*/
private $secretKey;

/**
* @var int
*/
private $ivSize;

/**
* {@inheritdoc}
*/
public function __construct($key) {
$this->secretKey = md5($key);
$this->ivSize = mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_CBC
);
}

/**
* {@inheritdoc}
*/
public function encrypt($data) {
$iv = mcrypt_create_iv( $this->ivSize, MCRYPT_RAND);
return trim(base64_encode(
$iv .
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$this->secretKey,
$data,
MCRYPT_MODE_CBC,
$iv
)));
}

/**
* {@inheritdoc}
*/
function decrypt($data) {
$chipertext = base64_decode($data);
if (strlen($chipertext) < $this->ivSize) {
return null;
}
return trim(mcrypt_decrypt(
MCRYPT_RIJNDAEL_256,
$this->secretKey,
substr($chipertext, $this->ivSize),
MCRYPT_MODE_CBC,
substr($chipertext, 0, $this->ivSize)
));
}
}