4

I am working on a solution that needs to decrypt PKCS#7 encrypted data, preferably in C#. As far as I can see, the .NET api has support for this through the System.Security.Cryptography.Pkcs namespace. However it seems that the implementation can only work on byte arrays. So what do I do when I have a large encrypted file that does not fit into the memory?

Am I missing something here, or is there another way to do this on a Stream level instead of using bytearrays?

4 Answers 4

5

The .NET framework has only rudimentary support for PKCS#7/CMS, so it does not support streaming the data.

You need a 3rd party library. As others have mentioned, BouncyCastle is an excellent choice. It does support PKCS#7 (PKCS#7 is also known as CMS).

Their tests are probably a good place to look for sample code: EnvelopedDataStreamTest.cs.

1
  • Ah, i didn't know about the c# version. Have used it in Java some years ago, and I know it supported PKCS#7 in that version. Strange I did not find this when Googling. However, thanks for pointing it out :-) Oct 1, 2009 at 22:17
1

I found an example of how to decrypt PKCS#7 in C#. It may be enough to get you started.

2
  • The only problem is that your example seems to operate on the file, rather than decrypting a stream. Oct 1, 2009 at 16:28
  • 1
    Also, Chilkat is commercial but BouncyCastle is open source and free. Oct 1, 2009 at 22:21
1

.Net Framework from 2.0 provided pkcs namespace that helps us to do sign/encrypt/decrypt in pkcs#7 standard. Please refer here for the overview http://msdn.microsoft.com/en-us/library/bb885086.aspx and here for a good example http:**msdn.microsoft.com/en-us/library/bb924565.aspx.

0

Do you have the option to change to using the BouncyCastle API, from http://www.bouncycastle.org/csharp/, as it uses streams for decryption.

But, I don't believe it can use PKCS#7 encrypted data, so you would need to use BouncyCastle for the encryption side also.

Here is an example of using this API: http://elian.co.uk/post/2009/07/29/Bouncy-Castle-CSharp.aspx

If you want to use the .NET stuff this may be useful: http://www.geekpedia.com/tutorial227_Encrypting-and-Decrypting-Files-with-Csharp.html

2
  • Bouncy Castle for C# can indeed both encrypt and decrypt using the PKCS#7 scheme. However they call it CMS (Cryptographic Messaging Syntax), which is what PKCS#7 defines. So using Bouncy Castle to solve this problem worked perfectly. Oct 9, 2009 at 10:51
  • @Johnny Egeland - Thank you, I didn't realize that PKCS#7 == CMS Oct 9, 2009 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.