ุงู„ุชุญู‚ู‚ ู…ู† ุตุญุฉ ุงู„ุชูˆู‚ูŠุนุงุช ุงู„ุฅู„ูƒุชุฑูˆู†ูŠุฉ ููŠ C # ุจุงุณุชุฎุฏุงู… Crypto PRO

ุงุณุชู…ุฑุงุฑ ุงู„ุญูˆุงุฑ ุญูˆู„ ู…ูˆุถูˆุน ุงู„ุชูˆู‚ูŠุนุงุช ุงู„ุฅู„ูƒุชุฑูˆู†ูŠุฉ (ูŠุดุงุฑ ุฅู„ูŠู‡ ููŠู…ุง ุจุนุฏ ุจุงู„ุชูˆู‚ูŠุน ุงู„ุฅู„ูƒุชุฑูˆู†ูŠ) ุŒ ูŠู†ุจุบูŠ ุฃู† ูŠู‚ุงู„ ุนู† ุงู„ุชุญู‚ู‚. ููŠ ุงู„ู…ู‚ุงู„ุฉ ุงู„ุณุงุจู‚ุฉ ุŒ ู‚ู…ุช ุจุชุญู„ูŠู„ ุงู„ุฌุฒุก ุงู„ุฃูƒุซุฑ ุตุนูˆุจุฉ ู…ู† ุงู„ู…ู‡ู…ุฉ - ุฅู†ุดุงุก ุชูˆู‚ูŠุน. ู‡ุฐู‡ ุงู„ู…ู‚ุงู„ุฉ ุฃุจุณุท ู‚ู„ูŠู„ุงู‹. ู…ุนุธู… ุงู„ูƒูˆุฏ ู‡ูˆ ุชูƒูŠู ุฃู…ุซู„ุฉ ู…ู† Crypto PRO .NET SDK. ุณูˆู ู†ุชุญู‚ู‚ ุฃูˆู„ุงู‹ ู…ู† ุฌู…ูŠุน ุงู„ุชูˆู‚ูŠุนุงุช ูˆูู‚ู‹ุง ู„ู€ GOST R 34.10-2001 ูˆ GOST R 34.10-2012 ุŒ ู„ุฐู„ูƒ ู†ุญุชุงุฌ ุฅู„ู‰ CRYPTO PRO.

ุชู†ู‚ุณู… ุงู„ู…ู‡ู…ุฉ ุจุงู„ู†ุณุจุฉ ู„ู†ุง ุฅู„ู‰ 3 ุฃุฌุฒุงุก: ุชูˆู‚ูŠุน ู…ู†ูุตู„ ุŒ ุชูˆู‚ูŠุน ููŠ PDF ูˆุชูˆู‚ูŠุน ููŠ MS Word.

ุงู„ุชุญู‚ู‚ ู…ู† ุงู„ุชูˆู‚ูŠุน ุงู„ู…ู†ูุตู„:

//dataFileRawBytes -     ContentInfo contentInfo = new ContentInfo(dataFileRawBytes); SignedCms signedCms = new SignedCms(contentInfo, true); //signatureFileRawBytes -    signedCms.Decode(signatureFileRawBytes); if (signedCms.SignerInfos.Count == 0) { //     } foreach (SignerInfo signerInfo in signedCms.SignerInfos) { //   DateTime? signDate = (signerInfo.SignedAttributes .Cast<CryptographicAttributeObject>() .FirstOrDefault(x => x.Oid.Value == "1.2.840.113549.1.9.5") ?.Values[0] as Pkcs9SigningTime)?.SigningTime; bool valid; try { signerInfo.CheckSignature(true); valid = true; } catch (CryptographicException exc) { valid = false; } //   .     X509Certificate2 certificate = signerInfo.Certificate; 

ุงู„ุชุนู„ูŠู‚ุงุช ูƒู„ู‡ุง ููŠ ุงู„ุฑู…ุฒ ุŒ ูˆุณุฃู„ูุช ุงู†ุชุจุงู‡ูƒ ูู‚ุท ุฅู„ู‰ ุงู„ุญุตูˆู„ ุนู„ู‰ ุดู‡ุงุฏุฉ ุŒ ูˆุณู†ุญุชุงุฌู‡ุง ู„ุงุญู‚ู‹ุง ุŒ ู„ุฃู† ุณูˆู ู†ุชุญู‚ู‚ ู…ู† ุงู„ุดู‡ุงุฏุฉ ุจุดูƒู„ ู…ู†ูุตู„.

ุญุณู†ู‹ุง ุŒ ู„ุง ุชู†ุณ ุฃู† ุชู„ุชู ูƒู„ ุดูŠุก ููŠ ุชุฌุฑุจุฉ ุงุณุชุฎุฏุงู… ูˆุงุณุชุฎุฏุงู… ุงู„ุขุฎุฑูŠู†. ููŠ ุงู„ู…ุซุงู„ ุŒ ุฃู†ุง ู„ุง ุฃู‚ูˆู… ุจุฐู„ูƒ ุนู† ู‚ุตุฏ ู„ุชู‚ู„ูŠู„ ุงู„ุญุฌู…

ุงู„ุชุญู‚ู‚ ู…ู† ุตุญุฉ ุงู„ุชูˆู‚ูŠุน ููŠ PDF. ู‡ู†ุง ู†ุญุชุงุฌ ุฅู„ู‰ iTextSharp (ุงู„ุฅุตุฏุงุฑ ุงู„ุญุงู„ูŠ ูˆู‚ุช ูƒุชุงุจุฉ 5.5.13):

  using (MemoryStream fileStream = new MemoryStream(dataFileRawBytes)) using (PdfReader pdfReader = new PdfReader(fileStream)) { AcroFields acroFields = pdfReader.AcroFields; //    List<string> signatureNames = acroFields.GetSignatureNames(); if (!signatureNames.Any()) { //   } foreach (string signatureName in signatureNames) { //       PdfDictionary singleSignature = acroFields.GetSignatureDictionary(signatureName); PdfString asString1 = singleSignature.GetAsString(PdfName.CONTENTS); byte[] signatureBytes = asString1.GetOriginalBytes(); RandomAccessFileOrArray safeFile = pdfReader.SafeFile; PdfArray asArray = singleSignature.GetAsArray(PdfName.BYTERANGE); using ( Stream stream = new RASInputStream( new RandomAccessSourceFactory().CreateRanged( safeFile.CreateSourceView(), asArray.AsLongArray()))) { using (MemoryStream ms = new MemoryStream((int)stream.Length)) { stream.CopyTo(ms); byte[] data = ms.GetBuffer(); ContentInfo contentInfo = new ContentInfo(data); SignedCms signedCms = new SignedCms(contentInfo, true); signedCms.Decode(signatureBytes); bool checkResult; //    ,    try { signedCms.CheckSignature(true); checkResult = true; } catch (Exception) { checkResult = false; } foreach (SignerInfo signerInfo in signedCms.SignerInfos) { //   DateTime? signDate = (signerInfo.SignedAttributes .Cast<CryptographicAttributeObject>() .FirstOrDefault(x => x.Oid.Value == "1.2.840.113549.1.9.5") ?.Values[0] as Pkcs9SigningTime)?.SigningTime; //  X509Certificate2 certificate = signerInfo.Certificate; } } } } } 

ู…ุฑุฉ ุฃุฎุฑู‰ ุŒ ู„ุง ูŠูˆุฌุฏ ุดูŠุก ุฎุงุต ู„ู„ุชุนู„ูŠู‚ ุนู„ูŠู‡. ู…ุง ู„ู… ูŠูƒู† ุนู„ูŠ ุฃู† ุฃู‚ูˆู„ ุนู† Oid ุŒ "1.2.840.113549.1.9.5" ู‡ูˆ Oid ู„ุชุงุฑูŠุฎ ุงู„ุชูˆู‚ูŠุน.

ูˆุงู„ุฃุฎูŠุฑ ููŠ ู‚ุงุฆู…ุชู†ุง ู‡ูˆ docx ุŒ ูˆุฑุจู…ุง ูŠูƒูˆู† ุงู„ุฎูŠุงุฑ ุงู„ุฃุณู‡ู„:

  using (MemoryStream fileStream = new MemoryStream(dataFileRawBytes)) using (Package filePackage = Package.Open(fileStream)) { PackageDigitalSignatureManager digitalSignatureManager = new PackageDigitalSignatureManager(filePackage); if (!digitalSignatureManager.IsSigned) { //    } foreach (PackageDigitalSignature signature in digitalSignatureManager.Signatures) { DateTime? signDate = signature.SigningTime; bool checkResult = signature.Verify() == VerifyResult.Success; //      X509Certificate2 certificate = new X509Certificate2(signature.Signer); } } 

ุงู„ุขู† ุณู†ู‚ูˆู… ุจุชุญู„ูŠู„ ุงู„ุดู‡ุงุฏุฉ ูˆุงู„ุชุญู‚ู‚ ู…ู† ุณู„ุณู„ุฉ ุงู„ุดู‡ุงุฏุงุช ุจุฃูƒู…ู„ู‡ุง. ู„ุฐู„ูƒ ุŒ ูŠุฌุจ ุฃู† ุชุนู…ู„ ุงู„ุชุฌู…ูŠุน ู…ู† ุชุญุช ู…ุณุชุฎุฏู… ู„ุฏูŠู‡ ุญู‚ ุงู„ูˆุตูˆู„ ุฅู„ู‰ ุงู„ุดุจูƒุฉ.

ุซู… ูŠุจุฏุฃ ุงู„ุฌุญูŠู… ุŒ ู„ุฃู†ู‡ ู„ุง ุฃุนุฑู ูƒูŠููŠุฉ ุงู„ุญุตูˆู„ ุนู„ู‰ ู…ุนู„ูˆู…ุงุช ุญูˆู„ ู…ุงู„ูƒ ุงู„ุดู‡ุงุฏุฉ ู…ู† ุฎู„ุงู„ Oid ุŒ ู„ุฐุง ุณุฃู‚ูˆู… ุจุชุญู„ูŠู„ ุงู„ุณู„ุณู„ุฉ. ุชุถุญูƒ ุจุตูˆุช ุฃุนู„ู‰: ูŠุจุฏุฃ ุงู„ุณูŠุฑูƒ.

ูˆู„ูƒู† ุจุฌุฏูŠุฉ ุŒ ู†ุฑุญุจ ุจูƒ ู„ู„ุชุนู„ูŠู‚ ุนู„ู‰ ุฃูˆู„ุฆูƒ ุงู„ุฐูŠู† ูŠุนุฑููˆู† ูƒูŠููŠุฉ ุงู„ู‚ูŠุงู… ุจุฐู„ูƒ ู…ู† ุฎู„ุงู„ Oid's:

  private static void FillElectronicSignature(X509Certificate2 certificate) { foreach (KeyValuePair<string, string> item in ParseCertificatesSubject(certificate.Subject)) { switch (item.Key) { case "C": string certificatesCountryName = item.Value; break; case "S": string certificatesState = item.Value; break; case "L": string certificatesLocality = item.Value; break; case "O": string certificatesOrganizationName = item.Value; break; case "OU": string certificatesOrganizationalUnitName = item.Value; break; case "CN": string certificatesCommonName = item.Value; break; case "E": string certificatesEmail = item.Value; break; case "STREET": string certificatesStreet = item.Value; break; //  ,  Window ,     ,   ,  INN //       deploy    // ,   -  case "": case "INN": case "1.2.643.3.131.1.1": string certificatesInn = item.Value; break; //  case "": case "OGRN": case "1.2.643.100.1": string certificatesOgrn = item.Value; break; //  case "": case "SNILS": case "1.2.643.100.3": string certificatesSnils = item.Value; break; case "SN": string certificatesOwnerLastName = item.Value; break; case "G": string certificatesOwnerFirstName = item.Value; break; //    default           } } DateTime certificateNotBefore = certificate.NotBefore; DateTime certificateNotAfter = certificate.NotAfter; string certificatesSerialNumber = certificate.SerialNumber; if (!certificate.Verify()) { //   using (X509Chain x509Chain = new X509Chain()) { x509Chain.Build(certificate); //    X509ChainStatus[] statuses = x509Chain.ChainStatus; //      int,    int certificatesErrorCode = statuses.Aggregate(X509ChainStatusFlags.NoError, (acc, chainStatus) => acc | chainStatus.Status, result => (int)result); } } } /// <summary> ///        /// </summary> private static Dictionary<string, string> ParseCertificatesSubject(string subject) { Dictionary<string, string> result = new Dictionary<string, string>(); //  ,     int quotationMarksCount = 0; //    "  " bool isKey = true; //    string key = string.Empty; //    string value = string.Empty; for (int i = 0; i < subject.Length; i++) { char c = subject[i]; if (isKey && c == '=') { isKey = false; continue; } if (isKey) key += c; else { if (c == '"') quotationMarksCount++; bool isItemEnd = (c == ',' && subject.Length >= i + 1 && subject[i + 1] == ' '); bool isLastChar = subject.Length == i + 1; if ((isItemEnd && quotationMarksCount % 2 == 0) || isLastChar) { if (isItemEnd) i++; if (isLastChar) value += c; isKey = true; if (value.StartsWith("\"") && value.EndsWith("\"")) value = value.Substring(1, value.Length - 2); value = value.Replace("\"\"", "\""); result.Add(key, value); key = string.Empty; value = string.Empty; quotationMarksCount = 0; continue; } value += c; } } return result; } 

ุงู„ุฑู…ุฒ ู‚ุตูŠุฑ ู‚ุฏุฑ ุงู„ุฅู…ูƒุงู† ุŒ ู„ูู‡ู… ุงู„ุฌูˆู‡ุฑ ุจุดูƒู„ ุฃูุถู„.

ุจุดูƒู„ ุนุงู… ุŒ ู‡ุฐุง ูƒู„ ุดูŠุก ุŒ ุฃู†ุง ุจุงู†ุชุธุงุฑ ุชุนู„ูŠู‚ุงุช ุญูˆู„ ุงู„ุญุตูˆู„ ุนู„ู‰ Oid ู…ู† ุงู„ุดู‡ุงุฏุฉ ูˆุฃูŠ ู†ู‚ุฏ ู…ู†ุทู‚ูŠ.

Source: https://habr.com/ru/post/ar426645/


All Articles