-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add RSA cryptographic algorithms #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 a53a904
feat: update release note in csproj
selfmadecode 4b02849
feat: add RsaEncryptionParameters to hold parameters for encryption
selfmadecode c10203a
feat: add RsaEncryptionResult
selfmadecode ea3b02c
feat: add IEncryptionData interface to hold related properties
selfmadecode b258e2c
feat: add base Rsa encryption class with method for data encryption a…
selfmadecode 6a932ef
feat: comment out Public key from RsaEncryptionResult
selfmadecode bc1b546
feat: use try catch for encryption
selfmadecode 668f633
feat: move rsa key generation method to KeyGenerator class
selfmadecode f2d1d7d
feat: rename algorithm class to rsa
selfmadecode c50d51e
feat: use byte[] to hold encrypted data
selfmadecode dcc1b7e
feat: comment out byte check
selfmadecode 3f04900
feat: test rsa algorithms
selfmadecode c1e911e
feat: update rsa algorithm comments
selfmadecode 2e1c458
faet: add rsa encryption result
selfmadecode 38b7058
feat: add rsa usage class
selfmadecode 4b71e03
modify RsaEncryptionParameters namespace
selfmadecode c1f8600
remove rsa usage from program class
selfmadecode f5d1e26
feat: update readme doc to reflect rsa implementation
selfmadecode 66eafce
remove unused code from rsa class
selfmadecode 9963463
mark rsa encryption class as internal
selfmadecode aab63fa
marke usage model as internal protected
selfmadecode 96107e9
update doc location
selfmadecode 3282f50
feat: update readme doc
selfmadecode fc89ae1
update rsa file location
selfmadecode 4bac397
add using statement in doc
selfmadecode 7487fda
chnage rsausage class namespace
selfmadecode de1b13d
update readme file
selfmadecode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
34 changes: 34 additions & 0 deletions
34
src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.