<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.1" />

RsaPrivateKeyStructure

using Org.BouncyCastle.Math; using System; namespace Org.BouncyCastle.Asn1.Pkcs { public class RsaPrivateKeyStructure : Asn1Encodable { private readonly BigInteger m_modulus; private readonly BigInteger m_publicExponent; private readonly BigInteger m_privateExponent; private readonly BigInteger m_prime1; private readonly BigInteger m_prime2; private readonly BigInteger m_exponent1; private readonly BigInteger m_exponent2; private readonly BigInteger m_coefficient; public BigInteger Modulus => m_modulus; public BigInteger PublicExponent => m_publicExponent; public BigInteger PrivateExponent => m_privateExponent; public BigInteger Prime1 => m_prime1; public BigInteger Prime2 => m_prime2; public BigInteger Exponent1 => m_exponent1; public BigInteger Exponent2 => m_exponent2; public BigInteger Coefficient => m_coefficient; public static RsaPrivateKeyStructure GetInstance(object obj) { if (obj == null) return null; RsaPrivateKeyStructure rsaPrivateKeyStructure = obj as RsaPrivateKeyStructure; if (rsaPrivateKeyStructure != null) return rsaPrivateKeyStructure; return new RsaPrivateKeyStructure(Asn1Sequence.GetInstance(obj)); } public static RsaPrivateKeyStructure GetInstance(Asn1TaggedObject obj, bool isExplicit) { return new RsaPrivateKeyStructure(Asn1Sequence.GetInstance(obj, isExplicit)); } public static RsaPrivateKeyStructure GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) { return new RsaPrivateKeyStructure(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); } public RsaPrivateKeyStructure(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger prime1, BigInteger prime2, BigInteger exponent1, BigInteger exponent2, BigInteger coefficient) { if (modulus == null) throw new ArgumentNullException("modulus"); m_modulus = modulus; if (publicExponent == null) throw new ArgumentNullException("publicExponent"); m_publicExponent = publicExponent; if (privateExponent == null) throw new ArgumentNullException("privateExponent"); m_privateExponent = privateExponent; if (prime1 == null) throw new ArgumentNullException("prime1"); m_prime1 = prime1; if (prime2 == null) throw new ArgumentNullException("prime2"); m_prime2 = prime2; if (exponent1 == null) throw new ArgumentNullException("exponent1"); m_exponent1 = exponent1; if (exponent2 == null) throw new ArgumentNullException("exponent2"); m_exponent2 = exponent2; if (coefficient == null) throw new ArgumentNullException("coefficient"); m_coefficient = coefficient; } private RsaPrivateKeyStructure(Asn1Sequence seq) { int count = seq.Count; if (count != 9) throw new ArgumentException("Bad sequence size: " + count.ToString(), "seq"); DerInteger instance = DerInteger.GetInstance(seq[0]); m_modulus = DerInteger.GetInstance(seq[1]).Value; m_publicExponent = DerInteger.GetInstance(seq[2]).Value; m_privateExponent = DerInteger.GetInstance(seq[3]).Value; m_prime1 = DerInteger.GetInstance(seq[4]).Value; m_prime2 = DerInteger.GetInstance(seq[5]).Value; m_exponent1 = DerInteger.GetInstance(seq[6]).Value; m_exponent2 = DerInteger.GetInstance(seq[7]).Value; m_coefficient = DerInteger.GetInstance(seq[8]).Value; if (!instance.HasValue(0)) throw new ArgumentException("wrong version for RSA private key"); } public override Asn1Object ToAsn1Object() { return new DerSequence(DerInteger.Zero, new DerInteger(m_modulus), new DerInteger(m_publicExponent), new DerInteger(m_privateExponent), new DerInteger(m_prime1), new DerInteger(m_prime2), new DerInteger(m_exponent1), new DerInteger(m_exponent2), new DerInteger(m_coefficient)); } } }