Skip to content

Commit

Permalink
generateRSAKeyPair function has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
leventkaragol committed Jun 30, 2024
1 parent f0be2e2 commit 33c9542
Show file tree
Hide file tree
Showing 4 changed files with 528 additions and 94 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,34 @@ int main() {

## How do I generate Public/Private Keys?

it is very easy to generate the Public and Private key pair, if OpenSSL is installed on your system. As a first step,
when you run it by typing the following line on the command line, a text file named "private_key.pem" will be created
containing the private key information. "2048" at the end of the command indicates bits value of the generated key.
You have two different options to create a Public and Private key pair. The first option, and the easier one, is to use the
generateRSAKeyPair function in the library, passing the desired key length as a parameter. Below is a sample code for this usage.

```cpp
auto keyPair = CryptoService::generateRSAKeyPair(2048);

std::cout << "2048 bit Public RSA Key:" << std::endl << keyPair.publicKey << std::endl;
std::cout << "2048 bit Private RSA Key:" << std::endl << keyPair.privateKey << std::endl;
```

> [!TIP]
> If you don't know what value to write here, please see the next topic
> If you are not sure of the key length you will need, please see the next topic

Optionally, you can also pass a passphrase as follows to the generateRSAKeyPair function during key creation. In this case,
you will need to pass this passphrase to the decryptWithRSA function to decrypt the text.

```cpp
auto keyPair = CryptoService::generateRSAKeyPair(2048, "myPassphrase");

std::cout << "2048 bit Public RSA Key (with passphrase):" << std::endl << keyPair.publicKey << std::endl;
std::cout << "2048 bit Private RSA Key (with passphrase):" << std::endl << keyPair.privateKey << std::endl;
```

As a second option, if OpenSSL is installed on your system, you can use the necessary OpenSSL commands from the
command line to create a Public and Private key pair. As the first step in this option, when you run it by typing
the following line on the command line, a text file named "private_key.pem" will be created containing the private
key information.

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

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

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

RSAKeyPair generateRSAKeyPair(int keyLength, const std::string& passphrase = "");

std::string encryptWithRSA(const std::string& plaintext, const std::string& publicKeyStr);

std::string decryptWithRSA(const std::string& ciphertext, const std::string& privateKeyStr);
Expand Down
35 changes: 35 additions & 0 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ void CorruptedTextExceptionWithAES()
}
}

void generatePublicPrivateKeyPairForRSA()
{
auto keyPair = CryptoService::generateRSAKeyPair(2048);

std::cout << "2048 bit Public RSA Key:" << std::endl << keyPair.publicKey << std::endl;
std::cout << "2048 bit Private RSA Key:" << std::endl << keyPair.privateKey << std::endl;
}

void generatePublicPrivateKeyPairForRSAWithPassphrase()
{
auto keyPair = CryptoService::generateRSAKeyPair(2048, "myPassphrase");

std::cout << "2048 bit Public RSA Key (with passphrase):" << std::endl << keyPair.publicKey << std::endl;
std::cout << "2048 bit Private RSA Key (with passphrase):" << std::endl << keyPair.privateKey << std::endl;
}

void encryptWithRSA()
{
auto plainText = "This text will be encrypted soon";
Expand Down Expand Up @@ -109,6 +125,19 @@ void decryptWithRSA()
std::cout << "Decrypted Text: " << plainText << std::endl;
}

void decryptWithRSAWithPassphrase()
{
auto keyPair = CryptoService::generateRSAKeyPair(2048, "myPassphrase");

auto plainText = "This text will be encrypted soon (with passphrase)";

auto encryptedText = CryptoService::encryptWithRSA(plainText, keyPair.publicKey);

auto decryptedText = CryptoService::decryptWithRSA(encryptedText, keyPair.privateKey, "myPassphrase");

std::cout << "Decrypted Text: " << decryptedText << std::endl;
}

void invalidPublicKeyExceptionWithRSA()
{
auto plainText = "This text will be encrypted soon";
Expand Down Expand Up @@ -238,10 +267,16 @@ int main()

// Asymmetric Encryption with RSA

generatePublicPrivateKeyPairForRSA();

generatePublicPrivateKeyPairForRSAWithPassphrase();

encryptWithRSA();

decryptWithRSA();

decryptWithRSAWithPassphrase();

invalidPublicKeyExceptionWithRSA();

invalidPrivateKeyExceptionWithRSA();
Expand Down
Loading

0 comments on commit 33c9542

Please sign in to comment.