|
6 | 6 |
|
7 | 7 | namespace SafeCrypt.RsaEncryption |
8 | 8 | { |
9 | | - public class RsaEncryption |
10 | | - { |
| 9 | + public static class RsaEncryption |
| 10 | + { |
11 | 11 | /// <summary> |
12 | | - /// Encrypts data using RSA public key. |
| 12 | + /// Asynchronously encrypts the provided data using the RSA (Rivest–Shamir–Adleman) algorithm. |
13 | 13 | /// </summary> |
14 | 14 | /// <param name="data">The data to be encrypted.</param> |
15 | | - /// <param name="publicKey">The RSA public key.</param> |
16 | | - /// <returns>The encrypted data.</returns> |
17 | | - public static async Task<RsaEncryptionResult> EncryptAsync(string data, string publicKey) |
| 15 | + /// <param name="publicKey">The public key used for encryption.</param> |
| 16 | + /// <returns> |
| 17 | + /// A task representing the asynchronous operation that, upon completion, |
| 18 | + /// returns an <see cref="RsaEncryptionResult"/> containing the encrypted data. |
| 19 | + /// </returns> |
| 20 | + /// <remarks> |
| 21 | + /// This method uses the RSA algorithm to encrypt the input data with the provided public key. |
| 22 | + /// The encryption is performed asynchronously using <see cref="Task.Run"/>. |
| 23 | + /// </remarks> |
| 24 | + /// <param name="data">The data to be encrypted.</param> |
| 25 | + /// <param name="publicKey">The public key used for encryption.</param> |
| 26 | + /// <returns> |
| 27 | + /// A task representing the asynchronous operation that, upon completion, |
| 28 | + /// returns an <see cref="RsaEncryptionResult"/> containing the encrypted data. |
| 29 | + /// </returns> |
| 30 | + internal static async Task<RsaEncryptionResult> EncryptAsync(string data, string publicKey) |
18 | 31 | { |
19 | 32 | var result = new RsaEncryptionResult(); |
20 | 33 |
|
@@ -46,17 +59,31 @@ public static async Task<RsaEncryptionResult> EncryptAsync(string data, string p |
46 | 59 | /// <param name="encryptedData">The encrypted data.</param> |
47 | 60 | /// <param name="privateKey">The RSA private key.</param> |
48 | 61 | /// <returns>The decrypted data.</returns> |
49 | | - public static async Task<string> DecryptAsync(byte[] encryptedData, string privateKey) |
| 62 | + internal static async Task<RsaDecryptionResult> DecryptAsync(byte[] encryptedData, string privateKey) |
50 | 63 | { |
51 | | - return await Task.Run(() => |
| 64 | + var result = new RsaDecryptionResult(); |
| 65 | + |
| 66 | + try |
52 | 67 | { |
53 | | - using (var rsa = new RSACryptoServiceProvider()) |
| 68 | + var decryptedData = await Task.Run(() => |
54 | 69 | { |
55 | | - rsa.FromXmlString(privateKey); |
56 | | - byte[] decryptedData = rsa.Decrypt(encryptedData, false); |
57 | | - return Encoding.UTF8.GetString(decryptedData); |
58 | | - } |
59 | | - }); |
| 70 | + using (var rsa = new RSACryptoServiceProvider()) |
| 71 | + { |
| 72 | + rsa.FromXmlString(privateKey); |
| 73 | + byte[] dataBytes = encryptedData; |
| 74 | + return rsa.Decrypt(encryptedData, false); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + result.DecryptedData = decryptedData; |
| 79 | + |
| 80 | + } |
| 81 | + catch (Exception ex) |
| 82 | + { |
| 83 | + result.Errors.Add(ex.Message); |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
60 | 87 | } |
61 | 88 | } |
62 | 89 | } |
0 commit comments