Skip to content

Commit 668f633

Browse files
committed
feat: move rsa key generation method to KeyGenerator class
1 parent bc1b546 commit 668f633

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,7 @@
77
namespace SafeCrypt.RsaEncryption
88
{
99
public class RsaEncryption
10-
{
11-
/// <summary>
12-
/// Generates RSA key pair.
13-
/// </summary>
14-
/// <param name="keySize">The size of the key pair (e.g., 1024, 2048 bits).</param>
15-
/// <returns>The generated RSA key pair.</returns>
16-
public static Tuple<string, string> GenerateRsaKeys(int keySize)
17-
{
18-
using (var rsa = new RSACryptoServiceProvider(keySize))
19-
{
20-
string publicKey = rsa.ToXmlString(false); // Don't include private key
21-
string privateKey = rsa.ToXmlString(true); // Include private key
22-
23-
return new Tuple<string, string>(publicKey, privateKey);
24-
}
25-
}
26-
10+
{
2711
/// <summary>
2812
/// Encrypts data using RSA public key.
2913
/// </summary>

src/SafeCrypt.Lib/Helpers/KeyGenerators.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,33 @@ public static string GenerateAesSecretKey(int keySize)
6767
return Convert.ToBase64String(aesAlg.Key);
6868
}
6969
}
70+
71+
/// <summary>
72+
/// Generates a pair of RSA public and private keys with the specified key size.
73+
/// </summary>
74+
/// <param name="keySize">The size of the key pair (e.g., 1024, 2048 bits).</param>
75+
/// <returns>
76+
/// A <see cref="Tuple{T1, T2}"/> containing the generated RSA public and private keys.
77+
/// Item1 represents the public key, and Item2 represents the private key.
78+
/// </returns>
79+
/// <remarks>
80+
/// The generated keys are in XML format. The public key does not include the private key,
81+
/// while the private key includes both public and private components.
82+
/// </remarks>
83+
/// <param name="keySize">The size of the key pair (e.g., 1024, 2048 bits).</param>
84+
/// <returns>A tuple containing the generated RSA public and private keys.</returns>
85+
/// <exception cref="CryptographicException">
86+
/// Thrown if an error occurs during key generation.
87+
/// </exception>
88+
public static Tuple<string, string> GenerateRsaKeys(int keySize)
89+
{
90+
using (var rsa = new RSACryptoServiceProvider(keySize))
91+
{
92+
string publicKey = rsa.ToXmlString(false); // Don't include private key
93+
string privateKey = rsa.ToXmlString(true); // Include private key
94+
95+
return new Tuple<string, string>(publicKey, privateKey);
96+
}
97+
}
7098
}
7199
}

0 commit comments

Comments
 (0)