From 4b028498b34be04677a5cb6092ae5c5286470933 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 6 Feb 2024 14:44:57 +0100 Subject: [PATCH 01/35] feat: add RsaEncryptionParameters to hold parameters for encryption --- .../Models/Encrypt/RsaEncryptionParameters.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs new file mode 100644 index 0000000..ee9d4b8 --- /dev/null +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs @@ -0,0 +1,20 @@ +namespace SafeCrypt.RsaEncryption.Models +{ + internal class RsaEncryptionParameters : IEncryptionData + { + /// + /// Gets or sets the public key for RSA encryption. + /// + public string PublicKey { get; set; } + + /// + /// Gets or sets the private key for RSA encryption. + /// + public string PrivateKey { get; set; } + + /// + /// Gets or sets the data to be encrypted using RSA. + /// + public string DataToEncrypt { get; set; } + } +} From c10203ae290a431b6cdefea2431248c914c2ad41 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 6 Feb 2024 14:45:29 +0100 Subject: [PATCH 02/35] feat: add RsaEncryptionResult --- .../Models/RsaEncryptionResult.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs new file mode 100644 index 0000000..87d6430 --- /dev/null +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SafeCrypt.RsaEncryption.Models +{ + public class RsaEncryptionResult + { + /// + /// Gets or sets the encrypted data. + /// + public byte[] EncryptedData { get; set; } + + /// + /// Gets or sets the list of errors encountered during encryption. + /// + public List Errors { get; set; } + + /// + /// Gets or sets the public key used for encryption. + /// + public string PublicKey { get; set; } + + /// + /// Gets or sets the private key used for encryption. + /// + public string PrivateKey { get; set; } + + /// + /// Initializes a new instance of the class. + /// + public RsaEncryptionResult() + { + Errors = new List(); + } + } +} From ea3b02ccc75eeb7e1620a97ec7125b491a8b8198 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 6 Feb 2024 14:47:30 +0100 Subject: [PATCH 03/35] feat: add IEncryptionData interface to hold related properties --- src/SafeCrypt.Lib/Interface/IEncryptionData.cs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/SafeCrypt.Lib/Interface/IEncryptionData.cs diff --git a/src/SafeCrypt.Lib/Interface/IEncryptionData.cs b/src/SafeCrypt.Lib/Interface/IEncryptionData.cs new file mode 100644 index 0000000..b0418df --- /dev/null +++ b/src/SafeCrypt.Lib/Interface/IEncryptionData.cs @@ -0,0 +1,7 @@ +namespace SafeCrypt +{ + internal interface IEncryptionData + { + string DataToEncrypt { get; set; } + } +} From b258e2c4e8f8b8e6ca9505446d877c238ac664a9 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 6 Feb 2024 14:49:12 +0100 Subject: [PATCH 04/35] feat: add base Rsa encryption class with method for data encryption and decryption including method to generate keys --- .../Encryption/RsaEncryption/RsaEncryption.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs new file mode 100644 index 0000000..e50de3d --- /dev/null +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs @@ -0,0 +1,65 @@ +using System.Security.Cryptography; +using System.Text; +using System; +using System.Threading.Tasks; + +namespace SafeCrypt.RsaEncryption +{ + public class RsaEncryption + { + /// + /// Generates RSA key pair. + /// + /// The size of the key pair (e.g., 1024, 2048 bits). + /// The generated RSA key pair. + public static Tuple GenerateRsaKeys(int keySize) + { + using (var rsa = new RSACryptoServiceProvider(keySize)) + { + string publicKey = rsa.ToXmlString(false); // Don't include private key + string privateKey = rsa.ToXmlString(true); // Include private key + + return new Tuple(publicKey, privateKey); + } + } + + /// + /// Encrypts data using RSA public key. + /// + /// The data to be encrypted. + /// The RSA public key. + /// The encrypted data. + public static async Task EncryptAsync(string data, string publicKey) + { + + return await Task.Run(() => { + using (var rsa = new RSACryptoServiceProvider()) + { + rsa.FromXmlString(publicKey); + byte[] dataBytes = Encoding.UTF8.GetBytes(data); + byte[] encryptedData = rsa.Encrypt(dataBytes, false); + return encryptedData; + } + }); + } + + /// + /// Decrypts data using RSA private key. + /// + /// The encrypted data. + /// The RSA private key. + /// The decrypted data. + public static async Task DecryptAsync(byte[] encryptedData, string privateKey) + { + return await Task.Run(() => + { + using (var rsa = new RSACryptoServiceProvider()) + { + rsa.FromXmlString(privateKey); + byte[] decryptedData = rsa.Decrypt(encryptedData, false); + return Encoding.UTF8.GetString(decryptedData); + } + }); + } + } +} From 6a932efb1b85a03a2cb586befb6f4e63e16144de Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 6 Feb 2024 15:12:05 +0100 Subject: [PATCH 05/35] feat: comment out Public key from RsaEncryptionResult --- .../RsaEncryption/Models/RsaEncryptionResult.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs index 87d6430..5555428 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs @@ -16,10 +16,10 @@ public class RsaEncryptionResult /// public List Errors { get; set; } - /// - /// Gets or sets the public key used for encryption. - /// - public string PublicKey { get; set; } + ///// + ///// Gets or sets the public key used for encryption. + ///// + //public string PublicKey { get; set; } /// /// Gets or sets the private key used for encryption. From bc1b5463ccbcc51735cc25786c6852d7fb94c45e Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 6 Feb 2024 15:13:20 +0100 Subject: [PATCH 06/35] feat: use try catch for encryption --- .../Encryption/RsaEncryption/RsaEncryption.cs | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs index e50de3d..5847d85 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs @@ -2,6 +2,7 @@ using System.Text; using System; using System.Threading.Tasks; +using SafeCrypt.RsaEncryption.Models; namespace SafeCrypt.RsaEncryption { @@ -29,18 +30,30 @@ public static Tuple GenerateRsaKeys(int keySize) /// The data to be encrypted. /// The RSA public key. /// The encrypted data. - public static async Task EncryptAsync(string data, string publicKey) + public static async Task EncryptAsync(string data, string publicKey) { + var result = new RsaEncryptionResult(); - return await Task.Run(() => { - using (var rsa = new RSACryptoServiceProvider()) + try + { + var encryptedData = await Task.Run(() => { - rsa.FromXmlString(publicKey); - byte[] dataBytes = Encoding.UTF8.GetBytes(data); - byte[] encryptedData = rsa.Encrypt(dataBytes, false); - return encryptedData; - } - }); + using (var rsa = new RSACryptoServiceProvider()) + { + rsa.FromXmlString(publicKey); + byte[] dataBytes = Encoding.UTF8.GetBytes(data); + return rsa.Encrypt(dataBytes, false); + } + }); + + result.EncryptedData = encryptedData; + } + catch (Exception ex) + { + result.Errors.Add(ex.Message); + } + + return result; } /// From 668f63305eb153010a2d1e8cb28bd9d4d23354c4 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 6 Feb 2024 15:30:06 +0100 Subject: [PATCH 07/35] feat: move rsa key generation method to KeyGenerator class --- .../Encryption/RsaEncryption/RsaEncryption.cs | 18 +----------- src/SafeCrypt.Lib/Helpers/KeyGenerators.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs index 5847d85..7dd5bda 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs @@ -7,23 +7,7 @@ namespace SafeCrypt.RsaEncryption { public class RsaEncryption - { - /// - /// Generates RSA key pair. - /// - /// The size of the key pair (e.g., 1024, 2048 bits). - /// The generated RSA key pair. - public static Tuple GenerateRsaKeys(int keySize) - { - using (var rsa = new RSACryptoServiceProvider(keySize)) - { - string publicKey = rsa.ToXmlString(false); // Don't include private key - string privateKey = rsa.ToXmlString(true); // Include private key - - return new Tuple(publicKey, privateKey); - } - } - + { /// /// Encrypts data using RSA public key. /// diff --git a/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs b/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs index 44a2c5a..3625465 100644 --- a/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs +++ b/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs @@ -67,5 +67,33 @@ public static string GenerateAesSecretKey(int keySize) return Convert.ToBase64String(aesAlg.Key); } } + + /// + /// Generates a pair of RSA public and private keys with the specified key size. + /// + /// The size of the key pair (e.g., 1024, 2048 bits). + /// + /// A containing the generated RSA public and private keys. + /// Item1 represents the public key, and Item2 represents the private key. + /// + /// + /// The generated keys are in XML format. The public key does not include the private key, + /// while the private key includes both public and private components. + /// + /// The size of the key pair (e.g., 1024, 2048 bits). + /// A tuple containing the generated RSA public and private keys. + /// + /// Thrown if an error occurs during key generation. + /// + public static Tuple GenerateRsaKeys(int keySize) + { + using (var rsa = new RSACryptoServiceProvider(keySize)) + { + string publicKey = rsa.ToXmlString(false); // Don't include private key + string privateKey = rsa.ToXmlString(true); // Include private key + + return new Tuple(publicKey, privateKey); + } + } } } From f2d1d7d76a4dd8342042eeee0d261b8d05fcc7cb Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Wed, 7 Feb 2024 13:33:14 +0100 Subject: [PATCH 08/35] feat: rename algorithm class to rsa --- .../Encryption/RsaEncryption/Rsa.cs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs new file mode 100644 index 0000000..b15d394 --- /dev/null +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs @@ -0,0 +1,80 @@ +using System; +using System.Text; +using System.Threading.Tasks; +using SafeCrypt.RsaEncryption.Models; + +namespace SafeCrypt.RsaEncryption +{ + + + public static class Rsa + { + /// + /// Asynchronously encrypts the specified data using RSA encryption. + /// + /// The parameters for RSA encryption. + /// An containing the encrypted data, and errors (if any). + public static async Task 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) + { + // If there are errors in the encryption process, add them to the result and return + result.Errors.AddRange(data.Errors); + return result; + } + + // Convert the encrypted data to a hexadecimal string + //result.EncryptedData = BitConverter.ToString(data.EncryptedData); + result.EncryptedData = Encoding.UTF8.GetString(data.EncryptedData); //Encoding.UTF8.GetString(decryptedData) + return result; + } + + public static async Task DecryptAsync(RsaDecryptionParameters model) + { + var result = new DecryptionResult(); + + if (string.IsNullOrWhiteSpace(model.DataToDecrypt)) + { + result.Errors.Add($"Data 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) + { + // if there are errors in the encryption process, add them to the result and return + result.Errors.AddRange(data.Errors); + return result; + } + + // convert the encrypted data to a hexadecimal string + result.DecryptedData = Encoding.UTF8.GetString(data.DecryptedData); + return result; + } + } +} From c50d51e85131ddaaa6817b041ff2041209d570f1 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Wed, 7 Feb 2024 14:09:07 +0100 Subject: [PATCH 09/35] feat: use byte[] to hold encrypted data --- .../Models/Encrypt/RsaEncryptionParameters.cs | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs index ee9d4b8..f1c6073 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs @@ -1,20 +1,34 @@ -namespace SafeCrypt.RsaEncryption.Models +using System.ComponentModel.DataAnnotations; + +namespace SafeCrypt.RsaEncryption.Models { - internal class RsaEncryptionParameters : IEncryptionData + public sealed class RsaEncryptionParameters : IEncryptionData { /// /// Gets or sets the public key for RSA encryption. /// - public string PublicKey { get; set; } + [Required] + public string PublicKey { get; set; } + + /// + /// Gets or sets the data to be encrypted using RSA. + /// + [Required] + public string DataToEncrypt { get; set; } + } + public sealed class RsaDecryptionParameters + { /// - /// Gets or sets the private key for RSA encryption. + /// Gets or sets the public key for RSA encryption. /// + [Required] public string PrivateKey { get; set; } /// /// Gets or sets the data to be encrypted using RSA. /// - public string DataToEncrypt { get; set; } + [Required] + public byte[] DataToDecrypt { get; set; } } } From dcc1b7eb04dba4814c2f12a0372723c041cd513d Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Wed, 7 Feb 2024 17:51:29 +0100 Subject: [PATCH 10/35] feat: comment out byte check --- .../Encryption/RsaEncryption/Rsa.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs index b15d394..81eb84a 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs @@ -41,8 +41,11 @@ public static async Task EncryptAsync(RsaEncryptionParameters } // Convert the encrypted data to a hexadecimal string + //result.EncryptedData = data.EncryptedData; + //result.EncryptedData = Convert.ToBase64String(data.EncryptedData); //result.EncryptedData = BitConverter.ToString(data.EncryptedData); - result.EncryptedData = Encoding.UTF8.GetString(data.EncryptedData); //Encoding.UTF8.GetString(decryptedData) + //result.EncryptedData = Encoding.UTF8.GetString(data.EncryptedData); //Encoding.UTF8.GetString(decryptedData) + result.EncryptedData = data.EncryptedData; //Encoding.UTF8.GetString(decryptedData) return result; } @@ -50,11 +53,11 @@ public static async Task DecryptAsync(RsaDecryptionParameters { var result = new DecryptionResult(); - if (string.IsNullOrWhiteSpace(model.DataToDecrypt)) - { - result.Errors.Add($"Data cannot be null {nameof(model.DataToDecrypt)}"); - return result; - } + //if (string.IsNullOrWhiteSpace(model.DataToDecrypt)) + //{ + // result.Errors.Add($"Data cannot be null {nameof(model.DataToDecrypt)}"); + // return result; + //} if (string.IsNullOrWhiteSpace(model.PrivateKey)) { @@ -71,8 +74,7 @@ public static async Task DecryptAsync(RsaDecryptionParameters result.Errors.AddRange(data.Errors); return result; } - - // convert the encrypted data to a hexadecimal string + result.DecryptedData = Encoding.UTF8.GetString(data.DecryptedData); return result; } From 3f049005c15e3d5200dc40315f97f71c54236a98 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 8 Feb 2024 13:03:25 +0100 Subject: [PATCH 11/35] feat: test rsa algorithms --- src/SafeCrypt.Test/Program.cs | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index 79b733e..21cd922 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -2,7 +2,10 @@ using SafeCrypt.AESDecryption; using SafeCrypt.AESEncryption; +using SafeCrypt.Helpers; using SafeCrypt.Models; +using SafeCrypt.RsaEncryption; +using SafeCrypt.RsaEncryption.Models; var dataToEncrypt = "Data to Encrypt"; var secret = "hghjuytsdfraestwsgtere=="; @@ -37,4 +40,41 @@ Console.WriteLine($"Secret key: {decryptionData.SecretKey}"); + +/////////////////////////// +/// +// Example: Generate RSA keys +var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); +string rsaPublicKey = rsaKeyPair.Item1; +string rsaPrivateKey = rsaKeyPair.Item2; + +Console.WriteLine($"pubic key {rsaPublicKey}"); +Console.WriteLine($"private key {rsaPrivateKey}"); + +// Example: Encrypt and Decrypt using RSA +string originalData = "Hello, RSA Encryption!"; + +var enModel = new RsaEncryptionParameters +{ + DataToEncrypt = originalData, + PublicKey = rsaPublicKey, +}; + +var encryptedData = await Rsa.EncryptAsync(enModel); + +var uccRYTED = new RsaDecryptionParameters +{ + DataToDecrypt = encryptedData.EncryptedData, + PrivateKey = rsaPrivateKey +}; + +var decryptedData = await Rsa.DecryptAsync(uccRYTED); + +// Display results +Console.WriteLine($"Original Data: {originalData}"); +Console.WriteLine($"Encrypted Data: {encryptedData.EncryptedData}"); +//Console.WriteLine($"Encrypted Data-------: {BitConverter.ToString(encryptedData.EncryptedData)}"); +Console.WriteLine($"Decrypted Data: {decryptedData.DecryptedData}"); + + Console.WriteLine("Hello, World!"); From c1e911e63745f546d71b9522349ee69a149f547d Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 8 Feb 2024 13:05:26 +0100 Subject: [PATCH 12/35] feat: update rsa algorithm comments --- .../Encryption/RsaEncryption/RsaEncryption.cs | 55 ++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs index 7dd5bda..6dc57d9 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs @@ -6,15 +6,28 @@ namespace SafeCrypt.RsaEncryption { - public class RsaEncryption - { + public static class RsaEncryption + { /// - /// Encrypts data using RSA public key. + /// Asynchronously encrypts the provided data using the RSA (Rivest–Shamir–Adleman) algorithm. /// /// The data to be encrypted. - /// The RSA public key. - /// The encrypted data. - public static async Task EncryptAsync(string data, string publicKey) + /// The public key used for encryption. + /// + /// A task representing the asynchronous operation that, upon completion, + /// returns an containing the encrypted data. + /// + /// + /// This method uses the RSA algorithm to encrypt the input data with the provided public key. + /// The encryption is performed asynchronously using . + /// + /// The data to be encrypted. + /// The public key used for encryption. + /// + /// A task representing the asynchronous operation that, upon completion, + /// returns an containing the encrypted data. + /// + internal static async Task EncryptAsync(string data, string publicKey) { var result = new RsaEncryptionResult(); @@ -46,17 +59,31 @@ public static async Task EncryptAsync(string data, string p /// The encrypted data. /// The RSA private key. /// The decrypted data. - public static async Task DecryptAsync(byte[] encryptedData, string privateKey) + internal static async Task DecryptAsync(byte[] encryptedData, string privateKey) { - return await Task.Run(() => + var result = new RsaDecryptionResult(); + + try { - using (var rsa = new RSACryptoServiceProvider()) + var decryptedData = await Task.Run(() => { - rsa.FromXmlString(privateKey); - byte[] decryptedData = rsa.Decrypt(encryptedData, false); - return Encoding.UTF8.GetString(decryptedData); - } - }); + using (var rsa = new RSACryptoServiceProvider()) + { + rsa.FromXmlString(privateKey); + byte[] dataBytes = encryptedData; + return rsa.Decrypt(encryptedData, false); + } + }); + + result.DecryptedData = decryptedData; + + } + catch (Exception ex) + { + result.Errors.Add(ex.Message); + } + + return result; } } } From 2e1c45897e24e2fad01d6b1b3ca434d30a431422 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Fri, 9 Feb 2024 21:09:54 +0100 Subject: [PATCH 13/35] faet: add rsa encryption result --- .../Models/RsaEncryptionResult.cs | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs index 5555428..95d21d8 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs @@ -16,6 +16,27 @@ public class RsaEncryptionResult /// public List Errors { get; set; } + /// + /// Initializes a new instance of the class. + /// + public RsaEncryptionResult() + { + Errors = new List(); + } + } + + public class RsaDecryptionResult + { + /// + /// Gets or sets the encrypted data. + /// + public byte[] DecryptedData { get; set; } + + /// + /// Gets or sets the list of errors encountered during encryption. + /// + public List Errors { get; set; } + ///// ///// Gets or sets the public key used for encryption. ///// @@ -29,7 +50,43 @@ public class RsaEncryptionResult /// /// Initializes a new instance of the class. /// - public RsaEncryptionResult() + public RsaDecryptionResult() + { + Errors = new List(); + } + } + + public class EncryptionResult + { + /// + /// Gets or sets the list of errors encountered during encryption. + /// + public List Errors { get; set; } + + /// + /// Gets or sets the encrypted data. + /// + public byte[] EncryptedData { get; set; } + + public EncryptionResult() + { + Errors = new List(); + } + } + + public class DecryptionResult + { + /// + /// Gets or sets the list of errors encountered during encryption. + /// + public List Errors { get; set; } + + /// + /// Gets or sets the encrypted data. + /// + public string DecryptedData { get; set; } + + public DecryptionResult() { Errors = new List(); } From 38b7058f288db017479ca98263f3e714a75d324a Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:21:32 +0100 Subject: [PATCH 14/35] feat: add rsa usage class --- src/SafeCrypt.Test/Usage/RsaUsage.cs | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/SafeCrypt.Test/Usage/RsaUsage.cs diff --git a/src/SafeCrypt.Test/Usage/RsaUsage.cs b/src/SafeCrypt.Test/Usage/RsaUsage.cs new file mode 100644 index 0000000..177707f --- /dev/null +++ b/src/SafeCrypt.Test/Usage/RsaUsage.cs @@ -0,0 +1,54 @@ +using SafeCrypt.Helpers; +using SafeCrypt.RsaEncryption; +using System.Collections; + +namespace safecrypt_testapp.Usage; + +internal class RsaUsage +{ + internal async void Usage() + { + // Example: Generate RSA keys + var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); + + string rsaPublicKey = rsaKeyPair.Item1; + string rsaPrivateKey = rsaKeyPair.Item2; + + Console.WriteLine($"pubic key {rsaPublicKey}"); + Console.WriteLine($"private key {rsaPrivateKey}"); + + // Example: Encrypt and Decrypt using RSA + string originalData = "Hello, RSA Encryption!"; + + var enModel = new RsaEncryptionParameters + { + DataToEncrypt = originalData, + PublicKey = rsaPublicKey, + }; + + var encryptedData = await Rsa.EncryptAsync(enModel); + + Console.WriteLine($"Original Data: {originalData}"); + + Console.WriteLine("Original byte array: " + BitConverter.ToString(encryptedData.EncryptedData)); + string EncryptedDataconvertedString = Convert.ToBase64String(encryptedData.EncryptedData); + + byte[] convertedBytes = Convert.FromBase64String(EncryptedDataconvertedString); + + Console.WriteLine("Converted back to byte array: " + BitConverter.ToString(convertedBytes)); + bool arraysAreEqual = StructuralComparisons.StructuralEqualityComparer.Equals(encryptedData.EncryptedData, convertedBytes); + Console.WriteLine("Original and converted byte arrays are equal: " + arraysAreEqual); + + // Decrypting + var decryptionModel = new RsaDecryptionParameters + { + DataToDecrypt = convertedBytes, + PrivateKey = rsaPrivateKey + }; + + var decryptedData = await Rsa.DecryptAsync(decryptionModel); + Console.WriteLine($"{decryptedData.DecryptedData}"); + + Console.ReadLine(); + } +} From 4b71e03c74c2092add1f7b562c34c4db2cdc18bc Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:21:52 +0100 Subject: [PATCH 15/35] modify RsaEncryptionParameters namespace --- .../RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs index f1c6073..d688ff6 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/Encrypt/RsaEncryptionParameters.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations; -namespace SafeCrypt.RsaEncryption.Models +namespace SafeCrypt.RsaEncryption { public sealed class RsaEncryptionParameters : IEncryptionData { From c1f86002580788ab9762762357143deaa4dbad3a Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:22:17 +0100 Subject: [PATCH 16/35] remove rsa usage from program class --- src/SafeCrypt.Test/Program.cs | 43 +---------------------------------- 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index 21cd922..8c3518c 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -2,10 +2,7 @@ using SafeCrypt.AESDecryption; using SafeCrypt.AESEncryption; -using SafeCrypt.Helpers; using SafeCrypt.Models; -using SafeCrypt.RsaEncryption; -using SafeCrypt.RsaEncryption.Models; var dataToEncrypt = "Data to Encrypt"; var secret = "hghjuytsdfraestwsgtere=="; @@ -39,42 +36,4 @@ Console.WriteLine($"IV key: {decryptionData.Iv}"); Console.WriteLine($"Secret key: {decryptionData.SecretKey}"); - - -/////////////////////////// -/// -// Example: Generate RSA keys -var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); -string rsaPublicKey = rsaKeyPair.Item1; -string rsaPrivateKey = rsaKeyPair.Item2; - -Console.WriteLine($"pubic key {rsaPublicKey}"); -Console.WriteLine($"private key {rsaPrivateKey}"); - -// Example: Encrypt and Decrypt using RSA -string originalData = "Hello, RSA Encryption!"; - -var enModel = new RsaEncryptionParameters -{ - DataToEncrypt = originalData, - PublicKey = rsaPublicKey, -}; - -var encryptedData = await Rsa.EncryptAsync(enModel); - -var uccRYTED = new RsaDecryptionParameters -{ - DataToDecrypt = encryptedData.EncryptedData, - PrivateKey = rsaPrivateKey -}; - -var decryptedData = await Rsa.DecryptAsync(uccRYTED); - -// Display results -Console.WriteLine($"Original Data: {originalData}"); -Console.WriteLine($"Encrypted Data: {encryptedData.EncryptedData}"); -//Console.WriteLine($"Encrypted Data-------: {BitConverter.ToString(encryptedData.EncryptedData)}"); -Console.WriteLine($"Decrypted Data: {decryptedData.DecryptedData}"); - - -Console.WriteLine("Hello, World!"); +Console.ReadLine(); From f5d1e26e8dbcc781bb466221ecc9e6c8e84e9219 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:22:46 +0100 Subject: [PATCH 17/35] feat: update readme doc to reflect rsa implementation --- README.md | 4 +++ Rsa.md | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ SafeCrypt.sln | 6 ++++ 3 files changed, 99 insertions(+) create mode 100644 Rsa.md diff --git a/README.md b/README.md index 6965cd2..c8ba4aa 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ The Encryption library provides a set of methods for encrypting and decrypting d - [Usage](#usage) - [API Reference](#api-reference) - [Examples](#examples) +- [RSA Encryption and Decryption](#rsa) - [Contributing](#contributing) - [License](#license) @@ -121,6 +122,9 @@ class Program } } ``` +## Rsa +For more details on RSA Encryption, check the [Rsa.md](../doc/Rsa.md) document. + ## Contributing diff --git a/Rsa.md b/Rsa.md new file mode 100644 index 0000000..12fe8b7 --- /dev/null +++ b/Rsa.md @@ -0,0 +1,89 @@ +# 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; + +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. \ No newline at end of file diff --git a/SafeCrypt.sln b/SafeCrypt.sln index c639949..84a53ab 100644 --- a/SafeCrypt.sln +++ b/SafeCrypt.sln @@ -13,6 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt", "src\SafeCrypt. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt.App", "src\SafeCrypt.Test\SafeCrypt.App.csproj", "{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{632FAE12-6DE3-4B21-ADBB-F5222B35F707}" + ProjectSection(SolutionItems) = preProject + Rsa.md = Rsa.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -36,6 +41,7 @@ Global {1D91E775-F63F-4537-B81E-B8F9A6480D6D} = {0B7C0C60-9850-4554-AF85-86C0378B6B16} {AE9FAE54-9854-4F98-A60F-19125CEAA3A8} = {8507D130-9F07-426C-8EE6-0AC714CF72E5} {DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6} = {1D91E775-F63F-4537-B81E-B8F9A6480D6D} + {632FAE12-6DE3-4B21-ADBB-F5222B35F707} = {0B7C0C60-9850-4554-AF85-86C0378B6B16} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {639A4359-2BA4-4F71-9EBF-D6EAB68C84CB} From 66eafce5ec9d40d54d0fb2d7cff2bf956a88e90b Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:31:00 +0100 Subject: [PATCH 18/35] remove unused code from rsa class --- .../Encryption/RsaEncryption/Rsa.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs index 81eb84a..22963f4 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Rsa.cs @@ -35,17 +35,11 @@ public static async Task EncryptAsync(RsaEncryptionParameters if(data.Errors.Count > 0) { - // If there are errors in the encryption process, add them to the result and return result.Errors.AddRange(data.Errors); return result; } - // Convert the encrypted data to a hexadecimal string - //result.EncryptedData = data.EncryptedData; - //result.EncryptedData = Convert.ToBase64String(data.EncryptedData); - //result.EncryptedData = BitConverter.ToString(data.EncryptedData); - //result.EncryptedData = Encoding.UTF8.GetString(data.EncryptedData); //Encoding.UTF8.GetString(decryptedData) - result.EncryptedData = data.EncryptedData; //Encoding.UTF8.GetString(decryptedData) + result.EncryptedData = data.EncryptedData; return result; } @@ -53,11 +47,11 @@ public static async Task DecryptAsync(RsaDecryptionParameters { var result = new DecryptionResult(); - //if (string.IsNullOrWhiteSpace(model.DataToDecrypt)) - //{ - // result.Errors.Add($"Data cannot be null {nameof(model.DataToDecrypt)}"); - // return result; - //} + if(model.DataToDecrypt == null) + { + result.Errors.Add($"DataToDecrypt cannot be null {nameof(model.DataToDecrypt)}"); + return result; + } if (string.IsNullOrWhiteSpace(model.PrivateKey)) { @@ -70,7 +64,6 @@ public static async Task DecryptAsync(RsaDecryptionParameters if (data.Errors.Count > 0) { - // if there are errors in the encryption process, add them to the result and return result.Errors.AddRange(data.Errors); return result; } From 996346351d76cf6d20bbb15489a47512ac879069 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:31:33 +0100 Subject: [PATCH 19/35] mark rsa encryption class as internal --- src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs index 6dc57d9..a5cd195 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs @@ -6,7 +6,7 @@ namespace SafeCrypt.RsaEncryption { - public static class RsaEncryption + internal static class RsaEncryption { /// /// Asynchronously encrypts the provided data using the RSA (Rivest–Shamir–Adleman) algorithm. From aab63fa262170c7c7a71528261b876c9f403fa2f Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:32:02 +0100 Subject: [PATCH 20/35] marke usage model as internal protected --- src/SafeCrypt.Test/Usage/RsaUsage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SafeCrypt.Test/Usage/RsaUsage.cs b/src/SafeCrypt.Test/Usage/RsaUsage.cs index 177707f..fc4ef22 100644 --- a/src/SafeCrypt.Test/Usage/RsaUsage.cs +++ b/src/SafeCrypt.Test/Usage/RsaUsage.cs @@ -6,7 +6,7 @@ namespace safecrypt_testapp.Usage; internal class RsaUsage { - internal async void Usage() + internal protected async void Usage() { // Example: Generate RSA keys var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); From 96107e96a818e2bcde8bb1a55b444ae9fc34eabe Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:36:10 +0100 Subject: [PATCH 21/35] update doc location --- Rsa.md => doc/Rsa.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Rsa.md => doc/Rsa.md (100%) diff --git a/Rsa.md b/doc/Rsa.md similarity index 100% rename from Rsa.md rename to doc/Rsa.md From 3282f502f8ac897975093265fa30162cac6bfecf Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:45:09 +0100 Subject: [PATCH 22/35] feat: update readme doc --- README.md | 21 +++++++++++++-------- SafeCrypt.sln | 6 ------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c8ba4aa..474bbf1 100644 --- a/README.md +++ b/README.md @@ -4,15 +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) -- [RSA Encryption and Decryption](#rsa) +- [AES usage](#usage) +- [RSA Encryption and Decryption usage](#rsa) - [Contributing](#contributing) - [License](#license) @@ -35,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; @@ -122,7 +122,12 @@ 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. diff --git a/SafeCrypt.sln b/SafeCrypt.sln index 84a53ab..c639949 100644 --- a/SafeCrypt.sln +++ b/SafeCrypt.sln @@ -13,11 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt", "src\SafeCrypt. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt.App", "src\SafeCrypt.Test\SafeCrypt.App.csproj", "{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{632FAE12-6DE3-4B21-ADBB-F5222B35F707}" - ProjectSection(SolutionItems) = preProject - Rsa.md = Rsa.md - EndProjectSection -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,7 +36,6 @@ Global {1D91E775-F63F-4537-B81E-B8F9A6480D6D} = {0B7C0C60-9850-4554-AF85-86C0378B6B16} {AE9FAE54-9854-4F98-A60F-19125CEAA3A8} = {8507D130-9F07-426C-8EE6-0AC714CF72E5} {DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6} = {1D91E775-F63F-4537-B81E-B8F9A6480D6D} - {632FAE12-6DE3-4B21-ADBB-F5222B35F707} = {0B7C0C60-9850-4554-AF85-86C0378B6B16} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {639A4359-2BA4-4F71-9EBF-D6EAB68C84CB} From fc89ae1b5dc41a9378714fa8b49d752c2f4e1e26 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:48:03 +0100 Subject: [PATCH 23/35] update rsa file location --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 474bbf1..9eefa2e 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ class Program 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. +For more details on RSA Encryption, check the [Rsa.md](doc/Rsa.md) document. ## Contributing From 4bac39792f2b047099c868520a397e124959ab34 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:52:30 +0100 Subject: [PATCH 24/35] add using statement in doc --- doc/Rsa.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/Rsa.md b/doc/Rsa.md index 12fe8b7..0d9a2d0 100644 --- a/doc/Rsa.md +++ b/doc/Rsa.md @@ -17,6 +17,7 @@ It includes methods for generating RSA key pairs, encrypting data with a public ```csharp using SafeCrypt.Helpers; +using SafeCrypt.RsaEncryption; var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); From 7487fdab40c14b24a73c9fb64dbf4a7d0d8afeb8 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:52:56 +0100 Subject: [PATCH 25/35] chnage rsausage class namespace --- src/SafeCrypt.Test/Usage/RsaUsage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SafeCrypt.Test/Usage/RsaUsage.cs b/src/SafeCrypt.Test/Usage/RsaUsage.cs index fc4ef22..b9bb1ad 100644 --- a/src/SafeCrypt.Test/Usage/RsaUsage.cs +++ b/src/SafeCrypt.Test/Usage/RsaUsage.cs @@ -2,7 +2,7 @@ using SafeCrypt.RsaEncryption; using System.Collections; -namespace safecrypt_testapp.Usage; +namespace SafeCrypt.App.Usage; internal class RsaUsage { From de1b13d811cab784ff3df322f50f31dd022a9985 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 19 Feb 2024 00:53:22 +0100 Subject: [PATCH 26/35] update readme file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9eefa2e..a27e036 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ It is designed to be easy to use and can be integrated into C# applications that ## Table of Contents - [Installation](#installation) -- [AES usage](#usage) +- [AES Encryption and Decryption usage](#usage) - [RSA Encryption and Decryption usage](#rsa) - [Contributing](#contributing) - [License](#license) From cc700d5de7b4391362242dfb3030723dfd6d7cec Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 20 Feb 2024 00:26:39 +0100 Subject: [PATCH 27/35] featL turned BaseAesEncryption class to static --- src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs index e4d1da7..46ff7d7 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs @@ -6,7 +6,7 @@ namespace SafeCrypt.AesEncryption { - public class BaseAesEncryption + public static class BaseAesEncryption { /// /// Encrypts the provided data using the Advanced Encryption Standard (AES) algorithm. From 3b2c538569d194741141fc9ee987ed0513feb3c0 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 20 Feb 2024 00:27:06 +0100 Subject: [PATCH 28/35] featL turned Decrypting class and methods to static --- .../Encryption/AesEncryption/Decrypting.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs index ae42d21..73884a2 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs @@ -7,7 +7,7 @@ namespace SafeCrypt.AESDecryption { - public class AesDecryption : BaseAesEncryption + public static class AesDecryption { /// /// Asynchronously decrypts data from a hexadecimal string using the specified decryption parameters and cipher mode. @@ -19,7 +19,7 @@ public class AesDecryption : BaseAesEncryption /// The task result is a object containing the decrypted data, IV, and secret key. /// If decryption fails, the object will contain error information. /// - public async Task DecryptFromHexStringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) + public static async Task DecryptFromHexStringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new DecryptionData(); @@ -55,7 +55,7 @@ public async Task DecryptFromHexStringAsync(DecryptionParameters Data = param.DataToDecrypt.HexadecimalStringToByteArray() }; - var response = await DecryptAsync(byteEncryptionParameters, mode); + var response = await BaseAesEncryption.DecryptAsync(byteEncryptionParameters, mode); return new DecryptionData { @@ -75,7 +75,7 @@ public async Task DecryptFromHexStringAsync(DecryptionParameters /// The task result is a object containing the decrypted data, IV, and secret key. /// If decryption fails, the object will contain error information. /// - public async Task DecryptFromBase64StringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) + public static async Task DecryptFromBase64StringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new DecryptionData(); @@ -103,7 +103,7 @@ public async Task DecryptFromBase64StringAsync(DecryptionParamet Data = Convert.FromBase64String(param.DataToDecrypt) }; - var response = await DecryptAsync(byteDecryptionParameters, mode); + var response = await BaseAesEncryption.DecryptAsync(byteDecryptionParameters, mode); return new DecryptionData { @@ -121,7 +121,7 @@ public async Task DecryptFromBase64StringAsync(DecryptionParamet } - private void NullChecks(string data, string secretKey, string iv) + private static void NullChecks(string data, string secretKey, string iv) { if (data == null || data.Length <= 0) throw new ArgumentNullException(nameof(data)); @@ -133,13 +133,13 @@ private void NullChecks(string data, string secretKey, string iv) throw new ArgumentNullException(nameof(iv)); } - private (byte[], byte[]) ConvertKeysToBytesAndGetKeys(string secretKey, string iv) + private static (byte[], byte[]) ConvertKeysToBytesAndGetKeys(string secretKey, string iv) { return (secretKey.ConvertKeysToBytes(), iv.ConvertKeysToBytes()); } - private void AddError(DecryptionData responseData, string error) + private static void AddError(DecryptionData responseData, string error) { responseData.HasError = true; responseData.Errors.Add(error); From 965234d2ddf1fbb7919fd145b62277340e06b07e Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 20 Feb 2024 00:27:42 +0100 Subject: [PATCH 29/35] featL turned Encrypting class and methods to static --- .../Encryption/AesEncryption/Encrypting.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs index ca76d78..ebbc839 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs @@ -7,7 +7,7 @@ namespace SafeCrypt.AESEncryption { - public class AesEncryption : BaseAesEncryption + public static class AesEncryption { /// /// Asynchronously encrypts the provided data using the specified secret key and initialization vector (IV). @@ -23,7 +23,7 @@ public class AesEncryption : BaseAesEncryption /// The secret key used for encryption. /// The initialization vector used for encryption. /// The encrypted data as a byte array. - public async Task EncryptToHexStringAsync(EncryptionParameters param, CipherMode mode = CipherMode.CBC) + public static async Task EncryptToHexStringAsync(EncryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new EncryptionData(); @@ -52,7 +52,7 @@ public async Task EncryptToHexStringAsync(EncryptionParameters p Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = await EncryptAsync(byteEncryptionParameters, mode); + var response = await BaseAesEncryption.EncryptAsync(byteEncryptionParameters, mode); return new EncryptionData { @@ -84,7 +84,7 @@ public async Task EncryptToHexStringAsync(EncryptionParameters p /// /// Thrown if the base64secretKey is not a valid Base64-encoded string. /// - public async Task EncryptToBase64StringAsync(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC) + public static async Task EncryptToBase64StringAsync(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC) { // validate is base64 if (!Validators.IsBase64String(base64secretKey)) @@ -104,7 +104,7 @@ public async Task EncryptToBase64StringAsync(string dataToBeEncr Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = await EncryptAsync(byteEncryptionParameters, mode); + var response = await BaseAesEncryption.EncryptAsync(byteEncryptionParameters, mode); return new EncryptionData { @@ -114,7 +114,7 @@ public async Task EncryptToBase64StringAsync(string dataToBeEncr }; } - private EncryptionData ValidateEncryptionParameters(EncryptionParameters param) + private static EncryptionData ValidateEncryptionParameters(EncryptionParameters param) { var responseData = new EncryptionData(); @@ -134,7 +134,7 @@ private EncryptionData ValidateEncryptionParameters(EncryptionParameters param) return responseData; } - private void NullChecks(string data, string secretKey) + private static void NullChecks(string data, string secretKey) { if (data == null || data.Length <= 0) throw new ArgumentNullException(nameof(data)); @@ -143,7 +143,7 @@ private void NullChecks(string data, string secretKey) throw new ArgumentNullException(nameof(secretKey)); } - private void AddError(EncryptionData responseData, string error) + private static void AddError(EncryptionData responseData, string error) { responseData.HasError = true; responseData.Errors.Add(error); From 8411c9003fe1c84131affe16e3dd1149d4239f2a Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 20 Feb 2024 14:02:40 +0100 Subject: [PATCH 30/35] update program class to use static AES methods --- src/SafeCrypt.Test/Program.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index 79b733e..b971c9c 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -8,10 +8,9 @@ var secret = "hghjuytsdfraestwsgtere=="; // Encryption process -var encryptor = new AesEncryption(); // this method generates a random IV key for the encryption process // the IV is returned in the response with other properties -var response = await encryptor.EncryptToBase64StringAsync(dataToEncrypt, secret); +var response = await AesEncryption.EncryptToBase64StringAsync(dataToEncrypt, secret); Console.WriteLine("............Encryption Started............"); @@ -28,8 +27,7 @@ DataToDecrypt = response.EncryptedData }; -var decryptor = new AesDecryption(); -var decryptionData = await decryptor.DecryptFromBase64StringAsync(decryptorParam); +var decryptionData = await AesDecryption.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine("............Decryption Started............"); Console.WriteLine($"Decrypted data: { decryptionData.DecryptedData }"); From 5cf8ad8c85421478f67e1f5a593d8ae815b13eea Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Tue, 20 Feb 2024 14:05:00 +0100 Subject: [PATCH 31/35] update readme doc to reflect static method change --- README.md | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6965cd2..c70a4ef 100644 --- a/README.md +++ b/README.md @@ -47,15 +47,12 @@ class Program { static async Task Main() { - var aesEncryptor = new AesEncryption(); - var encryptedData = await aesEncryptor.EncryptToBase64StringAsync("Hello, World!", "gdjdtsraewsuteastwerse==" + var encryptedData = await AesEncryption.EncryptToBase64StringAsync("Hello, World!", "gdjdtsraewsuteastwerse==" Console.WriteLine($"Encrypted Data: {encryptedData.EncryptedData}"); Console.WriteLine($"Initialization Vector: {encryptedData.Iv}"); - - var aesDecryptor = new AesDecryption(); - + var parameterToDecrypt = new DecryptionParameters { DataToDecrypt = encryptedData.EncryptedData, @@ -64,7 +61,7 @@ class Program }; - var data = await aesDecryptor.DecryptFromBase64StringAsync(parameterToDecrypt) + var data = await AesDecryption.DecryptFromBase64StringAsync(parameterToDecrypt) Console.WriteLine($"Decrypted Data: {data.DecryptedData}"); Console.WriteLine($"Initialization Vector: {data.Iv}"); @@ -94,9 +91,8 @@ class Program SecretKey = secret }; - var encryptor = new AesEncryption(); - var response = await encryptor.EncryptToBase64StringAsync(encryptionParam.DataToEncrypt, secret); + var response = await AesEncryption.EncryptToBase64StringAsync(encryptionParam.DataToEncrypt, secret); Console.WriteLine(response.EncryptedData); Console.WriteLine(response.Iv); @@ -112,8 +108,7 @@ class Program }; - var decryptor = new AesDecryption(); - var decryptionData = await decryptor.DecryptFromBase64StringAsync(decryptorParam); + var decryptionData = await AesDecryption.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine(decryptionData.DecryptedData); Console.WriteLine(decryptionData.Iv); From 575b2ff5def88f3ab5d1e773ea9168e2322270c4 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 22 Feb 2024 11:09:23 +0100 Subject: [PATCH 32/35] feat: change aes encryption and decryption class into partial class feat: modified namespace to be AES for encryption and decryption --- src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs | 4 ++-- src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs index 73884a2..82164e4 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs @@ -5,9 +5,9 @@ using System.Security.Cryptography; using System.Threading.Tasks; -namespace SafeCrypt.AESDecryption +namespace SafeCrypt.AES { - public static class AesDecryption + public static partial class Aes { /// /// Asynchronously decrypts data from a hexadecimal string using the specified decryption parameters and cipher mode. diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs index ebbc839..293af57 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs @@ -5,9 +5,9 @@ using SafeCrypt.Helpers; using SafeCrypt.Models; -namespace SafeCrypt.AESEncryption +namespace SafeCrypt.AES { - public static class AesEncryption + public static partial class Aes { /// /// Asynchronously encrypts the provided data using the specified secret key and initialization vector (IV). From a3630c9840a28fbb6ebd4a4faa49e09dfb4efee2 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 22 Feb 2024 11:10:06 +0100 Subject: [PATCH 33/35] feat: update readme doc to use newly created AES class and namespace for encryption and decryption --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c70a4ef..01d45f1 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,7 @@ Now, you can reference the SafeCrypt library in your C# project. To use the library in your C# application, instantiate the `AesEncryption` or `AesDecryption` class and call the provided methods. Here's a simple example: ```csharp -using SafeCrypt.AESDecryption; -using SafeCrypt.AESEncryption; +using SafeCrypt.AES; using SafeCrypt.Models; class Program @@ -61,7 +60,7 @@ class Program }; - var data = await AesDecryption.DecryptFromBase64StringAsync(parameterToDecrypt) + var data = await Aes.DecryptFromBase64StringAsync(parameterToDecrypt) Console.WriteLine($"Decrypted Data: {data.DecryptedData}"); Console.WriteLine($"Initialization Vector: {data.Iv}"); @@ -71,8 +70,7 @@ class Program ------------------------------------------------------------------------------------------------------- -using SafeCrypt.AESDecryption; -using SafeCrypt.AESEncryption; +using SafeCrypt.AES; using SafeCrypt.Models; class Program @@ -92,7 +90,7 @@ class Program }; - var response = await AesEncryption.EncryptToBase64StringAsync(encryptionParam.DataToEncrypt, secret); + var response = await Aes.EncryptToBase64StringAsync(encryptionParam.DataToEncrypt, secret); Console.WriteLine(response.EncryptedData); Console.WriteLine(response.Iv); From d6d4e35bf0ba1e13a84c3cb774b2bfeeb3714017 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 22 Feb 2024 11:10:44 +0100 Subject: [PATCH 34/35] feat: update program class with namespace and class name change for aes class --- src/SafeCrypt.Test/Program.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index b971c9c..b50bb32 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -1,7 +1,6 @@ // See https://aka.ms/new-console-template for more information -using SafeCrypt.AESDecryption; -using SafeCrypt.AESEncryption; +using SafeCrypt.AES; using SafeCrypt.Models; var dataToEncrypt = "Data to Encrypt"; @@ -10,7 +9,7 @@ // Encryption process // this method generates a random IV key for the encryption process // the IV is returned in the response with other properties -var response = await AesEncryption.EncryptToBase64StringAsync(dataToEncrypt, secret); +var response = await Aes.EncryptToBase64StringAsync(dataToEncrypt, secret); Console.WriteLine("............Encryption Started............"); @@ -27,7 +26,7 @@ DataToDecrypt = response.EncryptedData }; -var decryptionData = await AesDecryption.DecryptFromBase64StringAsync(decryptorParam); +var decryptionData = await Aes.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine("............Decryption Started............"); Console.WriteLine($"Decrypted data: { decryptionData.DecryptedData }"); From eb6bd33d2ef4edd983739ad5630df3a720cd954c Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 22 Feb 2024 11:12:22 +0100 Subject: [PATCH 35/35] update readme doc --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 01d45f1..bbd703c 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ class Program static async Task Main() { - var encryptedData = await AesEncryption.EncryptToBase64StringAsync("Hello, World!", "gdjdtsraewsuteastwerse==" + var encryptedData = await Aes.EncryptToBase64StringAsync("Hello, World!", "gdjdtsraewsuteastwerse==" Console.WriteLine($"Encrypted Data: {encryptedData.EncryptedData}"); Console.WriteLine($"Initialization Vector: {encryptedData.Iv}"); @@ -106,7 +106,7 @@ class Program }; - var decryptionData = await AesDecryption.DecryptFromBase64StringAsync(decryptorParam); + var decryptionData = await Aes.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine(decryptionData.DecryptedData); Console.WriteLine(decryptionData.Iv);