Skip to content

Commit

Permalink
Fix RSACrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
AxiosLeo authored and wb-hx510875 committed Jul 20, 2020
1 parent aa73046 commit 0269aee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/RSACrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class RSACrypt
{
public $config = [
'digest_alg' => 'sha512',
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
];
private $private_key;
private $public_key;
private $max_length;
Expand Down Expand Up @@ -54,7 +59,7 @@ public function publicKey($public_key = null)
*/
public function create(): self
{
$res = openssl_pkey_new();
$res = openssl_pkey_new($this->config);
openssl_pkey_export($res, $pri_key);
$this->privateKey($pri_key);
$res = openssl_pkey_get_details($res);
Expand Down Expand Up @@ -117,7 +122,7 @@ private function encrypt($data, $type = 'private')
if (false === $result) {
throw new \ErrorException('Failed encrypt by ' . $type . ' key. string : ' . $src);
}
$str .= 0 == $count ? base64_encode($result) : ',' . base64_encode($result);
$str .= 0 == $count ? base64_encode($out) : ',' . base64_encode($out);
++$count;
}

Expand All @@ -139,22 +144,26 @@ private function decrypt($data, $type = 'private')
$dataArray = explode(',', $data);
foreach ($dataArray as $src) {
$result = 'private' === $type ?
@openssl_private_encrypt(base64_decode($src), $out, $this->privateKey()) :
@openssl_public_decrypt(base64_decode($src), $out, $this->publicKey());
@openssl_private_encrypt(base64_decode($src), $out, $this->private_key) :
@openssl_public_decrypt(base64_decode($src), $out, $this->public_key);
if (false === $result) {
throw new \ErrorException('Failed decrypt by ' . $type . ' key. string : ' . $src);
}
$str .= $out;
}
} else {
dump('private' === $type);

$out = '';
$src = base64_decode($data);
$result = 'private' === $type ?
@openssl_private_encrypt(base64_decode($src), $out, $this->privateKey()) :
@openssl_public_decrypt(base64_decode($src), $out, $this->publicKey());
@openssl_private_encrypt($src, $out, $this->private_key) :
@openssl_public_decrypt($src, $out, $this->public_key);
if (false === $result) {
throw new \ErrorException('Failed decrypt by ' . $type . ' key. string : ' . $src);
throw new \ErrorException('Failed decrypt by ' . $type . ' key. string : ' . $data);
}
$str .= $out;
die();
$str = $out;
}

return $str;
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/RSACryptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace axios\tools\tests\unit;

use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
class RSACryptTest extends TestCase
{
public function testRSACrypt1()
{
$rsa = new \axios\tools\RSACrypt();
$rsa->create();
$data = 'test';
$encryptStr = $rsa->encryptByPrivateKey($data);
$this->assertEquals($data, $rsa->decryptByPublicKey($encryptStr));
}

public function testRSACrypt2()
{
$rsa = new \axios\tools\RSACrypt();
$rsa->create();
$data = 'test';
$encryptStr = $rsa->encryptByPublicKey($data);
$this->assertEquals($data, $rsa->decryptByPrivateKey($encryptStr));
}
}

0 comments on commit 0269aee

Please sign in to comment.