{"id":1333,"date":"2024-06-19T14:25:18","date_gmt":"2024-06-19T13:25:18","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=1333"},"modified":"2024-06-19T14:43:50","modified_gmt":"2024-06-19T13:43:50","slug":"decrypt-smime-encrypted-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/encryption\/bouncy-castle-library\/decrypt-smime-encrypted-file","title":{"rendered":"Decrypt smime encrypted file"},"content":{"rendered":"\n<p>A file encrypted using the following openssl command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>openssl  smime  -encrypt -aes256  -in InFilePath -binary  -outform DEM  \n-out OutFilePath PublicKeyFilePath<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Decrypt the file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tAsymmetricKeyParameter Key;\n\n\tusing (var Stream1 = File.OpenRead(\"C:\\\\My Folder\\\\my_private_key1.pem\"))\n\t{\n\t\tusing (var Reader1 = new StreamReader(Stream1))\n\t\t{\n\t\t\tvar Pem1 = new PemReader(Reader1);\n\n\t\t\tvar KeyObject = Pem1.ReadObject();\n\n\t\t\tif (KeyObject is AsymmetricCipherKeyPair pair)\n\t\t\t{\n\t\t\t\tKey = pair.Private;\n\t\t\t}\n\t\t\telse if (KeyObject is AsymmetricKeyParameter)\n\t\t\t{\n\t\t\t\tKey = (AsymmetricKeyParameter)KeyObject;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t}\n\n\tvar EncryptedData = File.ReadAllBytes(\"C:\\\\My Folder\\\\EncryptedFile.bin\");\n\n\tvar Parser = new CmsEnvelopedDataParser (EncryptedData);\n\tvar Recipients = Parser.GetRecipientInfos ();\n\tbyte&#91;] DecryptedData = new Byte&#91;0];\n\n\tforeach (RecipientInformation recipient in Recipients.GetRecipients())\n\t{\n\t\tDecryptedData = recipient.GetContent(Key);\n\t\tbreak;\n\t}\n\n\tif (DecryptedData.Length > 0)\n\t{\n\t\tFileStream File2 = new FileStream(\"C:\\\\My Folder\\\\DecruptedFile.bin\", FileMode.Create, FileAccess.Write, FileShare.None);\n\t\tFile2.Write(DecryptedData);\n\t\tFile2.Close();\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A file encrypted using the following openssl command: Decrypt the file<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[156],"tags":[],"class_list":["post-1333","post","type-post","status-publish","format-standard","hentry","category-bouncy-castle-library"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=1333"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1333\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1333\/revisions\/1336"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=1333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=1333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=1333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}