-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathRsa.cs
More file actions
82 lines (73 loc) · 2.09 KB
/
Rsa.cs
File metadata and controls
82 lines (73 loc) · 2.09 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace Waher.Security.PKCS
{
/// <summary>
/// Base class for RSA algorithms
/// </summary>
public abstract class Rsa : SignatureAlgorithm
{
private readonly RSA rsa;
/// <summary>
/// Base class for RSA algorithms
/// </summary>
/// <param name="RSA">RSA cryptogaphic service provider.</param>
public Rsa(RSA RSA)
{
this.rsa = RSA;
}
/// <summary>
/// Object Identity for the PKI algorithm.
/// </summary>
public override string PkiAlgorithmOID => "1.2.840.113549.1.1.1";
/// <summary>
/// Name of hash algorithm to use for signatues.
/// </summary>
public abstract HashAlgorithmName HashAlgorithmName
{
get;
}
/// <summary>
/// Exports the public key using DER.
/// </summary>
/// <param name="Output">Encoded output.</param>
public override void ExportPublicKey(DerEncoder Output)
{
RSAParameters Parameters = this.rsa.ExportParameters(false);
Output.StartSEQUENCE();
Output.INTEGER(Parameters.Modulus, false);
Output.INTEGER(Parameters.Exponent, false);
Output.EndSEQUENCE();
}
/// <summary>
/// Exports the private key using DER.
/// </summary>
/// <param name="Output">Encoded output.</param>
public override void ExportPrivateKey(DerEncoder Output)
{
RSAParameters Parameters = this.rsa.ExportParameters(true);
Output.StartSEQUENCE();
Output.INTEGER(0); // Version
Output.INTEGER(Parameters.Modulus, false);
Output.INTEGER(Parameters.Exponent, false);
Output.INTEGER(Parameters.D, false);
Output.INTEGER(Parameters.P, false);
Output.INTEGER(Parameters.Q, false);
Output.INTEGER(Parameters.DP, false);
Output.INTEGER(Parameters.DQ, false);
Output.INTEGER(Parameters.InverseQ, false);
Output.EndSEQUENCE();
}
/// <summary>
/// Signs data.
/// </summary>
/// <param name="Data">Binary data to sign.</param>
/// <returns>Signature.</returns>
public override byte[] Sign(byte[] Data)
{
return this.rsa.SignData(Data, this.HashAlgorithmName, RSASignaturePadding.Pkcs1);
}
}
}