diff --git a/src/Crypt/Cipher/PhpAesCipher.php b/src/Crypt/Cipher/PhpAesCipher.php index 340b5a3b8..9c0c91eb4 100644 --- a/src/Crypt/Cipher/PhpAesCipher.php +++ b/src/Crypt/Cipher/PhpAesCipher.php @@ -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); } /** @@ -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)); } } diff --git a/src/Crypt/Cipher/SimpleCipher.php b/src/Crypt/Cipher/SimpleCipher.php index 29fe68725..585c919c0 100644 --- a/src/Crypt/Cipher/SimpleCipher.php +++ b/src/Crypt/Cipher/SimpleCipher.php @@ -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); @@ -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)); } /** diff --git a/src/Crypt/Crypt.php b/src/Crypt/Crypt.php index 7b51a397e..40e5b07c8 100644 --- a/src/Crypt/Crypt.php +++ b/src/Crypt/Crypt.php @@ -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(); diff --git a/src/Crypt/Mcrypt/AbstractMcryptCipher.php b/src/Crypt/Mcrypt/AbstractMcryptCipher.php index b4ca162ba..146522872 100644 --- a/src/Crypt/Mcrypt/AbstractMcryptCipher.php +++ b/src/Crypt/Mcrypt/AbstractMcryptCipher.php @@ -68,6 +68,8 @@ public function __construct() */ public function decrypt($data, $key = null, $iv = null) { + $data = base64_decode($data); + if (!$iv) { $ivSize = $this->getIVSize(); @@ -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); } /** diff --git a/src/Crypt/Test/Cipher/BlowfishCipherTest.php b/src/Crypt/Test/Cipher/BlowfishCipherTest.php index 11d464be9..e748bf801 100644 --- a/src/Crypt/Test/Cipher/BlowfishCipherTest.php +++ b/src/Crypt/Test/Cipher/BlowfishCipherTest.php @@ -9,6 +9,7 @@ namespace Windwalker\Crypt\Test\Cipher; use Windwalker\Crypt\Cipher\BlowfishCipher; +use Windwalker\Crypt\Crypt; /** * Test class of CipherBlowfish diff --git a/src/Crypt/Test/Cipher/SimpleCipherTest.php b/src/Crypt/Test/Cipher/SimpleCipherTest.php index 5101ff2fb..0422d5b15 100644 --- a/src/Crypt/Test/Cipher/SimpleCipherTest.php +++ b/src/Crypt/Test/Cipher/SimpleCipherTest.php @@ -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); } diff --git a/src/Crypt/Test/CryptTest.php b/src/Crypt/Test/CryptTest.php index 9b87bd526..880d8a502 100644 --- a/src/Crypt/Test/CryptTest.php +++ b/src/Crypt/Test/CryptTest.php @@ -8,6 +8,7 @@ namespace Windwalker\Crypt\Test; +use Windwalker\Crypt\Cipher\PhpAesCipher; use Windwalker\Crypt\Cipher\SimpleCipher; use Windwalker\Crypt\Crypt; @@ -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); } /** @@ -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')); } /**