Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user data decryption. #570

Merged
merged 3 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions src/Encryption/Encryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class Encryptor
*/
protected $blockSize;

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

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

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

return base64_decode($this->AESKey.'=', true);
Expand Down
99 changes: 99 additions & 0 deletions src/MiniProgram/Encryption/Encryptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Encryptor.php.
*
* Part of Overtrue\WeChat.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author mingyoung <[email protected]>
* @copyright 2016
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\MiniProgram\Encryption;

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.
*
* @var string
*/
protected $iv;

/**
* Encryptor constructor.
*
* @param string $sessionKey
* @param string $iv
*/
public function __construct($sessionKey, $iv)
{
$this->iv = base64_decode($iv, true);

parent::__construct(null, null, $sessionKey);
}

/**
* Decrypt data.
*
* @param $encrypted
*
* @return string
*/
public function decryptData($encrypted)
{
return $this->decrypt($encrypted);
}

/**
* Decrypt data.
*
* @param string $encrypted
*
* @return Collection
*
* @throws EncryptionException
*/
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);

return new Collection($result);
}
}