Skip to content

Commit

Permalink
Fix cipher bug #3
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Mar 27, 2017
1 parent 78a35f5 commit ff1dce3
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Crypt/Cipher/PhpAesCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PhpAesCipher implements CipherInterface
*/
public function decrypt($data, $key = null, $iv = null)
{
return \AesCtr::decrypt($data, $key, $this->keyLength);
return \AesCtr::decrypt(base64_decode($data), $key, $this->keyLength);
}

/**
Expand All @@ -60,6 +60,6 @@ public function decrypt($data, $key = null, $iv = null)
*/
public function encrypt($data, $key = null, $iv = null)
{
return \AesCtr::encrypt($data, $key, $this->keyLength);
return base64_encode(\AesCtr::encrypt($data, $key, $this->keyLength));
}
}
4 changes: 3 additions & 1 deletion src/Crypt/Cipher/SimpleCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class SimpleCipher implements CipherInterface
*/
public function decrypt($data, $key = null, $iv = null)
{
$data = base64_decode($data);

if (!$iv)
{
$iv = substr($data, 0, static::DEFAULT_RANDOM_BYTE_LENGTH);
Expand Down Expand Up @@ -99,7 +101,7 @@ public function encrypt($data, $key = null, $iv = null)

$key = sha1($iv . $key);

return $iv . $this->doEncrypt($data, $key);
return base64_encode($iv . $this->doEncrypt($data, $key));
}

/**
Expand Down
6 changes: 0 additions & 6 deletions src/Crypt/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ public function encrypt($string, $key = null, $iv = null)
*/
public function decrypt($string, $key = null, $iv = null)
{
// B/C for 3.1 and older
if (strpos($string, ':') !== false)
{
$string = base64_decode(str_replace(' ', '+', $string));
}

$key = $key ? : $this->getKey();
$iv = $iv ? : $this->getIv();

Expand Down
4 changes: 3 additions & 1 deletion src/Crypt/Mcrypt/AbstractMcryptCipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function __construct()
*/
public function decrypt($data, $key = null, $iv = null)
{
$data = base64_decode($data);

if (!$iv)
{
$ivSize = $this->getIVSize();
Expand Down Expand Up @@ -120,7 +122,7 @@ public function encrypt($data, $key = null, $iv = null)
// Encrypt the data.
$encrypted = mcrypt_encrypt($this->type, $key, $data, $this->mode, $iv);

return $iv . $encrypted;
return base64_encode($iv . $encrypted);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Crypt/Test/Cipher/BlowfishCipherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Windwalker\Crypt\Test\Cipher;

use Windwalker\Crypt\Cipher\BlowfishCipher;
use Windwalker\Crypt\Crypt;

/**
* Test class of CipherBlowfish
Expand Down
13 changes: 2 additions & 11 deletions src/Crypt/Test/Cipher/SimpleCipherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,9 @@ public function testEncrypt()
*/
public function testDecrypt()
{
// Use IV
$iv = CryptHelper::genRandomBytes(16);

$data = $this->instance->encrypt('windwalker', $this->key, $iv);

$ivSize = strlen($iv);

$iv = substr($data, 0, $ivSize);

$data = substr($data, $ivSize);
$data = $this->instance->encrypt('windwalker', $this->key);

$data = $this->instance->decrypt($data, $this->key, $iv);
$data = $this->instance->decrypt($data, $this->key);

$this->assertEquals('windwalker', $data);
}
Expand Down
13 changes: 7 additions & 6 deletions src/Crypt/Test/CryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Windwalker\Crypt\Test;

use Windwalker\Crypt\Cipher\PhpAesCipher;
use Windwalker\Crypt\Cipher\SimpleCipher;
use Windwalker\Crypt\Crypt;

Expand All @@ -33,7 +34,7 @@ class CryptTest extends \PHPUnit\Framework\TestCase
*/
protected function setUp()
{
$this->instance = new Crypt(new SimpleCipher);
$this->instance = new Crypt(new PhpAesCipher);
}

/**
Expand All @@ -56,15 +57,15 @@ protected function tearDown()
*/
public function testEncrypt()
{
$hash = $this->instance->encrypt('windwalker');
$encrypted = $this->instance->encrypt('windwalker');

$this->assertTrue($this->instance->verify('windwalker', $hash));
$this->assertTrue($this->instance->verify('windwalker', $encrypted));

$crypt = new Crypt(new SimpleCipher, 'flower');
$crypt = new Crypt(new PhpAesCipher, 'flower');

$hash = $crypt->encrypt('windwalker');
$encrypted = $crypt->encrypt('windwalker');

$this->assertTrue($crypt->verify('windwalker', $hash, 'flower'));
$this->assertTrue($crypt->verify('windwalker', $encrypted, 'flower'));
}

/**
Expand Down

0 comments on commit ff1dce3

Please sign in to comment.