RsaSecretBcpgKey
Base class for an RSA secret (or private) key.
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
using System;
namespace Org.BouncyCastle.Bcpg
{
public class RsaSecretBcpgKey : BcpgObject, IBcpgKey
{
private readonly MPInteger m_d;
private readonly MPInteger m_p;
private readonly MPInteger m_q;
private readonly MPInteger m_u;
private readonly BigInteger m_expP;
private readonly BigInteger m_expQ;
private readonly BigInteger m_crt;
public BigInteger Modulus => m_p.Value.Multiply(m_q.Value);
public BigInteger PrivateExponent => m_d.Value;
public BigInteger PrimeP => m_p.Value;
public BigInteger PrimeQ => m_q.Value;
public BigInteger PrimeExponentP => m_expP;
public BigInteger PrimeExponentQ => m_expQ;
public BigInteger CrtCoefficient => m_crt;
public string Format => "PGP";
public RsaSecretBcpgKey(BcpgInputStream bcpgIn)
{
m_d = new MPInteger(bcpgIn);
m_p = new MPInteger(bcpgIn);
m_q = new MPInteger(bcpgIn);
m_u = new MPInteger(bcpgIn);
m_expP = m_d.Value.Remainder(m_p.Value.Subtract(BigInteger.One));
m_expQ = m_d.Value.Remainder(m_q.Value.Subtract(BigInteger.One));
m_crt = BigIntegers.ModOddInverse(m_p.Value, m_q.Value);
}
public RsaSecretBcpgKey(BigInteger d, BigInteger p, BigInteger q)
{
int num = p.CompareTo(q);
if (num >= 0) {
if (num == 0)
throw new ArgumentException("p and q cannot be equal");
BigInteger bigInteger = p;
p = q;
q = bigInteger;
}
m_d = new MPInteger(d);
m_p = new MPInteger(p);
m_q = new MPInteger(q);
m_u = new MPInteger(BigIntegers.ModOddInverse(q, p));
m_expP = d.Remainder(p.Subtract(BigInteger.One));
m_expQ = d.Remainder(q.Subtract(BigInteger.One));
m_crt = BigIntegers.ModOddInverse(p, q);
}
public override byte[] GetEncoded()
{
return BcpgOutputStream.GetEncodedOrNull(this);
}
public override void Encode(BcpgOutputStream bcpgOut)
{
bcpgOut.WriteObjects(m_d, m_p, m_q, m_u);
}
}
}