@@ -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
183205openssl 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
216242There 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
343369std::string decryptWithAES(const std::string& ciphertext, const std::string& key);
344370
371+ RSAKeyPair generateRSAKeyPair(int keyLength, const std::string& passphrase = "");
372+
345373std::string encryptWithRSA(const std::string& plaintext, const std::string& publicKeyStr);
346374
347375std::string decryptWithRSA(const std::string& ciphertext, const std::string& privateKeyStr);
0 commit comments