@@ 48-65 (lines=18) @@ | ||
45 | * @return string |
|
46 | * @throws InvalidKeyTypeAlgorithmException |
|
47 | */ |
|
48 | public function encrypt(Key $key, $message) |
|
49 | { |
|
50 | if(!($key instanceof RSAPublicKey)) |
|
51 | throw new InvalidKeyTypeAlgorithmException('key is not public'); |
|
52 | ||
53 | if($key->getFormat() !== 'PKCS8') |
|
54 | throw new InvalidKeyTypeAlgorithmException('keys is not on PKCS1 format'); |
|
55 | ||
56 | $res = $this->rsa_impl->loadKey($key->getEncoded()); |
|
57 | ||
58 | if(!$res) |
|
59 | throw new InvalidKeyTypeAlgorithmException('could not parse the key'); |
|
60 | ||
61 | if($this->rsa_impl->getSize() < $this->getMinKeyLen()) |
|
62 | throw new InvalidKeyTypeAlgorithmException('len is invalid'); |
|
63 | ||
64 | return $this->rsa_impl->encrypt($message); |
|
65 | } |
|
66 | ||
67 | /** |
|
68 | * @param Key $key |
|
@@ 73-91 (lines=19) @@ | ||
70 | * @return string |
|
71 | * @throws InvalidKeyTypeAlgorithmException |
|
72 | */ |
|
73 | public function decrypt(Key $key, $enc_message){ |
|
74 | ||
75 | if(!($key instanceof RSAPrivateKey)) |
|
76 | throw new InvalidKeyTypeAlgorithmException('key is not private'); |
|
77 | ||
78 | ||
79 | if($key->getFormat() !== 'PKCS1') |
|
80 | throw new InvalidKeyTypeAlgorithmException('keys is not on PKCS1 format'); |
|
81 | ||
82 | $res = $this->rsa_impl->loadKey($key->getEncoded()); |
|
83 | ||
84 | if(!$res) |
|
85 | throw new InvalidKeyTypeAlgorithmException('could not parse the key'); |
|
86 | ||
87 | if($this->rsa_impl->getSize() < $this->getMinKeyLen()) |
|
88 | throw new InvalidKeyTypeAlgorithmException('len is invalid'); |
|
89 | ||
90 | return $this->rsa_impl->decrypt($enc_message); |
|
91 | } |
|
92 | ||
93 | /** |
|
94 | * @return int |