Skip to content

Commit f5d1e26

Browse files
committed
feat: update readme doc to reflect rsa implementation
1 parent c1f8600 commit f5d1e26

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The Encryption library provides a set of methods for encrypting and decrypting d
1212
- [Usage](#usage)
1313
- [API Reference](#api-reference)
1414
- [Examples](#examples)
15+
- [RSA Encryption and Decryption](#rsa)
1516
- [Contributing](#contributing)
1617
- [License](#license)
1718

@@ -121,6 +122,9 @@ class Program
121122
}
122123
}
123124
```
125+
## Rsa
126+
For more details on RSA Encryption, check the [Rsa.md](../doc/Rsa.md) document.
127+
124128

125129
## Contributing
126130

Rsa.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# RSA Encryption and Decryption
2+
3+
## Overview
4+
5+
This library provides a straightforward implementation of RSA encryption and decryption in C# using the .NET `RSACryptoServiceProvider`.
6+
It includes methods for generating RSA key pairs, encrypting data with a public key, and decrypting data with a private key.
7+
8+
## Table of Contents
9+
10+
- [Usage](#usage)
11+
- [Generate RSA Keys](#generate-rsa-keys)
12+
- [Encrypt and Decrypt using RSA](#encrypt-and-decrypt-using-rsa)
13+
14+
## Usage
15+
16+
### Generate RSA Keys
17+
18+
```csharp
19+
using SafeCrypt.Helpers;
20+
21+
var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048);
22+
23+
string rsaPublicKey = rsaKeyPair.Item1;
24+
string rsaPrivateKey = rsaKeyPair.Item2;
25+
26+
Console.WriteLine($"Public Key: {rsaPublicKey}");
27+
Console.WriteLine($"Private Key: {rsaPrivateKey}");
28+
```
29+
30+
### Encrypt and Decrypt using RSA
31+
32+
```csharp
33+
using SafeCrypt.RsaEncryption;
34+
35+
// Encrypt
36+
string originalData = "Hello, RSA Encryption!";
37+
38+
var encryptionModel = new RsaEncryptionParameters
39+
{
40+
DataToEncrypt = originalData,
41+
PublicKey = rsaPublicKey,
42+
};
43+
44+
var encryptedData = await Rsa.EncryptAsync(encryptionModel);
45+
46+
Console.WriteLine($"Original Data: {originalData}");
47+
Console.WriteLine("Encrypted Data: " + BitConverter.ToString(encryptedData.EncryptedData));
48+
49+
// Convert encrypted byte array to Base64 string
50+
string encryptedDataConvertedString = Convert.ToBase64String(encryptedData.EncryptedData);
51+
52+
// Convert string back to byte array for decryption
53+
byte[] convertedBytes = Convert.FromBase64String(encryptedDataConvertedString);
54+
55+
bool arraysAreEqual = StructuralComparisons.StructuralEqualityComparer.Equals(encryptedData.EncryptedData, convertedBytes);
56+
Console.WriteLine("Original and converted byte arrays are equal: " + arraysAreEqual); // should return true
57+
58+
59+
60+
// Decrypt
61+
var decryptionModel = new RsaDecryptionParameters
62+
{
63+
DataToDecrypt = convertedBytes, // encryptedData.EncryptedData
64+
PrivateKey = rsaPrivateKey,
65+
};
66+
67+
var decryptedData = await Rsa.DecryptAsync(decryptionModel);
68+
69+
// if Error occurs during encryption
70+
if (decryptedData.Errors.Count > 0)
71+
{
72+
Console.WriteLine("Decryption Errors:");
73+
foreach (var error in decryptedData.Errors)
74+
{
75+
Console.WriteLine(error);
76+
}
77+
}
78+
else
79+
{
80+
Console.WriteLine($"Decrypted Data: {decryptedData.DecryptedData}");
81+
}
82+
83+
// Note: The return type from Rsa.EncryptAsync is `EncryptionResult`, and Rsa.DecryptAsync is `DecryptionResult`.
84+
// Both models include a list of errors encountered during encryption/decryption.
85+
86+
```
87+
## Contributing
88+
89+
Contributions are welcome! Feel free to open issues, submit pull requests, or provide feedback.

SafeCrypt.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt", "src\SafeCrypt.
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt.App", "src\SafeCrypt.Test\SafeCrypt.App.csproj", "{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}"
1515
EndProject
16+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{632FAE12-6DE3-4B21-ADBB-F5222B35F707}"
17+
ProjectSection(SolutionItems) = preProject
18+
Rsa.md = Rsa.md
19+
EndProjectSection
20+
EndProject
1621
Global
1722
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1823
Debug|Any CPU = Debug|Any CPU
@@ -36,6 +41,7 @@ Global
3641
{1D91E775-F63F-4537-B81E-B8F9A6480D6D} = {0B7C0C60-9850-4554-AF85-86C0378B6B16}
3742
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8} = {8507D130-9F07-426C-8EE6-0AC714CF72E5}
3843
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6} = {1D91E775-F63F-4537-B81E-B8F9A6480D6D}
44+
{632FAE12-6DE3-4B21-ADBB-F5222B35F707} = {0B7C0C60-9850-4554-AF85-86C0378B6B16}
3945
EndGlobalSection
4046
GlobalSection(ExtensibilityGlobals) = postSolution
4147
SolutionGuid = {639A4359-2BA4-4F71-9EBF-D6EAB68C84CB}

0 commit comments

Comments
 (0)