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

Added support for seal and open #12

Merged
merged 2 commits into from
Oct 9, 2024
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
72 changes: 71 additions & 1 deletion src/AESCryptoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ public function generateKey()
* @todo Change return type to string only, throw exception instead
* @return bool|string
*/
public function generateIV()
public function generateIV(?string $cipher = null)
{
if ($cipher != null) {
$this->cipher = strtolower($cipher);
}

if (in_array($this->cipher, openssl_get_cipher_methods())) {
if ($ivLength = openssl_cipher_iv_length($this->cipher)) {
if ($iv = openssl_random_pseudo_bytes($ivLength)) {
Expand All @@ -92,6 +96,8 @@ public function generateIV()
return $this->iv;
}

var_dump('Eroro');

return false;
}

Expand Down Expand Up @@ -154,4 +160,68 @@ public function decrypt(string $encryptedData): string

return $decryptedText;
}

/**
* Seal data using AES-256-CBC and public key
*
* Sealed data are array that contains encrypted data [1] and encrypted key [0]
* encrypted data also contains IV
*
* @param string $plain_text
* @param RSAParameters $rSAParameters
* @param bool $humanReadableData whether to return base64 encoded data
* @return array Sealed data
*/
public function seal(string $plain_text, RSAParameters $rSAParameters, bool $humanReadableData = false): array
{
$this->generateIV('aes-256-cbc');

openssl_seal($plain_text, $sealed_data, $ekeys, [$rSAParameters->getPublicKey()], 'aes-256-cbc', $this->iv);

$sealed_data = $this->iv . $sealed_data;

if ($humanReadableData) {
return [
base64_encode($ekeys[0]),
base64_encode($sealed_data)
];
};

return [
$ekeys[0],
$sealed_data
];
}

/**
* open function using AES-256-CBC and private key
*
* @param string $sealed_data
* @param string $ekeys
* @param RSAParameters $rSAParameters
* @return string Opened data
*/
public function open(string $sealed_data, string $ekeys, RSAParameters $rSAParameters): string
{
if (preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $sealed_data)) {
$sealed_data = base64_decode($sealed_data);
}

if (preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $ekeys)) {
$ekeys = base64_decode($ekeys);
}

if ($ivLength = openssl_cipher_iv_length('aes-256-cbc')) {
$iv_len = $ivLength;
} else {
throw new IvGenerateException();
}

$iv = substr($sealed_data, 0, $iv_len);
$encryptedData = substr($sealed_data, $iv_len);

openssl_open($encryptedData, $open_data, $ekeys, $rSAParameters->getPrivateKey(), 'aes-256-cbc', $iv);

return $open_data;
}
}
16 changes: 0 additions & 16 deletions src/RSACryptoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ public function publicDecrypt(string $encryptedText): string
return $plainText;
}

/**
* @param string $plain_text
* @return string
*/
protected function seal(string $plain_text): string
{
//openssl_open($plain_text, $sealed_data, $ekeys, [$this->parameters->getPrivateKey()])

throw new NotImplementedException();
}

protected function open(): string
{
throw new NotImplementedException();
}

/**
* Sign data with key and return signature
*
Expand Down
19 changes: 19 additions & 0 deletions tests/RSACryptoServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MayMeow\Cryptography\Tests;

use MayMeow\Cryptography\AESCryptoServiceProvider;
use MayMeow\Cryptography\RSACryptoServiceProvider;
use MayMeow\Cryptography\RSAParameters;
use PHPUnit\Framework\TestCase;
Expand All @@ -21,4 +22,22 @@ public function canEncryptAndDecryptText()

$this->assertEquals($plainText, $rsa->decrypt($encryptedTest));
}

/** @test */
public function canSealData()
{
$plainText = "This is going";
$parameters = new RSAParameters();
$parameters->generateKeys("passphrase");

$rsa = new RSACryptoServiceProvider();
$rsa->setParameters($parameters);

$aes = new AESCryptoServiceProvider();

$sealed = $aes->seal($plainText, $parameters, humanReadableData: true);
$opened = $aes->open($sealed[1], $sealed[0], $parameters);

$this->assertEquals($plainText, $opened);
}
}