-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSAWithRSAParameterKey.cs
44 lines (41 loc) · 1.26 KB
/
RSAWithRSAParameterKey.cs
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
using System;
using System.Security.Cryptography;
namespace Crypto
{
public class RSAWithRSAParameterKey
{
private RSAParameters _publicKey;
private RSAParameters _privateKey;
public void AssignKey()
{
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.PersistKeyInCsp = false;
this._publicKey = rsa.ExportParameters(false);
this._privateKey = rsa.ExportParameters(true);
}
}
public byte[] EncryptData(byte[] dataToEncrypt)
{
byte[] cipherBytes;
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.PersistKeyInCsp = false;
rsa.ImportParameters(_publicKey);
cipherBytes = rsa.Encrypt(dataToEncrypt, true);
}
return cipherBytes;
}
public byte[] DecryptData(byte[] dataToDecrypt)
{
byte[] plain;
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.PersistKeyInCsp = false;
rsa.ImportParameters(_privateKey);
plain = rsa.Decrypt(dataToDecrypt, true);
}
return plain;
}
}
}