This repository has been archived by the owner on Oct 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSAProcessor.php
72 lines (61 loc) · 1.83 KB
/
RSAProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace MahdiIDea\Gateway\Pasargad;
class RSAProcessor
{
private $public_key = null;
private $private_key = null;
private $modulus = null;
private $key_length = "1024";
public function __construct($xmlRsakey = null, $type = null)
{
$xmlObj = null;
if ($xmlRsakey == null) {
$xmlObj = simplexml_load_file("xmlfile/RSAKey.xml");
} elseif ($type == RSAKeyType::XMLFile) {
$xmlObj = simplexml_load_file($xmlRsakey);
} else {
$xmlObj = simplexml_load_string($xmlRsakey);
}
$this->modulus = RSA::binary_to_number(base64_decode($xmlObj->Modulus));
$this->public_key = RSA::binary_to_number(base64_decode($xmlObj->Exponent));
$this->private_key = RSA::binary_to_number(base64_decode($xmlObj->D));
$this->key_length = strlen(base64_decode($xmlObj->Modulus)) * 8;
}
public function getPublicKey()
{
return $this->public_key;
}
public function getPrivateKey()
{
return $this->private_key;
}
public function getKeyLength()
{
return $this->key_length;
}
public function getModulus()
{
return $this->modulus;
}
public function encrypt($data)
{
return base64_encode(RSA::rsa_encrypt($data, $this->public_key, $this->modulus, $this->key_length));
}
public function dencrypt($data)
{
return RSA::rsa_decrypt($data, $this->private_key, $this->modulus, $this->key_length);
}
public function sign($data)
{
return RSA::rsa_sign($data, $this->private_key, $this->modulus, $this->key_length);
}
public function verify($data)
{
return RSA::rsa_verify($data, $this->public_key, $this->modulus, $this->key_length);
}
}
class RSAKeyType
{
const XMLFile = 0;
const XMLString = 1;
}