Skip to content

Commit c1e911e

Browse files
committed
feat: update rsa algorithm comments
1 parent 3f04900 commit c1e911e

1 file changed

Lines changed: 41 additions & 14 deletions

File tree

src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@
66

77
namespace SafeCrypt.RsaEncryption
88
{
9-
public class RsaEncryption
10-
{
9+
public static class RsaEncryption
10+
{
1111
/// <summary>
12-
/// Encrypts data using RSA public key.
12+
/// Asynchronously encrypts the provided data using the RSA (Rivest–Shamir–Adleman) algorithm.
1313
/// </summary>
1414
/// <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)
1831
{
1932
var result = new RsaEncryptionResult();
2033

@@ -46,17 +59,31 @@ public static async Task<RsaEncryptionResult> EncryptAsync(string data, string p
4659
/// <param name="encryptedData">The encrypted data.</param>
4760
/// <param name="privateKey">The RSA private key.</param>
4861
/// <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)
5063
{
51-
return await Task.Run(() =>
64+
var result = new RsaDecryptionResult();
65+
66+
try
5267
{
53-
using (var rsa = new RSACryptoServiceProvider())
68+
var decryptedData = await Task.Run(() =>
5469
{
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;
6087
}
6188
}
6289
}

0 commit comments

Comments
 (0)