private byte[] CoSignBuffer(byte[] data, byte[] signdata) { X509Certificate2 m_cert = cbCerts.SelectedItem as X509Certificate2; if (m_cert == null) { MessageBox.Show("не найден сертификат!"); return(null); } SignedData signedData = new SignedDataClass(); Utilities utilities = new UtilitiesClass(); byte[] array = data; Signer signer = new SignerClass(); IStore store = new StoreClass(); bool flag2 = false; store.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "My", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY); foreach (ICertificate certificate in store.Certificates) { if (certificate.Thumbprint == m_cert.Thumbprint) { signer.Certificate = certificate; flag2 = true; break; } } if (!flag2) { throw new Exception("Не удалось найти сертификат подписи!"); } CapiComRCW.Attribute attribute = new AttributeClass(); attribute.Name = CAPICOM_ATTRIBUTE.CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME; attribute.Value = DateTime.Now.ToUniversalTime(); signer.AuthenticatedAttributes.Add(attribute); byte[] array3; byte[] array2 = signdata; ((CapiComRCW.ISignedData)signedData).set_Content(utilities.ByteArrayToBinaryString(array)); signedData.Verify(Convert.ToBase64String(array2), true, CAPICOM_SIGNED_DATA_VERIFY_FLAG.CAPICOM_VERIFY_SIGNATURE_ONLY); Store store2 = new StoreClass(); store2.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "AddressBook", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_WRITE); for (int i = 1; i <= signedData.Signers.Count; i++) { Signer signer2 = (Signer)signedData.Signers[i]; Certificate pVal = (Certificate)signer2.Certificate; store2.Add(pVal); } store2.Close(); string s = signedData.CoSign(signer, CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64); array3 = Convert.FromBase64String(s); return(array3); }
private void SignFile(string sFileIn) { if (Path.GetExtension(sFileIn).ToUpper().Equals(".ZIP")) { SignZip(sFileIn); if (!cbExtSignZIP.Checked) { return; } } X509Certificate2 m_cert = cbCerts.SelectedItem as X509Certificate2; if (m_cert == null) { throw new ApplicationException("Сформировать ЭЦП невозможно: не найден действительный сертификат отправителя!"); } SignedData signedData = new SignedDataClass(); Utilities utilities = new UtilitiesClass(); byte[] array; using (FileStream fileStream = new FileStream(sFileIn, FileMode.Open, FileAccess.Read)) { array = new byte[fileStream.Length]; fileStream.Position = 0L; fileStream.Read(array, 0, (int)fileStream.Length); fileStream.Close(); } byte[] array2 = null; bool flag = false; if (File.Exists(sFileIn + ".sig")) { flag = true; using (FileStream fileStream = new FileStream(sFileIn + ".sig", FileMode.Open, FileAccess.Read)) { array2 = new byte[fileStream.Length]; fileStream.Position = 0L; fileStream.Read(array2, 0, (int)fileStream.Length); fileStream.Close(); } } Signer signer = new SignerClass(); IStore store = new StoreClass(); bool flag2 = false; store.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "My", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY); foreach (ICertificate certificate in store.Certificates) { if (certificate.Thumbprint == m_cert.Thumbprint) { signer.Certificate = certificate; flag2 = true; break; } } if (!flag2) { throw new Exception("Не удалось найти сертификат подписи!"); } CapiComRCW.Attribute attribute = new AttributeClass(); attribute.Name = CAPICOM_ATTRIBUTE.CAPICOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME; attribute.Value = DateTime.Now.ToUniversalTime(); signer.AuthenticatedAttributes.Add(attribute); byte[] array3; if (flag) { // signedData.Content = ""; //signedData.Content = Marshal.PtrToStringBSTR( utilities.ByteArrayToBinaryString(array)); ((CapiComRCW.ISignedData)signedData).set_Content(utilities.ByteArrayToBinaryString(array)); try { signedData.Verify(Convert.ToBase64String(array2), true, CAPICOM_SIGNED_DATA_VERIFY_FLAG.CAPICOM_VERIFY_SIGNATURE_ONLY); } catch (Exception e) { errlist.Add("Ошибка проверки подписи!" + sFileIn + ":" + e.Message); } Store store2 = new StoreClass(); store2.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "AddressBook", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_WRITE); for (int i = 1; i <= signedData.Signers.Count; i++) { Signer signer2 = (Signer)signedData.Signers[i]; Certificate pVal = (Certificate)signer2.Certificate; store2.Add(pVal); } store2.Close(); string s = signedData.CoSign(signer, CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64); array3 = Convert.FromBase64String(s); } else { //signedData.Content = utilities.ByteArrayToBinaryString(array); ((CapiComRCW.ISignedData)signedData).set_Content(utilities.ByteArrayToBinaryString(array)); string s = signedData.Sign(signer, true, CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64); array3 = Convert.FromBase64String(s); } using (FileStream fileStream = new FileStream(sFileIn + ".sig", FileMode.Create, FileAccess.Write)) { fileStream.Write(array3, 0, array3.Length); fileStream.Close(); } }