Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d2e9566
Merge pull request #43 from selfmadecode/dev
selfmadecode Feb 2, 2024
a53a904
feat: update release note in csproj
selfmadecode Feb 2, 2024
4b02849
feat: add RsaEncryptionParameters to hold parameters for encryption
selfmadecode Feb 6, 2024
c10203a
feat: add RsaEncryptionResult
selfmadecode Feb 6, 2024
ea3b02c
feat: add IEncryptionData interface to hold related properties
selfmadecode Feb 6, 2024
b258e2c
feat: add base Rsa encryption class with method for data encryption a…
selfmadecode Feb 6, 2024
6a932ef
feat: comment out Public key from RsaEncryptionResult
selfmadecode Feb 6, 2024
bc1b546
feat: use try catch for encryption
selfmadecode Feb 6, 2024
668f633
feat: move rsa key generation method to KeyGenerator class
selfmadecode Feb 6, 2024
f2d1d7d
feat: rename algorithm class to rsa
selfmadecode Feb 7, 2024
c50d51e
feat: use byte[] to hold encrypted data
selfmadecode Feb 7, 2024
dcc1b7e
feat: comment out byte check
selfmadecode Feb 7, 2024
3f04900
feat: test rsa algorithms
selfmadecode Feb 8, 2024
c1e911e
feat: update rsa algorithm comments
selfmadecode Feb 8, 2024
2e1c458
faet: add rsa encryption result
selfmadecode Feb 9, 2024
38b7058
feat: add rsa usage class
selfmadecode Feb 18, 2024
4b71e03
modify RsaEncryptionParameters namespace
selfmadecode Feb 18, 2024
c1f8600
remove rsa usage from program class
selfmadecode Feb 18, 2024
f5d1e26
feat: update readme doc to reflect rsa implementation
selfmadecode Feb 18, 2024
66eafce
remove unused code from rsa class
selfmadecode Feb 18, 2024
9963463
mark rsa encryption class as internal
selfmadecode Feb 18, 2024
aab63fa
marke usage model as internal protected
selfmadecode Feb 18, 2024
96107e9
update doc location
selfmadecode Feb 18, 2024
3282f50
feat: update readme doc
selfmadecode Feb 18, 2024
fc89ae1
update rsa file location
selfmadecode Feb 18, 2024
4bac397
add using statement in doc
selfmadecode Feb 18, 2024
7487fda
chnage rsausage class namespace
selfmadecode Feb 18, 2024
de1b13d
update readme file
selfmadecode Feb 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions README.md

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 53 the closing bracket is missing.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ A C# library for encryption and decryption.

## Overview

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.

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

- [Installation](#installation)
- [Usage](#usage)
- [API Reference](#api-reference)
- [Examples](#examples)
- [AES Encryption and Decryption usage](#usage)
- [RSA Encryption and Decryption usage](#rsa)
- [Contributing](#contributing)
- [License](#license)

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

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

## Basic Usage
## Usage

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

```csharp
using SafeCrypt.AESDecryption;
Expand Down Expand Up @@ -122,6 +123,14 @@ class Program
}
```


## Rsa
This library provides a straightforward implementation of RSA encryption and decryption in C# using the .NET `RSACryptoServiceProvider`.
It includes methods for generating RSA key pairs, encrypting data with a public key, and decrypting data with a private key.

For more details on RSA Encryption, check the [Rsa.md](doc/Rsa.md) document.


## Contributing

If you would like to contribute to the development of the SafeCrypt library, follow these steps:
Expand Down
90 changes: 90 additions & 0 deletions doc/Rsa.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# RSA Encryption and Decryption

## Overview

This library provides a straightforward implementation of RSA encryption and decryption in C# using the .NET `RSACryptoServiceProvider`.
It includes methods for generating RSA key pairs, encrypting data with a public key, and decrypting data with a private key.

## Table of Contents

- [Usage](#usage)
- [Generate RSA Keys](#generate-rsa-keys)
- [Encrypt and Decrypt using RSA](#encrypt-and-decrypt-using-rsa)

## Usage

### Generate RSA Keys

```csharp
using SafeCrypt.Helpers;
using SafeCrypt.RsaEncryption;

var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048);

string rsaPublicKey = rsaKeyPair.Item1;
string rsaPrivateKey = rsaKeyPair.Item2;

Console.WriteLine($"Public Key: {rsaPublicKey}");
Console.WriteLine($"Private Key: {rsaPrivateKey}");
```

### Encrypt and Decrypt using RSA

```csharp
using SafeCrypt.RsaEncryption;

// Encrypt
string originalData = "Hello, RSA Encryption!";

var encryptionModel = new RsaEncryptionParameters
{
DataToEncrypt = originalData,
PublicKey = rsaPublicKey,
};

var encryptedData = await Rsa.EncryptAsync(encryptionModel);

Console.WriteLine($"Original Data: {originalData}");
Console.WriteLine("Encrypted Data: " + BitConverter.ToString(encryptedData.EncryptedData));

// Convert encrypted byte array to Base64 string
string encryptedDataConvertedString = Convert.ToBase64String(encryptedData.EncryptedData);

// Convert string back to byte array for decryption
byte[] convertedBytes = Convert.FromBase64String(encryptedDataConvertedString);

bool arraysAreEqual = StructuralComparisons.StructuralEqualityComparer.Equals(encryptedData.EncryptedData, convertedBytes);
Console.WriteLine("Original and converted byte arrays are equal: " + arraysAreEqual); // should return true



// Decrypt
var decryptionModel = new RsaDecryptionParameters
{
DataToDecrypt = convertedBytes, // encryptedData.EncryptedData
PrivateKey = rsaPrivateKey,
};

var decryptedData = await Rsa.DecryptAsync(decryptionModel);

// if Error occurs during encryption
if (decryptedData.Errors.Count > 0)
{
Console.WriteLine("Decryption Errors:");
foreach (var error in decryptedData.Errors)
{
Console.WriteLine(error);
}
}
else
{
Console.WriteLine($"Decrypted Data: {decryptedData.DecryptedData}");
}

// Note: The return type from Rsa.EncryptAsync is `EncryptionResult`, and Rsa.DecryptAsync is `DecryptionResult`.
// Both models include a list of errors encountered during encryption/decryption.

```
## Contributing

Contributions are welcome! Feel free to open issues, submit pull requests, or provide feedback.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.ComponentModel.DataAnnotations;

namespace SafeCrypt.RsaEncryption
{
public sealed class RsaEncryptionParameters : IEncryptionData
{
/// <summary>
/// Gets or sets the public key for RSA encryption.
/// </summary>
[Required]
public string PublicKey { get; set; }

/// <summary>
/// Gets or sets the data to be encrypted using RSA.
/// </summary>
[Required]
public string DataToEncrypt { get; set; }
}

public sealed class RsaDecryptionParameters
{
/// <summary>
/// Gets or sets the public key for RSA encryption.
/// </summary>
[Required]
public string PrivateKey { get; set; }

/// <summary>
/// Gets or sets the data to be encrypted using RSA.
/// </summary>
[Required]
public byte[] DataToDecrypt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SafeCrypt.RsaEncryption.Models
{
public class RsaEncryptionResult
{
/// <summary>
/// Gets or sets the encrypted data.
/// </summary>
public byte[] EncryptedData { get; set; }

/// <summary>
/// Gets or sets the list of errors encountered during encryption.
/// </summary>
public List<string> Errors { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="RsaEncryptionResult"/> class.
/// </summary>
public RsaEncryptionResult()
{
Errors = new List<string>();
}
}

public class RsaDecryptionResult
{
/// <summary>
/// Gets or sets the encrypted data.
/// </summary>
public byte[] DecryptedData { get; set; }

/// <summary>
/// Gets or sets the list of errors encountered during encryption.
/// </summary>
public List<string> Errors { get; set; }

///// <summary>
///// Gets or sets the public key used for encryption.
///// </summary>
//public string PublicKey { get; set; }

/// <summary>
/// Gets or sets the private key used for encryption.
/// </summary>
public string PrivateKey { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="RsaEncryptionResult"/> class.
/// </summary>
public RsaDecryptionResult()
{
Errors = new List<string>();
}
}

public class EncryptionResult
{
/// <summary>
/// Gets or sets the list of errors encountered during encryption.
/// </summary>
public List<string> Errors { get; set; }

/// <summary>
/// Gets or sets the encrypted data.
/// </summary>
public byte[] EncryptedData { get; set; }

public EncryptionResult()
{
Errors = new List<string>();
}
}

public class DecryptionResult
{
/// <summary>
/// Gets or sets the list of errors encountered during encryption.
/// </summary>
public List<string> Errors { get; set; }

/// <summary>
/// Gets or sets the encrypted data.
/// </summary>
public string DecryptedData { get; set; }

public DecryptionResult()
{
Errors = new List<string>();
}
}
}
75 changes: 75 additions & 0 deletions src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Text;
using System.Threading.Tasks;
using SafeCrypt.RsaEncryption.Models;

namespace SafeCrypt.RsaEncryption
{


public static class Rsa
{
/// <summary>
/// Asynchronously encrypts the specified data using RSA encryption.
/// </summary>
/// <param name="model">The parameters for RSA encryption.</param>
/// <returns>An <see cref="EncryptionResult"/> containing the encrypted data, and errors (if any).</returns>
public static async Task<EncryptionResult> EncryptAsync(RsaEncryptionParameters model)
{
var result = new EncryptionResult();

if(string.IsNullOrWhiteSpace(model.DataToEncrypt))
{
result.Errors.Add($"Data cannot be null {nameof(model.DataToEncrypt)}");
return result;
}

if (string.IsNullOrWhiteSpace(model.PublicKey))
{
result.Errors.Add($"PublicKey cannot be null {nameof(model.PublicKey)}");
return result;
}

// asynchronously perform RSA encryption
var data = await RsaEncryption.EncryptAsync(model.DataToEncrypt, model.PublicKey);

if(data.Errors.Count > 0)
{
result.Errors.AddRange(data.Errors);
return result;
}

result.EncryptedData = data.EncryptedData;
return result;
}

public static async Task<DecryptionResult> DecryptAsync(RsaDecryptionParameters model)
{
var result = new DecryptionResult();

if(model.DataToDecrypt == null)
{
result.Errors.Add($"DataToDecrypt cannot be null {nameof(model.DataToDecrypt)}");
return result;
}

if (string.IsNullOrWhiteSpace(model.PrivateKey))
{
result.Errors.Add($"PrivateKey cannot be null {nameof(model.PrivateKey)}");
return result;
}

// asynchronously perform RSA encryption
var data = await RsaEncryption.DecryptAsync(model.DataToDecrypt, model.PrivateKey);

if (data.Errors.Count > 0)
{
result.Errors.AddRange(data.Errors);
return result;
}

result.DecryptedData = Encoding.UTF8.GetString(data.DecryptedData);
return result;
}
}
}
Loading