-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAsymmetricEncryption.cs
More file actions
113 lines (105 loc) · 4.85 KB
/
Copy pathAsymmetricEncryption.cs
File metadata and controls
113 lines (105 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Security.Cryptography;
namespace BytecodeApi.Cryptography;
/// <summary>
/// Class to encrypt and decrypt data using RSA. The length of the encrypted data is limited. For larger amounts of data, the <see cref="AsymmetricContentEncryption" /> class is more suitable.
/// </summary>
public static class AsymmetricEncryption
{
/// <summary>
/// Generates a new public and private key pair.
/// </summary>
/// <param name="publicKey">When this method returns, an <see cref="RSAParameters" /> structure with the public key information.</param>
/// <param name="privateKey">When this method returns, an <see cref="RSAParameters" /> structure with the private key information.</param>
public static void GenerateKeyPair(out RSAParameters publicKey, out RSAParameters privateKey)
{
using RSACryptoServiceProvider rsa = new();
publicKey = rsa.ExportParameters(false);
privateKey = rsa.ExportParameters(true);
}
/// <summary>
/// Encrypts the specified <see cref="byte" />[] using RSA and the specified public key.
/// </summary>
/// <param name="data">A <see cref="byte" />[] with the data to be encrypted.</param>
/// <param name="key">An <see cref="RSAParameters" /> value containing the public key information.</param>
/// <returns>
/// A new <see cref="byte" />[] representing the encrypted version of <paramref name="data" />.
/// </returns>
public static byte[] Encrypt(byte[] data, RSAParameters key)
{
Check.ArgumentNull(data);
using RSACryptoServiceProvider rsa = new();
rsa.ImportParameters(key);
return rsa.Encrypt(data, true);
}
/// <summary>
/// Encrypts the specified <see cref="byte" />[] using RSA and the specified public key.
/// </summary>
/// <param name="data">A <see cref="byte" />[] with the data to be encrypted.</param>
/// <param name="derKey">A <see cref="byte" />[] containing the public key in its DER representation.</param>
/// <returns>
/// A new <see cref="byte" />[] representing the encrypted version of <paramref name="data" />.
/// </returns>
public static byte[] Encrypt(byte[] data, byte[] derKey)
{
Check.ArgumentNull(data);
Check.ArgumentNull(derKey);
return Encrypt(data, AsymmetricKeyConvert.ToKey(derKey));
}
/// <summary>
/// Encrypts the specified <see cref="byte" />[] using RSA and the specified public key.
/// </summary>
/// <param name="data">A <see cref="byte" />[] with the data to be encrypted.</param>
/// <param name="pemKey">A <see cref="string" /> value with the public key in its PEM representation, starting with "-----BEGIN PUBLIC KEY-----".</param>
/// <returns>
/// A new <see cref="byte" />[] representing the encrypted version of <paramref name="data" />.
/// </returns>
public static byte[] Encrypt(byte[] data, string pemKey)
{
Check.ArgumentNull(data);
Check.ArgumentNull(pemKey);
return Encrypt(data, AsymmetricKeyConvert.ToKey(pemKey));
}
/// <summary>
/// Decrypts the specified <see cref="byte" />[] using RSA and the specified private key.
/// </summary>
/// <param name="data">A <see cref="byte" />[] with the data to be decrypted.</param>
/// <param name="key">An <see cref="RSAParameters" /> value containing the private key information.</param>
/// <returns>
/// A new <see cref="byte" />[] representing the decrypted version of <paramref name="data" />.
/// </returns>
public static byte[] Decrypt(byte[] data, RSAParameters key)
{
Check.ArgumentNull(data);
using RSACryptoServiceProvider rsa = new();
rsa.ImportParameters(key);
return rsa.Decrypt(data, true);
}
/// <summary>
/// Decrypts the specified <see cref="byte" />[] using RSA and the specified private key.
/// </summary>
/// <param name="data">A <see cref="byte" />[] with the data to be decrypted.</param>
/// <param name="derKey">A <see cref="byte" />[] containing the private key in its DER representation.</param>
/// <returns>
/// A new <see cref="byte" />[] representing the decrypted version of <paramref name="data" />.
/// </returns>
public static byte[] Decrypt(byte[] data, byte[] derKey)
{
Check.ArgumentNull(data);
Check.ArgumentNull(derKey);
return Decrypt(data, AsymmetricKeyConvert.ToKey(derKey));
}
/// <summary>
/// Decrypts the specified <see cref="byte" />[] using RSA and the specified private key.
/// </summary>
/// <param name="data">A <see cref="byte" />[] with the data to be decrypted.</param>
/// <param name="pemKey">A <see cref="string" /> value with the private key in its PEM representation, starting with "-----BEGIN RSA PRIVATE KEY-----".</param>
/// <returns>
/// A new <see cref="byte" />[] representing the decrypted version of <paramref name="data" />.
/// </returns>
public static byte[] Decrypt(byte[] data, string pemKey)
{
Check.ArgumentNull(data);
Check.ArgumentNull(pemKey);
return Decrypt(data, AsymmetricKeyConvert.ToKey(pemKey));
}
}