Skip to content

Commit

Permalink
Merge pull request #12 from MayMeow/dev/add-seal-and-open
Browse files Browse the repository at this point in the history
Added support for seal and open
  • Loading branch information
MayMeow authored Oct 9, 2024
2 parents a94953f + 50564ff commit 2c7afb5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 17 deletions.
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);
}
}

0 comments on commit 2c7afb5

Please sign in to comment.