Iam rewriting old delphi aplication which using Chilkat lib for verifying signs in mails and external signs of mail attachments (two files ie. file.pdf and file.p7s). Is possible use Rebex.Security.Cryptography.Pkcs for it? This mens two things. First verify sign for specified file. Second verify sign certificates in p7s file.

asked 28 Jan '10, 10:36

Daniel%20Spurny's gravatar image

Daniel Spurny
765
accept rate: 0%

edited 10 Aug '10, 13:42

Rebex%20KB's gravatar image

Rebex KB ♦♦
258519


Yes, this is possible using the following code:

C#:

	// load the content of a file whose signature we wish to check
	byte[] content = File.ReadAllBytes(@"c:\temp\file.pdf");
	ContentInfo contentInfo = new ContentInfo(content);

	// create a PKCS #7 SignedData object base on this,
	// the second argument specifies "detached" style
	SignedData p7s = new SignedData(contentInfo, true);

	// load the detached signature file
	using (Stream input = File.OpenRead(@"c:\temp\file.p7s"))
	{
		p7s.Load(input);
	}

	// validate the signature and the certificates
	SignatureValidationResult result = p7s.Validate();

	// display the validation result
	if (result.Valid)
	{
		Console.WriteLine("Both signature and certificates are valid.");
	}
	else
	{
		Console.WriteLine("Signature or certificates are NOT valid.");
		Console.WriteLine("Signature problems: {0}", result.Status);
		Console.WriteLine("Certificate problems: {0}", result.CertificateValidationStatus);
	}

In addition to Rebex.Security.Cryptography.Pkcs namespace, this also needs Rebex.Security.Certificates from the same DLL.

For completeness, this is how you would create a detached signature (the file.p7s file) for file.pdf:

C#:

	// get a certificate with a private key
	Certificate certificate = ...

	// load the content of a file we need to sign
	byte[] content = File.ReadAllBytes(@"c:\temp\file.pdf");
	ContentInfo contentInfo = new ContentInfo(content);

	// create a PKCS #7 SignedDat object based on this,
	// the second argument specifies "detached" style
	SignedData p7s = new SignedData(contentInfo, true);

	// add signers
	SignerInfo signer = new SignerInfo(certificate);
	p7s.SignerInfos.Add(signer);

	// create the signature
	p7s.Sign();

	// save the signature into a detached signature file
	using (Stream output = File.Create(@"c:\temp\file.p7s"))
	{
		p7s.Save(output);
	}

If you would like a VB.NET version of this code, just let me know!

link

answered 28 Jan '10, 14:12

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28
accept rate: 31%

Thank you for quick answer a will try it.

(29 Jan '10, 15:30) Daniel Spurny

Hi, I´m trying to create and verify an e-mail using VB.NET 2010. I have a problem, I do not know what part of my e-mail is "ContentInfo". I have a smime.p7s file, but looking your C# code I can´t apply to my case.

Could you say me what part of e-mail is "ContentInfo" and if you can, show me the VB.NET code.

Grettings

link

answered 29 Jun '10, 08:24

Sergy's gravatar image

Sergy
1612
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×11
×3
×2
×1

Asked: 28 Jan '10, 10:36

Seen: 2,411 times

Last updated: 10 Aug '10, 13:42