Skip to content

Commit ee7367d

Browse files
author
Levent KARAGÖL
authored
Merge pull request #9 from leventkaragol/rsa
Rsa
2 parents dbcc7b7 + 0f39fcc commit ee7367d

File tree

4 files changed

+691
-62
lines changed

4 files changed

+691
-62
lines changed

README.md

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ Modern, easy-to-use, symmetric (AES-256) and asymmetric (RSA) encryption and als
1515

1616
* [How to add it to my project](#how-to-add-it-to-my-project)
1717
* [How to use? (Symmetric Encryption with AES)](#how-to-use-symmetric-encryption-with-aes)
18-
* [How to handle Exceptions?](#how-to-handle-exceptions)
18+
* [How to use? (Asymmetric Encryption with RSA)](#how-to-use-asymmetric-encryption-with-rsa)
19+
* [How do I generate Public/Private Keys?](#how-do-i-generate-publicprivate-keys)
20+
* [Relationship between key size and max text length that can be encrypted](#relationship-between-key-size-and-max-text-length-that-can-be-encrypted)
21+
* [How to handle Exceptions (AES)?](#how-to-handle-exceptions-aes)
22+
* [How to handle Exceptions (RSA)?](#how-to-handle-exceptions-rsa)
1923
* [Semantic Versioning](#semantic-versioning)
2024
* [Full function list](#full-function-list)
2125
* [License](#license)
@@ -76,10 +80,118 @@ int main() {
7680
> amount of "0" to the end of keys shorter than 32 characters, and ignores the last parts of keys longer than
7781
> 32 characters, allowing you to use the key you want without any errors.
7882
83+
## How to use? (Asymmetric Encryption with RSA)
7984

80-
## How to handle Exceptions?
85+
To encrypt and decrypt the given text with RSA, all you need to do is call the static **"encryptWithRSA"** and
86+
**"decryptWithRSA"** methods with a pair of public/private key.
8187

82-
There are two main Exceptions you may encounter when using the library. The first one is the **"InvalidKeyException"**
88+
> [!TIP]
89+
> If you don't know how to generate public/private keys, please see the next topic
90+
91+
```cpp
92+
#include "libcpp-crypto.hpp"
93+
94+
using namespace lklibs;
95+
96+
int main() {
97+
98+
auto plainText = "This text will be encrypted soon";
99+
100+
auto publicKey = "-----BEGIN PUBLIC KEY-----\n"
101+
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwIASR0gIwizgv0j/Gzj6\n"
102+
"E/gS1J6gwUXeDBND7c4rDdqh/NP78N6pWNKxa5YAytTAOsoqxLDRL29pq55HyRw5\n"
103+
"47M35hwdmEfE8bOjnogvHRRKu7A2iGV7akkK0cP6XgHgcJVlXBX2xCT70nIX4dDk\n"
104+
"vGhSKwrps1o+3XVhtnVoPsCDQEESApGalhQ55OT8s0fM7OTFMfqsV3GD9J9FO4wP\n"
105+
"BlawHpQ5rbWGsyNYXnjXzGpmuyKl4xQBVdbx1tzh+1XlwqMhbXibMozo5U5De0oH\n"
106+
"A9z1Owbt3++3t+LykQDcHEtiKcvYt71by1X3J2IQOBAwWJ2jRjZQ5QJWaGXirPdR\n"
107+
"VwIDAQAB\n"
108+
"-----END PUBLIC KEY-----";
109+
110+
auto privateKey = "-----BEGIN PRIVATE KEY-----\n"
111+
"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDAgBJHSAjCLOC/\n"
112+
"SP8bOPoT+BLUnqDBRd4ME0PtzisN2qH80/vw3qlY0rFrlgDK1MA6yirEsNEvb2mr\n"
113+
"nkfJHDnjszfmHB2YR8Txs6OeiC8dFEq7sDaIZXtqSQrRw/peAeBwlWVcFfbEJPvS\n"
114+
"chfh0OS8aFIrCumzWj7ddWG2dWg+wINAQRICkZqWFDnk5PyzR8zs5MUx+qxXcYP0\n"
115+
"n0U7jA8GVrAelDmttYazI1heeNfMama7IqXjFAFV1vHW3OH7VeXCoyFteJsyjOjl\n"
116+
"TkN7SgcD3PU7Bu3f77e34vKRANwcS2Ipy9i3vVvLVfcnYhA4EDBYnaNGNlDlAlZo\n"
117+
"ZeKs91FXAgMBAAECggEAKiO/HJiRxkQBvQ4TPlfWMsnjAWVqRnTve1A6VhQES8eZ\n"
118+
"H1oedGehxb51tVoEeWJiZFw+SYl1eX9XsAh5qXZC2+wvJ/HurpfDbq/G+RzRx3la\n"
119+
"NM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"
134+
"3UpJwB4dhuXypOjqvDQ6uyUSVbyD3TvneyB+8hYnPMXXTDtW6ne5pCMcxibtkK2T\n"
135+
"Tmy283w8+RuGbK4+7ifgV97PUGrYFAmpqWhaympDCTp5WTwgKsmCDxX0B3bq77xW\n"
136+
"oao1npvUquuvWx6cUaQN7Q==\n"
137+
"-----END PRIVATE KEY-----";
138+
139+
auto encryptedText = CryptoService::encryptWithRSA(plainText, publicKey);
140+
141+
std::cout << "Encrypted Text: " << encryptedText << std::endl;
142+
143+
auto decryptedText = CryptoService::decryptWithRSA(encryptedText, privateKey);
144+
145+
std::cout << "Decrypted Text: " << decryptedText << std::endl;
146+
147+
return 0;
148+
}
149+
```
150+
151+
## How do I generate Public/Private Keys?
152+
153+
it is very easy to generate the Public and Private key pair, if OpenSSL is installed on your system. As a first step,
154+
when you run it by typing the following line on the command line, a text file named "private_key.pem" will be created
155+
containing the private key information. "2048" at the end of the command indicates bits value of the generated key.
156+
157+
> [!TIP]
158+
> If you don't know what value to write here, please see the next topic
159+
160+
```bash
161+
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
162+
```
163+
164+
Then, when you run it by typing the following line on the command line, a text file named "public_key.pem" will be created
165+
containing the public key information for this private key.
166+
167+
```bash
168+
openssl rsa -pubout -in private_key.pem -out public_key.pem
169+
```
170+
171+
## Relationship between key size and max text length that can be encrypted
172+
173+
The size of the Key used during asymmetric encryption with RSA is not only related to the security of the encryption
174+
process, but also determines what the longest text that can be encrypted with this key can be. Basically, the longest text
175+
that can be encrypted with a 2<sup>x</sup> bit key can be calculated as 2<sup>x-3</sup>-11 for ASCII character set. Other
176+
character sets can take up twice. I am sharing the table below for a quick reference.
177+
178+
| Key Bits | Maximum Text Length |
179+
|----------|---------------------|
180+
| 2048 | 245 |
181+
| 4096 | 501 |
182+
| 8192 | 1013 |
183+
| 16384 | 2037 |
184+
| 32768 | 4085 |
185+
| 65536 | 8181 |
186+
187+
> [!IMPORTANT]
188+
> Do not think that you can easily create a longer key to encrypt a longer text with RSA. Each row in the table above consumes
189+
> 4 times more CPU power during encryption/decryption process than the row above. Additionally, generating a 65K bit key takes
190+
> time and requires a lot of patience, even for a high-end computer.
191+
192+
## How to handle Exceptions (AES)?
193+
194+
There are two main Exceptions you may encounter when using the library for AES encryption. The first one is the **"InvalidKeyException"**
83195
you will receive if the encryption key of the text you want to decrypt is incorrect, and the second one is the
84196
**"CorruptedTextException"** you will receive if the text you want to decrypt is invalid.
85197

@@ -136,6 +248,52 @@ int main() {
136248
}
137249
```
138250

251+
## How to handle Exceptions (RSA)?
252+
253+
The exception part for the RSA side is a little different. If the public and private keys used are not correct,
254+
**"InvalidPublicKeyException"** and **"InvalidPrivateKeyException"** are thrown. However, the structure of the keys
255+
used must be corrupt to throw these exceptions. If you use incompatible but structurally valid keys, no exception
256+
will be thrown. However, the text obtained after decryption will consist of just meaningless characters.
257+
258+
```cpp
259+
#include "libcpp-crypto.hpp"
260+
261+
using namespace lklibs;
262+
263+
int main() {
264+
265+
auto plainText = "This text will be encrypted soon";
266+
267+
auto publicKey = "-----BEGIN PUBLIC KEY-----\n"
268+
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwIASR0gIwizgv0j/Gzj6\n"
269+
"E/gS1J6gwUXeDBND7c4rDdqh/NP78N6pWNKxa5YAytTAOsoqxLDRL29pq55HyRw5\n"
270+
"47M35hwdmEfE8bOjnogvHRRKu7A2iGV7akkK0cP6XgHgcJVlXBX2xCT70nIX4dDk\n"
271+
"vGhSKwrps1o+3XVhtnVoPsCDQEESApGalhQ55OT8s0fM7OTFMfqsV3GD9J9FO4wP\n"
272+
"BlawHpQ5rbWGsyNYXnjXzGpmuyKl4xQBVdbx1tzh+1XlwqMhbXibMozo5U5De0oH\n"
273+
"A9z1Owbt3++3t+LykQDcHEtiKcvYt71by1X3J2IQOBAwWJ2jRjZQ5QJWaGXirPdR\n"
274+
"VwIDAQAB\n"
275+
"-----END PUBLIC KEY-----";
276+
277+
auto privateKey = "-----BEGIN PRIVATE KEY-----\n"
278+
"SOME_INVALID_KEY/\n"
279+
"-----END PRIVATE KEY-----";
280+
281+
auto encryptedText = CryptoService::encryptWithRSA(plainText, publicKey);
282+
283+
try
284+
{
285+
auto decryptedText = CryptoService::decryptWithRSA(encryptedText, privateKey);
286+
}
287+
catch (const InvalidPrivateKeyException& e)
288+
{
289+
std::cerr << e.what() << std::endl; // RSA private key is invalid
290+
}
291+
292+
return 0;
293+
}
294+
```
295+
296+
139297
## Semantic Versioning
140298

141299
Versioning of the library is done using conventional semantic versioning. Accordingly,
@@ -163,6 +321,10 @@ You don't need to create an instance of the class to use them.
163321
static std::string encryptWithAES(const std::string& plaintext, const std::string& key);
164322

165323
static std::string decryptWithAES(const std::string& ciphertext, const std::string& key);
324+
325+
static std::string encryptWithRSA(const std::string& plaintext, const std::string& publicKeyStr);
326+
327+
static std::string decryptWithRSA(const std::string& ciphertext, const std::string& privateKeyStr);
166328
```
167329
168330
## License

examples/main.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,172 @@ void CorruptedTextExceptionWithAES()
5252
}
5353
}
5454

55+
void encryptWithRSA()
56+
{
57+
auto plainText = "This text will be encrypted soon";
58+
59+
auto publicKey = "-----BEGIN PUBLIC KEY-----\n"
60+
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwIASR0gIwizgv0j/Gzj6\n"
61+
"E/gS1J6gwUXeDBND7c4rDdqh/NP78N6pWNKxa5YAytTAOsoqxLDRL29pq55HyRw5\n"
62+
"47M35hwdmEfE8bOjnogvHRRKu7A2iGV7akkK0cP6XgHgcJVlXBX2xCT70nIX4dDk\n"
63+
"vGhSKwrps1o+3XVhtnVoPsCDQEESApGalhQ55OT8s0fM7OTFMfqsV3GD9J9FO4wP\n"
64+
"BlawHpQ5rbWGsyNYXnjXzGpmuyKl4xQBVdbx1tzh+1XlwqMhbXibMozo5U5De0oH\n"
65+
"A9z1Owbt3++3t+LykQDcHEtiKcvYt71by1X3J2IQOBAwWJ2jRjZQ5QJWaGXirPdR\n"
66+
"VwIDAQAB\n"
67+
"-----END PUBLIC KEY-----";
68+
69+
auto encryptedText = CryptoService::encryptWithRSA(plainText, publicKey);
70+
71+
std::cout << "Encrypted Text: " << encryptedText << std::endl;
72+
}
73+
74+
void decryptWithRSA()
75+
{
76+
auto encryptedText = "RVq6qJqexZGmxQtnCEwgIJqj7rxcAdnlQUqQ+qwWzc0y/VlX/EJ+/HuuMPrFbvDcgJnfVDz7WE2pHcJWR0lLR++UWxzLT8PRqBCpI0fX7ymqsaPCn2w/Q826FplfzlIpt0LZdkrkaSa/V/HMcQOPJ4JONuWxNaUpggA0KKOHdkH8A2RcpSnc4NskwPt84b9TU/mEwHlvyIzpvSF3/AiaGbSH5wEIe5iniBBhE7bUlkadvGu6jv8wiGqSeD/fWzZM7gbwmlxqxSzKFIJOpNwxya5+h7mHNGMmmZVVfGunrm6P0l7Qfz/cGf7Rca2SOgFosBWCr7NpjjM/AaK4PYVZwg==";
77+
78+
auto privateKey = "-----BEGIN PRIVATE KEY-----\n"
79+
"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDAgBJHSAjCLOC/\n"
80+
"SP8bOPoT+BLUnqDBRd4ME0PtzisN2qH80/vw3qlY0rFrlgDK1MA6yirEsNEvb2mr\n"
81+
"nkfJHDnjszfmHB2YR8Txs6OeiC8dFEq7sDaIZXtqSQrRw/peAeBwlWVcFfbEJPvS\n"
82+
"chfh0OS8aFIrCumzWj7ddWG2dWg+wINAQRICkZqWFDnk5PyzR8zs5MUx+qxXcYP0\n"
83+
"n0U7jA8GVrAelDmttYazI1heeNfMama7IqXjFAFV1vHW3OH7VeXCoyFteJsyjOjl\n"
84+
"TkN7SgcD3PU7Bu3f77e34vKRANwcS2Ipy9i3vVvLVfcnYhA4EDBYnaNGNlDlAlZo\n"
85+
"ZeKs91FXAgMBAAECgg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w4R8unec4zyYCZVZfSFVvFy1/6Iw40vq8yz3qQd+c7aREWENJg84H+rOx\n"
101+
"n+Tnfq/sKgK1ufdVWlLlMaRxf6dPo2iSuNcAnAECgYActoBiupT5Kc2ZK/lCylJc\n"
102+
"3UpJwB4dhuXypOjqvDQ6uyUSVbyD3TvneyB+8hYnPMXXTDtW6ne5pCMcxibtkK2T\n"
103+
"Tmy283w8+RuGbK4+7ifgV97PUGrYFAmpqWhaympDCTp5WTwgKsmCDxX0B3bq77xW\n"
104+
"oao1npvUquuvWx6cUaQN7Q==\n"
105+
"-----END PRIVATE KEY-----";
106+
107+
auto plainText = CryptoService::decryptWithRSA(encryptedText, privateKey);
108+
109+
std::cout << "Decrypted Text: " << plainText << std::endl;
110+
}
111+
112+
void invalidPublicKeyExceptionWithRSA()
113+
{
114+
auto plainText = "This text will be encrypted soon";
115+
116+
auto publicKey = "-----BEGIN PUBLIC KEY-----\n"
117+
"SOME_INVALID_KEY\n"
118+
"-----END PUBLIC KEY-----";
119+
120+
try
121+
{
122+
auto encryptedText = CryptoService::encryptWithRSA(plainText, publicKey);
123+
}
124+
catch (const InvalidPublicKeyException& e)
125+
{
126+
std::cout << "Encryption Error: " << e.what() << std::endl;
127+
}
128+
}
129+
130+
void invalidPrivateKeyExceptionWithRSA()
131+
{
132+
auto encryptedText = "RVq6qJqexZGmxQtnCEwgIJqj7rxcAdnlQUqQ+qwWzc0y/VlX/EJ+/HuuMPrFbvDcgJnfVDz7WE2pHcJWR0lLR++UWxzLT8PRqBCpI0fX7ymqsaPCn2w/Q826FplfzlIpt0LZdkrkaSa/V/HMcQOPJ4JONuWxNaUpggA0KKOHdkH8A2RcpSnc4NskwPt84b9TU/mEwHlvyIzpvSF3/AiaGbSH5wEIe5iniBBhE7bUlkadvGu6jv8wiGqSeD/fWzZM7gbwmlxqxSzKFIJOpNwxya5+h7mHNGMmmZVVfGunrm6P0l7Qfz/cGf7Rca2SOgFosBWCr7NpjjM/AaK4PYVZwg==";
133+
134+
auto privateKey = "-----BEGIN PRIVATE KEY-----\n"
135+
"SOME_INVALID_KEY/\n"
136+
"-----END PRIVATE KEY-----";
137+
138+
try
139+
{
140+
auto plainText = CryptoService::decryptWithRSA(encryptedText, privateKey);
141+
}
142+
catch (const InvalidPrivateKeyException& e)
143+
{
144+
std::cout << "Decryption Error: " << e.what() << std::endl;
145+
}
146+
}
147+
148+
void tooLongTextExceptionForEncryptionWithRSA()
149+
{
150+
auto plainText = "This text used for testing is too long as a 2048 bit key is used during encryption with RSA, so it will give an exception. The longest text that can be encrypted with a 2048 bit key is 245 characters. However there are 246 characters in this text";
151+
152+
auto publicKey = "-----BEGIN PUBLIC KEY-----\n"
153+
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwIASR0gIwizgv0j/Gzj6\n"
154+
"E/gS1J6gwUXeDBND7c4rDdqh/NP78N6pWNKxa5YAytTAOsoqxLDRL29pq55HyRw5\n"
155+
"47M35hwdmEfE8bOjnogvHRRKu7A2iGV7akkK0cP6XgHgcJVlXBX2xCT70nIX4dDk\n"
156+
"vGhSKwrps1o+3XVhtnVoPsCDQEESApGalhQ55OT8s0fM7OTFMfqsV3GD9J9FO4wP\n"
157+
"BlawHpQ5rbWGsyNYXnjXzGpmuyKl4xQBVdbx1tzh+1XlwqMhbXibMozo5U5De0oH\n"
158+
"A9z1Owbt3++3t+LykQDcHEtiKcvYt71by1X3J2IQOBAwWJ2jRjZQ5QJWaGXirPdR\n"
159+
"VwIDAQAB\n"
160+
"-----END PUBLIC KEY-----";
161+
162+
try
163+
{
164+
auto encryptedText = CryptoService::encryptWithRSA(plainText, publicKey);
165+
}
166+
catch (const TextTooLongForPublicKeyException& e)
167+
{
168+
std::cout << "Encryption Error: " << e.what() << std::endl;
169+
}
170+
}
171+
172+
void CorruptedTextExceptionWithRSA()
173+
{
174+
auto encryptedText = "1";
175+
176+
auto privateKey = "-----BEGIN PRIVATE KEY-----\n"
177+
"MIIEvAIBADANBgkqhkiG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qzJdd6qnNwUIK32GaY8F5pxo12zcpaVGLPk6J78HxQrnx6gQH/\n"
193+
"KT3Bc9wT4M7nHIkt/Y879Z7BaiQ+2TafSdNWmobIB2H5X9zd0JoSs9gScbbaZ0kA\n"
194+
"eW+KEtkBGQKBgDCEEgzKEuU8Td1i7nPdY6zgRtvbCj3f368vRZ5DhNHtA21a/0MS\n"
195+
"fxMZfDwl8lJAOvuOGRthuBIV7j+S7elANrEJJgJiP8l6f7Ygawe9g8WjmV4Gy7Dk\n"
196+
"W2N3ahRDTiEurzcIAT8R2MusznM0NkDSsQUf1NnBVE9aVkcypStiANibAoGAXWaT\n"
197+
"Rvx987bjjd2fb1loCzpt5IrK7eaPx8c5jO0o2T8OTzE5urNJiv5bcSHTYEZLN4AM\n"
198+
"M+o0kUmw4R8unec4zyYCZVZfSFVvFy1/6Iw40vq8yz3qQd+c7aREWENJg84H+rOx\n"
199+
"n+Tnfq/sKgK1ufdVWlLlMaRxf6dPo2iSuNcAnAECgYActoBiupT5Kc2ZK/lCylJc\n"
200+
"3UpJwB4dhuXypOjqvDQ6uyUSVbyD3TvneyB+8hYnPMXXTDtW6ne5pCMcxibtkK2T\n"
201+
"Tmy283w8+RuGbK4+7ifgV97PUGrYFAmpqWhaympDCTp5WTwgKsmCDxX0B3bq77xW\n"
202+
"oao1npvUquuvWx6cUaQN7Q==\n"
203+
"-----END PRIVATE KEY-----";
204+
205+
try
206+
{
207+
auto plainText = CryptoService::decryptWithRSA(encryptedText, privateKey);
208+
209+
std::cout << "plainText: " << plainText << std::endl;
210+
}
211+
catch (const CorruptedTextException& e)
212+
{
213+
std::cout << "Decryption Error: " << e.what() << std::endl;
214+
}
215+
}
216+
55217
int main()
56218
{
219+
// Symmetric Encryption with AES
220+
57221
encryptWithAES();
58222

59223
decryptWithAES();
@@ -62,5 +226,20 @@ int main()
62226

63227
CorruptedTextExceptionWithAES();
64228

229+
230+
// Asymmetric Encryption with RSA
231+
232+
encryptWithRSA();
233+
234+
decryptWithRSA();
235+
236+
invalidPublicKeyExceptionWithRSA();
237+
238+
invalidPrivateKeyExceptionWithRSA();
239+
240+
tooLongTextExceptionForEncryptionWithRSA();
241+
242+
CorruptedTextExceptionWithRSA();
243+
65244
return 0;
66245
}

0 commit comments

Comments
 (0)