Search code examples
c#encryptionbouncycastlemimekit

How to decrypt smime file in C# application?


I have a file, which is encrypted using this command:

openssl smime -encrypt -aes256 -in fileToencrypt -binary -outform DEM -out encryptedFile public_key

It can be decrypted using command:

openssl smime -decrypt -in encryptedFile -binary -inform DEM -inkey private-key.pem -out decryptedFile

I need to decrypt it using private key (PEM format) in my .NET Core application. What could be the possible solution?


Solution

  • using System;
    using System.IO;
    using System.Linq;
    using System.Collections.Generic;
    
    using Org.BouncyCastle.Cms;
    using Org.BouncyCastle.Crypto;
    using Org.BouncyCastle.OpenSsl;
    using Org.BouncyCastle.X509;
    using Org.BouncyCastle.X509.Store;
    
    namespace SMimeDecryptExample
    {
        class Program
        {
            static void Main (string[] args)
            {
                AsymmetricKeyParameter key;
    
                using (var stream = File.OpenRead ("private-key.pem")) {
                    using (var reader = new StreamReader (stream)) {
                        var pem = new PemReader (reader);
    
                        var keyObject = pem.ReadObject ();
    
                        if (keyObject is AsymmetricCipherKeyPair pair) {
                            key = pair.Private;
                        } else if (keyObject is AsymmetricKeyParameter) {
                            key = (AsymmetricKeyParameter) keyObject;
                        }
                    }
                }
    
                var encryptedData = File.ReadAllBytes (args[0]);
                var parser = new CmsEnvelopedDataParser (encryptedData);
                var recipients = parser.GetRecipientInfos ();
                byte[] decryptedData;
    
                foreach (RecipientInformation recipient in recipients.GetRecipients ()) {
                    decryptedData = recipient.GetContent (key);
                    break;
                }
    
                // now you can do whatever you want with the decrypted data
            }
        }
    }