C
C#9mo ago
Makstvell

AS2 over http C# Send message with signature to mendelson server.

Hi guys) I am new in this channel. Can you please help me with AS2 over http PKCS#7 .p7s file) Yeah. In c# I must create CMS p7s file in which will be data and signature. Do you know how to do it? I tried to do with system..PKCS and BouncyCastle. But When I tested it with mendelson server I have error Verification failed. With certificates all good. I think not good with generated CMS. Can you help me?
3 Replies
Makstvell
MakstvellOP9mo ago
Now for Sign data I have this code: public byte[] SignData(byte[] data, string p12FilePath, string p12Password) { byte[] contentHash; using (SHA256 sha256 = SHA256.Create()) { contentHash = sha256.ComputeHash(data); } // Load the signer's certificate X509Certificate2 signerCert = new X509Certificate2(p12FilePath, p12Password); // Create a SignedCms object SignedCms signedCms = new SignedCms(SubjectIdentifierType.IssuerAndSerialNumber, new ContentInfo(contentHash), false); // Create a CmsSigner object CmsSigner signer = new CmsSigner(signerCert); signer.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1"); // Sign the hash of the content signedCms.ComputeSignature(signer, false); // Encode the signed CMS message byte[] signature = signedCms.Encode(); return signature; } And this: request.Headers.Add("EDIINT-Features", "multiple-attachments"); // Create multipart form data string boundary = "STARTBOUND_" + Guid.NewGuid().ToString() + "_ENDBOUND"; var formData = new MultipartFormDataContent(boundary);
formData.Headers.ContentType = new MediaTypeHeaderValue("multipart/signed"); formData.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("protocol", ""application/pkcs7-signature"")); formData.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("micalg", "sha1")); formData.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", $""{boundary}"")); formData.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); formData.Headers.ContentDisposition.FileName = "smime.p7m"; var fileContentPart = new ByteArrayContent(as2Message); fileContentPart.Headers.ContentType = new MediaTypeHeaderValue("application/EDIFACT"); fileContentPart.Headers.Add("Content-Transfer-Encoding", "binary"); fileContentPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "data.edifact" }; formData.Add(fileContentPart); var signatureData = SignData(as2Message, _PathToMyPrivateKey, _Passphrase); var signatureContentPart = new StringContent(Convert.ToBase64String(signatureData)); signatureContentPart.Headers.ContentType = new MediaTypeHeaderValue("application/pkcs7-signature"); signatureContentPart.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("name", "smime.p7s")); signatureContentPart.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("smime-type", "signed-data")); signatureContentPart.Headers.Add("Content-Transfer-Encoding", "base64"); signatureContentPart.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "smime.p7s" }; formData.Add(signatureContentPart); request.Content = formData;
oke
oke9mo ago
use c# $code blocks
MODiX
MODiX9mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Want results from more Discord servers?
Add your server