{"id":627,"date":"2004-09-03T09:30:16","date_gmt":"2004-09-03T13:30:16","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=627"},"modified":"2004-09-03T09:30:16","modified_gmt":"2004-09-03T13:30:16","slug":"pgp-decryption-using-c","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/09\/03\/pgp-decryption-using-c\/","title":{"rendered":"PGP Decryption using C#"},"content":{"rendered":"<p>Sorry if the PGP meme is getting old, I had to write some new code yesterday that decrypted a file full of ciphertext and I didn&#8217;t see any other examples on the net, so it gets posted here for posterity. Just to frame the issue, the ciphertext is actually stored in the database, so I first extract the ciphertext from a text column, write the ciphertext to a file, decrypt the file, read the results and then delete the file:<br \/>\n<code><br \/>\n\/\/ get ciphertext from DB<br \/>\n\/\/ and write to a file<br \/>\n\/\/ ...<br \/>\nstring passphrase = \"my_passphrase\";<br \/>\nstring filename = \"myapp\\encrypted.asc\";<br \/>\nProcessStartInfo psi = new ProcessStartInfo(\"pgp\");<br \/>\npsi.UseShellExecute = false;<br \/>\npsi.RedirectStandardInput = true;<br \/>\npsi.RedirectStandardOutput = true;<br \/>\npsi.RedirectStandardError = true;<br \/>\npsi.Arguments = filename + \" -m -z \" + passphrase;<br \/>\nProcess process = Process.Start(psi);<br \/>\nstring line = null;<br \/>\nstring message = null;<br \/>\nwhile((line = process.StandardOutput.ReadLine()) != null) {<br \/>\n\tmessage = line;<br \/>\n}<br \/>\nConsole.WriteLine(\"message = \" + message);<br \/>\n<\/code><br \/>\nIf you&#8217;re scoring at home, I used the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemDiagnosticsProcessStartInfoClassTopic.asp\">ProcessStartInfo<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemDiagnosticsProcessClassTopic.asp\">Process<\/a> classes from the .NET Framework to invoke <a href=\"http:\/\/www.pgpi.org\/\">pgp.exe<\/a> from the command line passing the <code>-m<\/code> flag so that the decrypted message is printed out to the screen (instead of pgp.exe decrypting the message to a new file) and passing the <code>-z<\/code> flag so that I can send the passphrase as an argument as well. In my project the message is only one line so I iterate over the lines of output until I get to the last line&#8230; where the message is then saved in the message string instance.<\/p>\n<p>Peeling away the code, you end up with this:<br \/>\n<code><br \/>\nC:\\pgp6.5.8&gt;pgp c:\\myapp\\encrypted.asc -m -z my_passphrase<br \/>\nPretty Good Privacy(tm) Version 6.5.8<br \/>\n(c) 1999 Network Associates Inc.<br \/>\nUses the RSAREF(tm) Toolkit, which is copyright RSA Data Security, Inc.<br \/>\nExport of this software may be restricted by the U.S. government.<br \/>\nmoreflagFile is encrypted.  Secret key is required to read it.<br \/>\nKey for user ID: Aaron Johnson<br \/>\n1024-bit DSS key, Key ID ******, created 2004\/09\/02<br \/>\nKey can sign.<br \/>\nJust a moment...<br \/>\nthis is the message<br \/>\n<\/code><\/p>\n<p>A caveat: if you run the above code from an ASP.NET web application make sure that ASPNET user has access to the private key.<\/p>\n<p>By the way, the folks over at <a href=\"http:\/\/www.bouncycastle.org\/\">Bouncy Castle<\/a> have a <a href=\"http:\/\/www.bouncycastle.org\/csharp\/\">C# port<\/a> of the their excellent Java encryption libraries, but it doesn&#8217;t appear that the <code>org.bouncycastle.openpgp<\/code> package has been ported just yet, otherwise I would have used that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorry if the PGP meme is getting old, I had to write some new code yesterday that decrypted a file full of ciphertext and I didn&#8217;t see any other examples on the net, so it gets posted here for posterity. Just to frame the issue, the ciphertext is actually stored in the database, so I &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/09\/03\/pgp-decryption-using-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PGP Decryption using C#<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,3,4,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/627"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=627"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/627\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}