Skip to content

Commit ff1dce3

Browse files
committed
Fix cipher bug #3
1 parent 78a35f5 commit ff1dce3

File tree

7 files changed

+18
-27
lines changed

7 files changed

+18
-27
lines changed

src/Crypt/Cipher/PhpAesCipher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PhpAesCipher implements CipherInterface
4343
*/
4444
public function decrypt($data, $key = null, $iv = null)
4545
{
46-
return \AesCtr::decrypt($data, $key, $this->keyLength);
46+
return \AesCtr::decrypt(base64_decode($data), $key, $this->keyLength);
4747
}
4848

4949
/**
@@ -60,6 +60,6 @@ public function decrypt($data, $key = null, $iv = null)
6060
*/
6161
public function encrypt($data, $key = null, $iv = null)
6262
{
63-
return \AesCtr::encrypt($data, $key, $this->keyLength);
63+
return base64_encode(\AesCtr::encrypt($data, $key, $this->keyLength));
6464
}
6565
}

src/Crypt/Cipher/SimpleCipher.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class SimpleCipher implements CipherInterface
3535
*/
3636
public function decrypt($data, $key = null, $iv = null)
3737
{
38+
$data = base64_decode($data);
39+
3840
if (!$iv)
3941
{
4042
$iv = substr($data, 0, static::DEFAULT_RANDOM_BYTE_LENGTH);
@@ -99,7 +101,7 @@ public function encrypt($data, $key = null, $iv = null)
99101

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

102-
return $iv . $this->doEncrypt($data, $key);
104+
return base64_encode($iv . $this->doEncrypt($data, $key));
103105
}
104106

105107
/**

src/Crypt/Crypt.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ public function encrypt($string, $key = null, $iv = null)
8989
*/
9090
public function decrypt($string, $key = null, $iv = null)
9191
{
92-
// B/C for 3.1 and older
93-
if (strpos($string, ':') !== false)
94-
{
95-
$string = base64_decode(str_replace(' ', '+', $string));
96-
}
97-
9892
$key = $key ? : $this->getKey();
9993
$iv = $iv ? : $this->getIv();
10094

src/Crypt/Mcrypt/AbstractMcryptCipher.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function __construct()
6868
*/
6969
public function decrypt($data, $key = null, $iv = null)
7070
{
71+
$data = base64_decode($data);
72+
7173
if (!$iv)
7274
{
7375
$ivSize = $this->getIVSize();
@@ -120,7 +122,7 @@ public function encrypt($data, $key = null, $iv = null)
120122
// Encrypt the data.
121123
$encrypted = mcrypt_encrypt($this->type, $key, $data, $this->mode, $iv);
122124

123-
return $iv . $encrypted;
125+
return base64_encode($iv . $encrypted);
124126
}
125127

126128
/**

src/Crypt/Test/Cipher/BlowfishCipherTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Windwalker\Crypt\Test\Cipher;
1010

1111
use Windwalker\Crypt\Cipher\BlowfishCipher;
12+
use Windwalker\Crypt\Crypt;
1213

1314
/**
1415
* Test class of CipherBlowfish

src/Crypt/Test/Cipher/SimpleCipherTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,9 @@ public function testEncrypt()
8787
*/
8888
public function testDecrypt()
8989
{
90-
// Use IV
91-
$iv = CryptHelper::genRandomBytes(16);
92-
93-
$data = $this->instance->encrypt('windwalker', $this->key, $iv);
94-
95-
$ivSize = strlen($iv);
96-
97-
$iv = substr($data, 0, $ivSize);
98-
99-
$data = substr($data, $ivSize);
90+
$data = $this->instance->encrypt('windwalker', $this->key);
10091

101-
$data = $this->instance->decrypt($data, $this->key, $iv);
92+
$data = $this->instance->decrypt($data, $this->key);
10293

10394
$this->assertEquals('windwalker', $data);
10495
}

src/Crypt/Test/CryptTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Windwalker\Crypt\Test;
1010

11+
use Windwalker\Crypt\Cipher\PhpAesCipher;
1112
use Windwalker\Crypt\Cipher\SimpleCipher;
1213
use Windwalker\Crypt\Crypt;
1314

@@ -33,7 +34,7 @@ class CryptTest extends \PHPUnit\Framework\TestCase
3334
*/
3435
protected function setUp()
3536
{
36-
$this->instance = new Crypt(new SimpleCipher);
37+
$this->instance = new Crypt(new PhpAesCipher);
3738
}
3839

3940
/**
@@ -56,15 +57,15 @@ protected function tearDown()
5657
*/
5758
public function testEncrypt()
5859
{
59-
$hash = $this->instance->encrypt('windwalker');
60+
$encrypted = $this->instance->encrypt('windwalker');
6061

61-
$this->assertTrue($this->instance->verify('windwalker', $hash));
62+
$this->assertTrue($this->instance->verify('windwalker', $encrypted));
6263

63-
$crypt = new Crypt(new SimpleCipher, 'flower');
64+
$crypt = new Crypt(new PhpAesCipher, 'flower');
6465

65-
$hash = $crypt->encrypt('windwalker');
66+
$encrypted = $crypt->encrypt('windwalker');
6667

67-
$this->assertTrue($crypt->verify('windwalker', $hash, 'flower'));
68+
$this->assertTrue($crypt->verify('windwalker', $encrypted, 'flower'));
6869
}
6970

7071
/**

0 commit comments

Comments
 (0)