{"id":579,"date":"2004-03-20T18:40:54","date_gmt":"2004-03-20T22:40:54","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=579"},"modified":"2004-03-20T18:40:54","modified_gmt":"2004-03-20T22:40:54","slug":"java-pgp","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/03\/20\/java-pgp\/","title":{"rendered":"Java &amp; PGP"},"content":{"rendered":"<p>One of the Java projects I&#8217;m working on required that the application encrypted certain pieces of information (not files, but strings) using PGP, and from what I can tell, there aren&#8217;t a whole lot of libraries and\/or examples out there.  The one I saw mentioned most frequently was the <a href=\"http:\/\/www.bouncycastle.org\/\">Bouncy Castle Crypto API<\/a>, but in the short time that I looked through the <a href=\"http:\/\/www.bouncycastle.org\/documentation.html\">documentation<\/a> and the examples (org.bouncycastle.openpgp.examples), I saw nothing that looked like it would help me.  So I downloaded PGP 6.5.8 (the version we&#8217;ve standardized on at work) from <a href=\"http:\/\/www.pgpi.org\/products\/pgp\/versions\/freeware\/win32\/6.5.8\/\">pgpi.org<\/a> and hacked away at a version that works from the command line.  Here&#8217;s the result:<br \/>\n<code><br \/>\nString information = \"The random text information I want to encrypt.\";<br \/>\nString filename = \"tempfile.txt\";<br \/>\n\/\/ path to where I have pgp installed<br \/>\nString directory = \"c:\\\\pgp6.5.8\\\\\";<br \/>\n\/\/ my pgp key<br \/>\nString pgpkey = \"aaron.s.johnson@gmail.com\";<br \/>\n\/\/ create a file and write the string to it<br \/>\nFile outputfile = new File(directory + filename);<br \/>\nFileWriter out = new FileWriter(outputfile);<br \/>\nout.write(information.toCharArray());<br \/>\nout.close();<br \/>\n\/\/ get a runtime object<br \/>\n<a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/Runtime.html\">Runtime<\/a> rt = Runtime.getRuntime();<br \/>\n\/\/ execute pgp from the command line<br \/>\n\/\/ e=encrypt a=ascii t=text file w=wipe the file<br \/>\nProcess process = rt.exec(directory + \"pgp -eatw \" + directory + filename + \" \" + pgpkey);<br \/>\n\/\/ wait for the execution to end<br \/>\nprocess.waitFor();<br \/>\n\/\/ read the armored ASCII file<br \/>\nFile inputfile = new File(directory + filename + \".asc\");<br \/>\nBufferedReader isr = new BufferedReader(new FileReader(inputfile));<br \/>\nStringBuffer decrypted = new StringBuffer();<br \/>\nString line = \"\";<br \/>\nwhile ((line = isr.readLine()) != null) {<br \/>\n&nbsp;&nbsp;decrypted.append(line + \"\\n\");<br \/>\n}<br \/>\n\/\/ close the reader<br \/>\nisr.close();<br \/>\ninputfile.delete();<br \/>\n\/\/ print out the encrypted string for kicks<br \/>\nSystem.out.println(decrypted.toString());<br \/>\n<\/code><br \/>\nA quick summary.  The PGP executable assumes that you&#8217;re encrypting and decrypting files, so I first take the string that I want to encrypt and write it to a text file using the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/io\/File.html\">File<\/a> and <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/io\/FileWriter.html\">FileWriter<\/a> classes.  Then I use the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/Runtime.html\">Runtime<\/a> class to execute PGP from the command line, which encrypts the file and deletes the original plain text unencrypted file that I just created. I call the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/Process.html#waitFor()\">waitFor()<\/a> method on the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/Process.html\">Process<\/a> to make sure that the execution is complete, and then I read in resulting armored ASCII file using the <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/io\/File.html\">File<\/a>, <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/io\/FileReader.html\">FileReader<\/a> and <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/io\/BufferedReader.html\">BufferedReader<\/a> classes.  Finally, I delete the armored ASCII file. Obviously in production you&#8217;d want to use a unique name for the file so that you don&#8217;t overwrite or delete a file that another thread just created.  Other than that, are there any issues that I missed?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the Java projects I&#8217;m working on required that the application encrypted certain pieces of information (not files, but strings) using PGP, and from what I can tell, there aren&#8217;t a whole lot of libraries and\/or examples out there. The one I saw mentioned most frequently was the Bouncy Castle Crypto API, but in &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/03\/20\/java-pgp\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java &amp; PGP<\/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":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/579"}],"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=579"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/579\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}