Skip to content

Commit

Permalink
Fix mini-program decryptor (#615)
Browse files Browse the repository at this point in the history
* 🐛Fix mini-program decryptor

* fix styles
  • Loading branch information
mingyoung authored and overtrue committed Mar 22, 2017
1 parent 63098b8 commit 38badb3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/Encryption/EncryptionException.php
Expand Up @@ -39,4 +39,5 @@ class EncryptionException extends CoreException
const ERROR_BASE64_ENCODE = -40009; // Base64 encoding failed
const ERROR_BASE64_DECODE = -40010; // Base64 decoding failed
const ERROR_XML_BUILD = -40011; // XML build failed
const ILLEGAL_BUFFER = -41003; // Illegal buffer
}
11 changes: 2 additions & 9 deletions src/Encryption/Encryptor.php
Expand Up @@ -59,13 +59,6 @@ class Encryptor
*/
protected $blockSize;

/**
* Aes key length.
*
* @var int
*/
protected $aesKeyLength = 43;

/**
* Constructor.
*
Expand Down Expand Up @@ -222,8 +215,8 @@ protected function getAESKey()
throw new InvalidConfigException("Configuration mission, 'aes_key' is required.");
}

if (strlen($this->AESKey) !== $this->aesKeyLength) {
throw new InvalidConfigException("The length of 'aes_key' must be {$this->aesKeyLength}.");
if (strlen($this->AESKey) !== 43) {
throw new InvalidConfigException("The length of 'aes_key' must be 43.");
}

return base64_decode($this->AESKey.'=', true);
Expand Down
41 changes: 28 additions & 13 deletions src/MiniProgram/Encryption/Encryptor.php
Expand Up @@ -26,18 +26,14 @@

namespace EasyWeChat\MiniProgram\Encryption;

use EasyWeChat\Core\Exceptions\InvalidConfigException;
use EasyWeChat\Encryption\EncryptionException;
use EasyWeChat\Encryption\Encryptor as BaseEncryptor;
use EasyWeChat\Support\Collection;
use Exception as BaseException;

class Encryptor extends BaseEncryptor
{
/**
* {@inheritdoc}.
*/
protected $aesKeyLength = 24;

/**
* A non-NULL Initialization Vector.
*
Expand All @@ -63,19 +59,21 @@ public function __construct($sessionKey, $iv)
*
* @param $encrypted
*
* @return string
* @return \EasyWeChat\Support\Collection
*/
public function decryptData($encrypted)
{
return $this->decrypt($encrypted);
return new Collection(
$this->decrypt($encrypted)
);
}

/**
* Decrypt data.
*
* @param string $encrypted
*
* @return Collection
* @return array
*
* @throws EncryptionException
*/
Expand All @@ -84,16 +82,33 @@ private function decrypt($encrypted)
try {
$key = $this->getAESKey();
$ciphertext = base64_decode($encrypted, true);

$decrypted = openssl_decrypt($ciphertext, 'aes-128-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $this->iv);

$result = $this->decode($decrypted);
} catch (BaseException $e) {
throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_DECRYPT_AES);
}

$result = json_decode($result, true);
$result = json_decode($this->decode($decrypted), true);

if (is_null($result)) {
throw new EncryptionException('ILLEGAL_BUFFER', EncryptionException::ILLEGAL_BUFFER);
}

return $result;
}

/**
* Return AESKey.
*
* @return string
*
* @throws InvalidConfigException
*/
protected function getAESKey()
{
if (empty($this->AESKey)) {
throw new InvalidConfigException("Configuration mission, 'aes_key' is required.");
}

return new Collection($result);
return base64_decode($this->AESKey, true);
}
}
37 changes: 37 additions & 0 deletions tests/MiniProgram/EncryptorTest.php
@@ -0,0 +1,37 @@
<?php

use EasyWeChat\MiniProgram\Encryption\Encryptor;

class EncryptorTest extends TestCase
{
public function getEncryptor()
{
return new Encryptor('tiihtNczf5v6AKRyjwEUhQ==', 'r7BXXKkLb8qrSNn05n0qiA==');
}

public function testDecrypt()
{
$encryptedData = 'CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM
QmRzooG2xrDcvSnxIMXFufNstNGTyaGS
9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+
3hVbJSRgv+4lGOETKUQz6OYStslQ142d
NCuabNPGBzlooOmB231qMM85d2/fV6Ch
evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6
/1Xx1COxFvrc2d7UL/lmHInNlxuacJXw
u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn
/Hz7saL8xz+W//FRAUid1OksQaQx4CMs
8LOddcQhULW4ucetDf96JcR3g0gfRK4P
C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB
6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns
/8wR2SiRS7MNACwTyrGvt9ts8p12PKFd
lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV
oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG
20f0a04COwfneQAGGwd5oa+T8yO5hzuy
Db/XcxxmK01EpqOyuxINew==';

$decrypted = $this->getEncryptor()->decryptData($encryptedData);

$this->assertEquals('oGZUI0egBJY1zhBYw2KhdUfwVJJE', $decrypted->openId);
$this->assertEquals('ocMvos6NjeKLIBqg5Mr9QjxrP1FA', $decrypted->unionId);
}
}

0 comments on commit 38badb3

Please sign in to comment.