public static string ExportPrivateKeyPKCS8(RSA rsa)
        {
            var parameters = rsa.ExportParameters(true);

            return(ToPem("RSA PRIVATE KEY", Asn1Encoder.Sequence(
                             Asn1Encoder.IntegerBigEndian(new byte[] { 0x00 }),   // Version
                             Asn1Encoder.Sequence(                                // AlgorithmIdentifier
                                 Asn1Encoder.ObjectIdentifier(OID_rsaEncryption), // algorithm
                                 Asn1Encoder.Null()                               // parameters
                                 ),
                             Asn1Encoder.OctetString(
                                 Asn1Encoder.Sequence(
                                     Asn1Encoder.IntegerBigEndian(new byte[] { 0x00 }), // Version
                                     Asn1Encoder.IntegerBigEndian(parameters.Modulus),
                                     Asn1Encoder.IntegerBigEndian(parameters.Exponent),
                                     Asn1Encoder.IntegerBigEndian(parameters.D),
                                     Asn1Encoder.IntegerBigEndian(parameters.P),
                                     Asn1Encoder.IntegerBigEndian(parameters.Q),
                                     Asn1Encoder.IntegerBigEndian(parameters.DP),
                                     Asn1Encoder.IntegerBigEndian(parameters.DQ),
                                     Asn1Encoder.IntegerBigEndian(parameters.InverseQ)
                                     )
                                 )
                             )));
        }
        private static byte[] EncodePSD2QcStatmentExtension(PSD2Roles roles, PSD2CertificateType certType, byte retentionPeriod, string ncaName, string ncaId)
        {
            var rolesSeq = new List <byte[]>();

            if (roles.HasFlag(PSD2Roles.PSP_AS))
            {
                rolesSeq.Add(Asn1Encoder.Sequence(Asn1Encoder.ObjectIdentifier(OID_PSP_AS), Asn1Encoder.Utf8String(PSD2Roles.PSP_AS.ToString())));
            }
            if (roles.HasFlag(PSD2Roles.PSP_PI))
            {
                rolesSeq.Add(Asn1Encoder.Sequence(Asn1Encoder.ObjectIdentifier(OID_PSP_PI), Asn1Encoder.Utf8String(PSD2Roles.PSP_PI.ToString())));
            }
            if (roles.HasFlag(PSD2Roles.PSP_AI))
            {
                rolesSeq.Add(Asn1Encoder.Sequence(Asn1Encoder.ObjectIdentifier(OID_PSP_AI), Asn1Encoder.Utf8String(PSD2Roles.PSP_AI.ToString())));
            }
            if (roles.HasFlag(PSD2Roles.PSP_IC))
            {
                rolesSeq.Add(Asn1Encoder.Sequence(Asn1Encoder.ObjectIdentifier(OID_PSP_IC), Asn1Encoder.Utf8String(PSD2Roles.PSP_IC.ToString())));
            }

            return(Asn1Encoder.Sequence(
                       Asn1Encoder.Sequence(
                           Asn1Encoder.ObjectIdentifier(OID_QcsCompliance) //  this certificate is issued as a Qualified Certificate
                           ),
                       Asn1Encoder.Sequence(
                           Asn1Encoder.ObjectIdentifier(OID_QcRetentionPeriod), // number of years after the validity period the certificate will be stored in the issuer's archive
                           Asn1Encoder.IntegerBigEndian(new[] { retentionPeriod })
                           ),
                       Asn1Encoder.Sequence(
                           Asn1Encoder.ObjectIdentifier(OID_QcsQcSSCD) // CAs claiming to issue certificates where the private key related to the certified public key resides in a Secure Signature Creation Device(SSCD)
                           ),
                       Asn1Encoder.Sequence(
                           Asn1Encoder.ObjectIdentifier(OID_QcType),
                           Asn1Encoder.Sequence(
                               Asn1Encoder.ObjectIdentifier(certType == PSD2CertificateType.QWAC ? OID_QWAC_web : OID_QSealC_eseal)
                               )
                           ),
                       Asn1Encoder.Sequence(
                           Asn1Encoder.ObjectIdentifier(OID_PSD2qcStatement),
                           Asn1Encoder.Sequence(
                               Asn1Encoder.Sequence(rolesSeq.ToArray()),
                               Asn1Encoder.Utf8String(ncaName),
                               Asn1Encoder.Utf8String(ncaId)
                               )
                           )
                       ));
        }
        public static string ExportPublicKeyPKCS1(RSA rsa)
        {
            var parameters = rsa.ExportParameters(false);

            return(ToPem("PUBLIC KEY", Asn1Encoder.Sequence(
                             Asn1Encoder.Sequence(                                // AlgorithmIdentifier
                                 Asn1Encoder.ObjectIdentifier(OID_rsaEncryption), // algorithm
                                 Asn1Encoder.Null()                               // parameters
                                 ),
                             Asn1Encoder.BitString(0,
                                                   Asn1Encoder.Sequence(
                                                       Asn1Encoder.IntegerBigEndian(parameters.Modulus),
                                                       Asn1Encoder.IntegerBigEndian(parameters.Exponent)
                                                       )
                                                   )
                             )));
        }