|
| 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 | + // If there are errors in the encryption process, add them to the result and return |
| 39 | + result.Errors.AddRange(data.Errors); |
| 40 | + return result; |
| 41 | + } |
| 42 | + |
| 43 | + // Convert the encrypted data to a hexadecimal string |
| 44 | + //result.EncryptedData = BitConverter.ToString(data.EncryptedData); |
| 45 | + result.EncryptedData = Encoding.UTF8.GetString(data.EncryptedData); //Encoding.UTF8.GetString(decryptedData) |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + public static async Task<DecryptionResult> DecryptAsync(RsaDecryptionParameters model) |
| 50 | + { |
| 51 | + var result = new DecryptionResult(); |
| 52 | + |
| 53 | + if (string.IsNullOrWhiteSpace(model.DataToDecrypt)) |
| 54 | + { |
| 55 | + result.Errors.Add($"Data cannot be null {nameof(model.DataToDecrypt)}"); |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + if (string.IsNullOrWhiteSpace(model.PrivateKey)) |
| 60 | + { |
| 61 | + result.Errors.Add($"PrivateKey cannot be null {nameof(model.PrivateKey)}"); |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + // asynchronously perform RSA encryption |
| 66 | + var data = await RsaEncryption.DecryptAsync(model.DataToDecrypt, model.PrivateKey); |
| 67 | + |
| 68 | + if (data.Errors.Count > 0) |
| 69 | + { |
| 70 | + // if there are errors in the encryption process, add them to the result and return |
| 71 | + result.Errors.AddRange(data.Errors); |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + // convert the encrypted data to a hexadecimal string |
| 76 | + result.DecryptedData = Encoding.UTF8.GetString(data.DecryptedData); |
| 77 | + return result; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments