@@ -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+ "NMUJ4wjoH+e2dR4EMFET20FxC1wJhX2dHL/6J2ZNtErX9fExIKB4U41vIvyHofis\n"
120+ "ABjyPMWsoDnFgJS2dYlXqDE9du2gXMq+vxyAKFuG5iLjtoaDGiY6hUx6eSUAclHd\n"
121+ "hR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"
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"**
83195you 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
141299Versioning 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.
163321static std::string encryptWithAES (const std::string& plaintext, const std::string& key);
164322
165323static 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
0 commit comments