-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitalSignature.cs
41 lines (37 loc) · 1.38 KB
/
DigitalSignature.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
using System;
using System.IO;
using System.Security.Cryptography;
namespace Crypto
{
public class DigitalSignature
{
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[] SignData(byte[] hasghOfDataToSign){
using(var rsa=new RSACryptoServiceProvider(2048)){
rsa.PersistKeyInCsp=false;
rsa.ImportParameters(this._privateKey);
var rsaFormatter= new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA25");
return rsaFormatter.CreateSignature(hasghOfDataToSign);
}
}
public bool VerifySignature(byte[] hasOfDataToSign, byte[] signature){
using(var rsa= new RSACryptoServiceProvider(2048)){
rsa.ImportParameters(this._publicKey);
var rsaDeformater=new RSAPKCS1SignatureDeformatter(rsa);
rsaDeformater.SetHashAlgorithm("SHA256");
return rsaDeformater.VerifySignature(hasOfDataToSign,signature);
}
}
}
}