Skip to content

Commit 33c9542

Browse files
author
Levent KARAGÖL
committed
generateRSAKeyPair function has been added
1 parent f0be2e2 commit 33c9542

File tree

4 files changed

+528
-94
lines changed

4 files changed

+528
-94
lines changed

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,34 @@ int main() {
172172

173173
## How do I generate Public/Private Keys?
174174

175-
it is very easy to generate the Public and Private key pair, if OpenSSL is installed on your system. As a first step,
176-
when you run it by typing the following line on the command line, a text file named "private_key.pem" will be created
177-
containing the private key information. "2048" at the end of the command indicates bits value of the generated key.
175+
You have two different options to create a Public and Private key pair. The first option, and the easier one, is to use the
176+
generateRSAKeyPair function in the library, passing the desired key length as a parameter. Below is a sample code for this usage.
177+
178+
```cpp
179+
auto keyPair = CryptoService::generateRSAKeyPair(2048);
180+
181+
std::cout << "2048 bit Public RSA Key:" << std::endl << keyPair.publicKey << std::endl;
182+
std::cout << "2048 bit Private RSA Key:" << std::endl << keyPair.privateKey << std::endl;
183+
```
178184

179185
> [!TIP]
180-
> If you don't know what value to write here, please see the next topic
186+
> If you are not sure of the key length you will need, please see the next topic
187+
188+
189+
Optionally, you can also pass a passphrase as follows to the generateRSAKeyPair function during key creation. In this case,
190+
you will need to pass this passphrase to the decryptWithRSA function to decrypt the text.
191+
192+
```cpp
193+
auto keyPair = CryptoService::generateRSAKeyPair(2048, "myPassphrase");
194+
195+
std::cout << "2048 bit Public RSA Key (with passphrase):" << std::endl << keyPair.publicKey << std::endl;
196+
std::cout << "2048 bit Private RSA Key (with passphrase):" << std::endl << keyPair.privateKey << std::endl;
197+
```
198+
199+
As a second option, if OpenSSL is installed on your system, you can use the necessary OpenSSL commands from the
200+
command line to create a Public and Private key pair. As the first step in this option, when you run it by typing
201+
the following line on the command line, a text file named "private_key.pem" will be created containing the private
202+
key information.
181203

182204
```bash
183205
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
@@ -211,6 +233,10 @@ character sets can take up twice. I am sharing the table below for a quick refer
211233
> 4 times more CPU power during encryption/decryption process than the row above. Additionally, generating a 65K bit key takes
212234
> time and requires a lot of patience, even for a high-end computer.
213235
236+
> [!CAUTION]
237+
> 1024-bit RSA keys are not secure in the face of today's increasing computing power and advanced factorization algorithms.
238+
> Please use keys of at least 2048 bits.
239+
214240
## How to handle Exceptions (AES)?
215241

216242
There are two main Exceptions you may encounter when using the library for AES encryption. The first one is the **"InvalidKeyException"**
@@ -342,6 +368,8 @@ std::string encryptWithAES(const std::string& plaintext, const std::string& key)
342368

343369
std::string decryptWithAES(const std::string& ciphertext, const std::string& key);
344370

371+
RSAKeyPair generateRSAKeyPair(int keyLength, const std::string& passphrase = "");
372+
345373
std::string encryptWithRSA(const std::string& plaintext, const std::string& publicKeyStr);
346374

347375
std::string decryptWithRSA(const std::string& ciphertext, const std::string& privateKeyStr);

examples/main.cpp

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

55+
void generatePublicPrivateKeyPairForRSA()
56+
{
57+
auto keyPair = CryptoService::generateRSAKeyPair(2048);
58+
59+
std::cout << "2048 bit Public RSA Key:" << std::endl << keyPair.publicKey << std::endl;
60+
std::cout << "2048 bit Private RSA Key:" << std::endl << keyPair.privateKey << std::endl;
61+
}
62+
63+
void generatePublicPrivateKeyPairForRSAWithPassphrase()
64+
{
65+
auto keyPair = CryptoService::generateRSAKeyPair(2048, "myPassphrase");
66+
67+
std::cout << "2048 bit Public RSA Key (with passphrase):" << std::endl << keyPair.publicKey << std::endl;
68+
std::cout << "2048 bit Private RSA Key (with passphrase):" << std::endl << keyPair.privateKey << std::endl;
69+
}
70+
5571
void encryptWithRSA()
5672
{
5773
auto plainText = "This text will be encrypted soon";
@@ -109,6 +125,19 @@ void decryptWithRSA()
109125
std::cout << "Decrypted Text: " << plainText << std::endl;
110126
}
111127

128+
void decryptWithRSAWithPassphrase()
129+
{
130+
auto keyPair = CryptoService::generateRSAKeyPair(2048, "myPassphrase");
131+
132+
auto plainText = "This text will be encrypted soon (with passphrase)";
133+
134+
auto encryptedText = CryptoService::encryptWithRSA(plainText, keyPair.publicKey);
135+
136+
auto decryptedText = CryptoService::decryptWithRSA(encryptedText, keyPair.privateKey, "myPassphrase");
137+
138+
std::cout << "Decrypted Text: " << decryptedText << std::endl;
139+
}
140+
112141
void invalidPublicKeyExceptionWithRSA()
113142
{
114143
auto plainText = "This text will be encrypted soon";
@@ -238,10 +267,16 @@ int main()
238267

239268
// Asymmetric Encryption with RSA
240269

270+
generatePublicPrivateKeyPairForRSA();
271+
272+
generatePublicPrivateKeyPairForRSAWithPassphrase();
273+
241274
encryptWithRSA();
242275

243276
decryptWithRSA();
244277

278+
decryptWithRSAWithPassphrase();
279+
245280
invalidPublicKeyExceptionWithRSA();
246281

247282
invalidPrivateKeyExceptionWithRSA();

0 commit comments

Comments
 (0)