{"id":8529,"date":"2022-02-21T21:30:35","date_gmt":"2022-02-21T20:30:35","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=8529"},"modified":"2022-03-10T08:27:50","modified_gmt":"2022-03-10T07:27:50","slug":"c-import-a-rsa-public-generated-by-openssl","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/c-import-a-rsa-public-generated-by-openssl\/","title":{"rendered":"C#: Import a rsa public key generated by openssl"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_8529 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_8529')){$('.twoclick_social_bookmarks_post_8529').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"C%23%3A%20Import%20a%20rsa%20public%20key%20generated%20by%20openssl\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Athe%20C%23%20%28.NET%204%29%20RSACryptoServiceProvider-%3EImportCspBlob%20methode%20has%20the%20ability%20to%20import%20RSA%20%28public%29%20keys.%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/c-import-a-rsa-public-generated-by-openssl\\\/\",\"post_id\":8529,\"post_title_referrer_track\":\"C%23%3A+Import+a+rsa+public+key+generated+by+openssl\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>the C# (.NET 4) RSACryptoServiceProvider-><strong>ImportCspBlob <\/strong>methode has the ability to import RSA (public) keys.<br \/>\n<!--more--><br \/>\nIt use the Microsoft BLOB format and this cannot be changed. Hint: .NET Core can directly import PKCS1 keys.<br \/>\n<!-- https:\/\/gist.github.com\/crazybyte\/4142937\/2b1a8e2d72af55105df0a42c9fb02b7cedd2a3a4 --><br \/>\nIf you want to import a RSA public key generated by openssl it must be exported in the correct format ImportCspBlob understands. When you try to import the default openssl public key format you will get an error<br \/>\n<code><br \/>\nbad version of provider<br \/>\n<\/code><\/p>\n<p>openssl can export a key in the MS CryptoApi format. Set output format as &#8220;MS PUBLICKEYBLOB&#8221; respectively for a private key &#8220;MS PRIVATEKEYBLOB&#8221;.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nopenssl rsa -in my.key -passin pass:mySecret -RSAPublicKey_out -outform &quot;MS PUBLICKEYBLOB&quot; -out my_rsa.pem\r\n<\/pre>\n<p>And get the base64 signature<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nopenssl base64 -in my_rsa.pem\r\n<\/pre>\n<p>For example: Verify a file integrity <a href=\"https:\/\/michlstechblog.info\/blog\/openssl-sign-a-file-and-verify-it\/\" rel=\"noopener\" target=\"_blank\">created by this post<\/a>. You can use the base64 signature output from the openssl command or read signature from the pem file.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System.Security.Cryptography;\r\nnamespace TestRSAPublicKey\r\n{\r\n    class Program\r\n    {\r\n        static string PubKey= @&quot;BgIAAACkAABSU0ExAAgAAAEAAQCpnLBNQxZ+2i30CJ7Rq2j6Lyf\/YUkRVyok7ACM\r\nHdQMhvrW8297fE7EjU36Y7RbaXJakOIPS78AAudG1V6mpAEyttMEPZHu30rjdUIs\r\ntbxTiy5Q70MoAU5cxnWi0\/x3IUiQSWOeIQoeF1I1icqA06vOfomNEVedDrVjFVdG\r\nyP06nD3xESvBiyRS4+pqntDd45IBsWk0fjRsW6PkIygRan+oX\/GPoYQ9s1sRDTC5\r\nC4Nku7T\/Ek7KZ96KBiAjME2BKDuH6qawIqzrfKyOs3w3dMPi5MqOWRRjKWhOEaQI\r\niAj9Nx6jvTUUB5q6DQWqOq7Ahkg4UVuHazOIVhiI+CnO9BLW&quot;;\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();\r\n            \/\/ byte&#x5B;] pubKeyBytes = Convert.FromBase64String(PubKey);\r\n            byte&#x5B;] pubKeyBytes = System.IO.File.ReadAllBytes(@&quot;D:\\temp\\my_rsa.pem&quot;);\r\n            rsa.ImportCspBlob(pubKeyBytes);\r\n            SHA256Managed sha256 = new SHA256Managed();\r\n            byte&#x5B;] data = System.IO.File.ReadAllBytes(@&quot;D:\\temp\\ImportendData.txt&quot;);\r\n            byte&#x5B;] hash = sha256.ComputeHash(data);\r\n            byte&#x5B;] signature = System.IO.File.ReadAllBytes(@&quot;D:\\temp\\ImportendData.txt.sign&quot;);\r\n            bool Result=rsa.VerifyHash(hash, CryptoConfig.MapNameToOID(&quot;SHA256&quot;), signature);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, the C# (.NET 4) RSACryptoServiceProvider->ImportCspBlob methode has the ability to import RSA (public) keys.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[150,1036],"tags":[1817,1814,1171,1815,547,1816,1818],"class_list":["post-8529","post","type-post","status-publish","format-standard","hentry","category-c","category-openssl","tag-bad-version-of-provider","tag-ensure-file-integrity","tag-import","tag-importcspblob","tag-openssl","tag-public-key","tag-verify-file"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=8529"}],"version-history":[{"count":11,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8529\/revisions"}],"predecessor-version":[{"id":8620,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/8529\/revisions\/8620"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=8529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=8529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=8529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}