Skip to content

Commit 7b9506f

Browse files
authored
Merge pull request #44 from selfmadecode/dev_ralph_rsa
feat: add RSA cryptographic algorithms
2 parents 79ab16f + de1b13d commit 7b9506f

10 files changed

Lines changed: 488 additions & 9 deletions

File tree

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ A C# library for encryption and decryption.
44

55
## Overview
66

7-
The Encryption library provides a set of methods for encrypting and decrypting data using the Advanced Encryption Standard (AES) algorithm, and other algorithm. It is designed to be easy to use and can be integrated into C# applications that require secure data transmission or storage.
8-
7+
The SafeCrypt library provides a set of methods for encrypting and decrypting data using various encryption algorithms,
8+
including the Advanced Encryption Standard (AES) and RSA (Rivest–Shamir–Adleman).
9+
It is designed to be easy to use and can be integrated into C# applications that require secure data transmission or storage.
910
## Table of Contents
1011

1112
- [Installation](#installation)
12-
- [Usage](#usage)
13-
- [API Reference](#api-reference)
14-
- [Examples](#examples)
13+
- [AES Encryption and Decryption usage](#usage)
14+
- [RSA Encryption and Decryption usage](#rsa)
1515
- [Contributing](#contributing)
1616
- [License](#license)
1717

@@ -34,9 +34,10 @@ To use the SafeCrypt library in your C# project, follow these steps:
3434

3535
Now, you can reference the SafeCrypt library in your C# project.
3636

37-
## Basic Usage
37+
## Usage
3838

39-
To use the library in your C# application, instantiate the `AesEncryption` or `AesDecryption` class and call the provided methods. Here's a simple example:
39+
To use the AES encryption in your C# application,
40+
instantiate the `AesEncryption` or `AesDecryption` class and call the provided methods. Here's a simple example:
4041

4142
```csharp
4243
using SafeCrypt.AESDecryption;
@@ -122,6 +123,14 @@ class Program
122123
}
123124
```
124125

126+
127+
## Rsa
128+
This library provides a straightforward implementation of RSA encryption and decryption in C# using the .NET `RSACryptoServiceProvider`.
129+
It includes methods for generating RSA key pairs, encrypting data with a public key, and decrypting data with a private key.
130+
131+
For more details on RSA Encryption, check the [Rsa.md](doc/Rsa.md) document.
132+
133+
125134
## Contributing
126135

127136
If you would like to contribute to the development of the SafeCrypt library, follow these steps:

doc/Rsa.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
using SafeCrypt.RsaEncryption;
21+
22+
var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048);
23+
24+
string rsaPublicKey = rsaKeyPair.Item1;
25+
string rsaPrivateKey = rsaKeyPair.Item2;
26+
27+
Console.WriteLine($"Public Key: {rsaPublicKey}");
28+
Console.WriteLine($"Private Key: {rsaPrivateKey}");
29+
```
30+
31+
### Encrypt and Decrypt using RSA
32+
33+
```csharp
34+
using SafeCrypt.RsaEncryption;
35+
36+
// Encrypt
37+
string originalData = "Hello, RSA Encryption!";
38+
39+
var encryptionModel = new RsaEncryptionParameters
40+
{
41+
DataToEncrypt = originalData,
42+
PublicKey = rsaPublicKey,
43+
};
44+
45+
var encryptedData = await Rsa.EncryptAsync(encryptionModel);
46+
47+
Console.WriteLine($"Original Data: {originalData}");
48+
Console.WriteLine("Encrypted Data: " + BitConverter.ToString(encryptedData.EncryptedData));
49+
50+
// Convert encrypted byte array to Base64 string
51+
string encryptedDataConvertedString = Convert.ToBase64String(encryptedData.EncryptedData);
52+
53+
// Convert string back to byte array for decryption
54+
byte[] convertedBytes = Convert.FromBase64String(encryptedDataConvertedString);
55+
56+
bool arraysAreEqual = StructuralComparisons.StructuralEqualityComparer.Equals(encryptedData.EncryptedData, convertedBytes);
57+
Console.WriteLine("Original and converted byte arrays are equal: " + arraysAreEqual); // should return true
58+
59+
60+
61+
// Decrypt
62+
var decryptionModel = new RsaDecryptionParameters
63+
{
64+
DataToDecrypt = convertedBytes, // encryptedData.EncryptedData
65+
PrivateKey = rsaPrivateKey,
66+
};
67+
68+
var decryptedData = await Rsa.DecryptAsync(decryptionModel);
69+
70+
// if Error occurs during encryption
71+
if (decryptedData.Errors.Count > 0)
72+
{
73+
Console.WriteLine("Decryption Errors:");
74+
foreach (var error in decryptedData.Errors)
75+
{
76+
Console.WriteLine(error);
77+
}
78+
}
79+
else
80+
{
81+
Console.WriteLine($"Decrypted Data: {decryptedData.DecryptedData}");
82+
}
83+
84+
// Note: The return type from Rsa.EncryptAsync is `EncryptionResult`, and Rsa.DecryptAsync is `DecryptionResult`.
85+
// Both models include a list of errors encountered during encryption/decryption.
86+
87+
```
88+
## Contributing
89+
90+
Contributions are welcome! Feel free to open issues, submit pull requests, or provide feedback.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace SafeCrypt.RsaEncryption
4+
{
5+
public sealed class RsaEncryptionParameters : IEncryptionData
6+
{
7+
/// <summary>
8+
/// Gets or sets the public key for RSA encryption.
9+
/// </summary>
10+
[Required]
11+
public string PublicKey { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets the data to be encrypted using RSA.
15+
/// </summary>
16+
[Required]
17+
public string DataToEncrypt { get; set; }
18+
}
19+
20+
public sealed class RsaDecryptionParameters
21+
{
22+
/// <summary>
23+
/// Gets or sets the public key for RSA encryption.
24+
/// </summary>
25+
[Required]
26+
public string PrivateKey { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets the data to be encrypted using RSA.
30+
/// </summary>
31+
[Required]
32+
public byte[] DataToDecrypt { get; set; }
33+
}
34+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace SafeCrypt.RsaEncryption.Models
6+
{
7+
public class RsaEncryptionResult
8+
{
9+
/// <summary>
10+
/// Gets or sets the encrypted data.
11+
/// </summary>
12+
public byte[] EncryptedData { get; set; }
13+
14+
/// <summary>
15+
/// Gets or sets the list of errors encountered during encryption.
16+
/// </summary>
17+
public List<string> Errors { get; set; }
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="RsaEncryptionResult"/> class.
21+
/// </summary>
22+
public RsaEncryptionResult()
23+
{
24+
Errors = new List<string>();
25+
}
26+
}
27+
28+
public class RsaDecryptionResult
29+
{
30+
/// <summary>
31+
/// Gets or sets the encrypted data.
32+
/// </summary>
33+
public byte[] DecryptedData { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the list of errors encountered during encryption.
37+
/// </summary>
38+
public List<string> Errors { get; set; }
39+
40+
///// <summary>
41+
///// Gets or sets the public key used for encryption.
42+
///// </summary>
43+
//public string PublicKey { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets the private key used for encryption.
47+
/// </summary>
48+
public string PrivateKey { get; set; }
49+
50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="RsaEncryptionResult"/> class.
52+
/// </summary>
53+
public RsaDecryptionResult()
54+
{
55+
Errors = new List<string>();
56+
}
57+
}
58+
59+
public class EncryptionResult
60+
{
61+
/// <summary>
62+
/// Gets or sets the list of errors encountered during encryption.
63+
/// </summary>
64+
public List<string> Errors { get; set; }
65+
66+
/// <summary>
67+
/// Gets or sets the encrypted data.
68+
/// </summary>
69+
public byte[] EncryptedData { get; set; }
70+
71+
public EncryptionResult()
72+
{
73+
Errors = new List<string>();
74+
}
75+
}
76+
77+
public class DecryptionResult
78+
{
79+
/// <summary>
80+
/// Gets or sets the list of errors encountered during encryption.
81+
/// </summary>
82+
public List<string> Errors { get; set; }
83+
84+
/// <summary>
85+
/// Gets or sets the encrypted data.
86+
/// </summary>
87+
public string DecryptedData { get; set; }
88+
89+
public DecryptionResult()
90+
{
91+
Errors = new List<string>();
92+
}
93+
}
94+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading.Tasks;
4+
using SafeCrypt.RsaEncryption.Models;
5+
6+
namespace SafeCrypt.RsaEncryption
7+
{
8+
9+
10+
public static class Rsa
11+
{
12+
/// <summary>
13+
/// Asynchronously encrypts the specified data using RSA encryption.
14+
/// </summary>
15+
/// <param name="model">The parameters for RSA encryption.</param>
16+
/// <returns>An <see cref="EncryptionResult"/> containing the encrypted data, and errors (if any).</returns>
17+
public static async Task<EncryptionResult> EncryptAsync(RsaEncryptionParameters model)
18+
{
19+
var result = new EncryptionResult();
20+
21+
if(string.IsNullOrWhiteSpace(model.DataToEncrypt))
22+
{
23+
result.Errors.Add($"Data cannot be null {nameof(model.DataToEncrypt)}");
24+
return result;
25+
}
26+
27+
if (string.IsNullOrWhiteSpace(model.PublicKey))
28+
{
29+
result.Errors.Add($"PublicKey cannot be null {nameof(model.PublicKey)}");
30+
return result;
31+
}
32+
33+
// asynchronously perform RSA encryption
34+
var data = await RsaEncryption.EncryptAsync(model.DataToEncrypt, model.PublicKey);
35+
36+
if(data.Errors.Count > 0)
37+
{
38+
result.Errors.AddRange(data.Errors);
39+
return result;
40+
}
41+
42+
result.EncryptedData = data.EncryptedData;
43+
return result;
44+
}
45+
46+
public static async Task<DecryptionResult> DecryptAsync(RsaDecryptionParameters model)
47+
{
48+
var result = new DecryptionResult();
49+
50+
if(model.DataToDecrypt == null)
51+
{
52+
result.Errors.Add($"DataToDecrypt cannot be null {nameof(model.DataToDecrypt)}");
53+
return result;
54+
}
55+
56+
if (string.IsNullOrWhiteSpace(model.PrivateKey))
57+
{
58+
result.Errors.Add($"PrivateKey cannot be null {nameof(model.PrivateKey)}");
59+
return result;
60+
}
61+
62+
// asynchronously perform RSA encryption
63+
var data = await RsaEncryption.DecryptAsync(model.DataToDecrypt, model.PrivateKey);
64+
65+
if (data.Errors.Count > 0)
66+
{
67+
result.Errors.AddRange(data.Errors);
68+
return result;
69+
}
70+
71+
result.DecryptedData = Encoding.UTF8.GetString(data.DecryptedData);
72+
return result;
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)