public EncryptedPrivateKey(byte[] data) { if (data == null) { throw new ArgumentNullException("data"); } var obj = Asn1Object.FromByteArray(data.Skip(16).ToArray()); if (obj is Asn1Sequence seq && seq.Count > 0) { if (seq[0] is Asn1Sequence seq2 && seq2.Count >= 2) { UKM = (seq2[0] as Asn1OctetString)?.GetOctets(); if (seq2[1] is Asn1Sequence seq3 && seq3.Count >= 2) { CEK = (seq3[0] as Asn1OctetString)?.GetOctets(); MAC = (seq3[1] as Asn1OctetString)?.GetOctets(); } foreach (var tag in seq2.OfType <Asn1TaggedObject>()) { if (tag.TagNo == 0) { KeyParameters = KeyParameters.GetInstance(tag.GetObject()); } } } } if (UKM == null || CEK == null || MAC == null || KeyParameters == null) { throw new CryptographicException("Ошибка в данных PRIVATEKEYBLOB."); } }
public HeaderStructure(Asn1Sequence seq) { if (seq?.Count > 0 && seq[0] is Asn1Sequence seq2) { foreach (Asn1TaggedObject tag in seq2.OfType <Asn1TaggedObject>()) { switch (tag.TagNo) { case 5: byte[] cert = (tag.GetObject() as Asn1OctetString)?.GetOctets(); Certificate = X509CertificateStructure.GetInstance(cert); break; case 6: cert = (tag.GetObject() as Asn1OctetString)?.GetOctets(); Certificate2 = X509CertificateStructure.GetInstance(cert); break; case 10: PublicX = Asn1OctetString.GetInstance(tag.GetObject())?.GetOctets(); break; } } Asn1Sequence seq3 = seq2?.OfType <Asn1Sequence>().FirstOrDefault(); PrivateKeyParameters = KeyParameters.GetInstance(seq3); Attributes = seq2?.OfType <DerBitString>().FirstOrDefault(); } if (seq?.Count > 1) { HMACKey = (seq[1] as Asn1OctetString)?.GetOctets(); } if (HMACKey == null || Attributes == null || PrivateKeyParameters == null || PublicX == null) { throw new CryptographicException("Ошибка в данных header.key."); } }
private static void PrintPrivateKey(BigInteger pkey, KeyParameters keyParams) { var gostParams = Gost3410PublicKeyAlgParameters.GetInstance(keyParams.Algorithm.Parameters); var pkeyEnc = new DerSequence( new DerInteger(0), new DerSequence( keyParams.Algorithm.Algorithm, new DerSequence( gostParams.PublicKeyParamSet, gostParams.DigestParamSet ) ), new DerOctetString(new DerInteger(pkey)) ); var pemObject = new PemObject("PRIVATE KEY", pkeyEnc.GetDerEncoded()); using (var sw = new StreamWriter(Console.OpenStandardOutput())) { var writer = new PemWriter(sw); writer.WriteObject(pemObject); } }