-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
44 lines (34 loc) · 1.42 KB
/
Program.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.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;
namespace RSATest;
public class RSADotnetExample
{
public static string GetPublicKeyXML(RSAParameters publicKey)
{
var writer = new StringWriter();
var xmlS = new XmlSerializer(typeof(RSAParameters));
xmlS.Serialize(writer, publicKey);
return writer.ToString();
}
public static void Main()
{
var data = "DATA TO SIGN!";
var rsa = RSA.Create();
var privateKey = rsa.ExportParameters(true);
var publicKey = rsa.ExportParameters(false);
var dataBytes = Encoding.Unicode.GetBytes(data);
rsa.ImportParameters(privateKey);
var signature = rsa.SignData(dataBytes, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
var signatureString = Convert.ToBase64String(signature);
Console.WriteLine($"signature: \n {signatureString} \n");
var publicKeyXMLString = RSADotnetExample.GetPublicKeyXML(publicKey);
var dataBytesVerify = Encoding.Unicode.GetBytes(data);
var signatureToBytes = Convert.FromBase64String(signatureString);
// rsa.FromXmlString(publicKeyXMLString);
// rsa.ImportParameters(publicKey);
var valid = rsa.VerifyData(dataBytesVerify, signatureToBytes, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
Console.WriteLine($"Valid: \n {valid} \n");
}
}