{"id":152,"date":"2010-12-04T03:54:16","date_gmt":"2010-12-04T08:54:16","guid":{"rendered":"http:\/\/joshho.com\/?p=152"},"modified":"2010-12-04T14:03:59","modified_gmt":"2010-12-04T19:03:59","slug":"gn-crypto-usage-example-in-java","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2010\/12\/04\/gn-crypto-usage-example-in-java\/","title":{"rendered":"GNU Crypto Usage Example in Java"},"content":{"rendered":"<p>So, how would one implement a cipher using the GNU Crypto algorithms?<\/p>\n<p>First things first, get the <a href=\"http:\/\/www.gnu.org\/software\/gnu-crypto\/\">jars<\/a> at the very least.<\/p>\n<p>Next, to ensure that your implementation of the Cipher in Java will work on all Java releases, ensure that you have the Base64Encoder\u00a0<a href=\"http:\/\/www.source-code.biz\/base64coder\/java\/Base64Coder.java.txt\">file<\/a> locally located within your project.<\/p>\n<p>From here, a good modified working example can be found here:<br \/>\n<!--more--> <strong>Working Code:<\/strong><\/p>\n<p><code><br \/>\nimport gnu.crypto.cipher.Twofish;<br \/> import java.io.UnsupportedEncodingException;<br \/> import java.lang.reflect.Array;<br \/> import java.security.InvalidKeyException;<\/p>\n<p> public class NewTest {<br \/> \/*public static void main(String argv[]) throws Exception {<br \/> String key = &quot;1234567812345678&quot;;<br \/> String plaintext = &quot;I am text to be hidden away&quot;;<br \/> String cipher = encrypt(plaintext,key);<br \/> System.out.println(cipher);<br \/> String cplaintext = decrypt(cipher,key);<br \/> System.out.println(cplaintext);<br \/> }*\/<\/p>\n<p> public static String encrypt (String cookieValue, String key) throws InvalidKeyException, UnsupportedEncodingException {<br \/> byte[] plainText;<br \/> byte[] encryptedText;<br \/> Twofish twofish = new Twofish();<br \/> \/\/ create a key<br \/> byte[] keyBytes = key.getBytes();<br \/> Object keyObject = twofish.makeKey(keyBytes, 16);<br \/> \/\/make the length of the text a multiple of the block size<br \/> if ((cookieValue.length() % 16) != 0) {<br \/> while ((cookieValue.length() % 16) != 0) {<br \/> cookieValue += &quot; &quot;;<br \/> }<br \/> }<br \/> \/\/ initialize byte arrays for plain\/encrypted text<br \/> plainText = cookieValue.getBytes(&quot;UTF8&quot;);<br \/> encryptedText = new byte[cookieValue.length()];<br \/> \/\/ encrypt text in 8-byte chunks<br \/> for (int i=0; i&lt;Array.getLength(plainText); i+=16) {<br \/> twofish.encrypt(plainText, i, encryptedText, i, keyObject, 16);<br \/> }<br \/> String encryptedString = Base64Coder.encodeLines(encryptedText);<br \/> return encryptedString;<br \/> }<\/p>\n<p> public static String decrypt (String cookieValue, String key) throws InvalidKeyException, UnsupportedEncodingException {<br \/> byte[] encryptedText;<br \/> byte[] decryptedText;<br \/> Twofish twofish = new Twofish();<br \/> \/\/create the key<br \/> byte[] keyBytes = key.getBytes();<br \/> Object keyObject = twofish.makeKey(keyBytes, 16);<br \/> \/\/make the length of the string a multiple of<br \/> \/\/the block size<br \/> if ((cookieValue.length() % 16) != 0) {<br \/> while ((cookieValue.length() % 16) != 0) {<br \/> cookieValue += &quot; &quot;;<br \/> }<br \/> }<br \/> \/\/initialize byte arrays that will hold encrypted\/decrypted<br \/> \/\/text<br \/> encryptedText = Base64Coder.decodeLines(cookieValue);<br \/> decryptedText = new byte[cookieValue.length()];<br \/> \/\/Iterate over the byte arrays by 16-byte blocks and decrypt.<br \/> for (int i=0; i&lt;Array.getLength(encryptedText); i+=16) {<br \/> twofish.decrypt(encryptedText, i, decryptedText, i, keyObject, 16);<br \/> }<br \/> String decryptedString = new String(decryptedText, &quot;UTF8&quot;);<br \/> return decryptedString;<br \/> }<br \/> }<br \/>\n<\/code><\/p>\n<p><a href=\"http:\/\/webcache.googleusercontent.com\/search?q=cache:nTz1UJBg4FUJ:www.webdeveloper.com\/forum\/showthread.php%3Ft%3D54852+http:\/\/www.webdeveloper.com\/forum\/showthread.php%3Ft%3D54852&#038;cd=1&#038;hl=en&#038;ct=clnk\">Bugged Source<\/a><\/p>\n<ul>\n<li>It is CRITICAL to ensure that your <span style=\"text-decoration: underline;\">block sizes matches the algorithm requirements<\/span>. The source uses Blowfish which uses 8 byte block sizes and my modified version of Twofish uses 16 byte block sizes.<\/li>\n<li>The second important thing to note is that your <span style=\"text-decoration: underline;\">key size matches the required size in the algorithm document<\/span>. The example above is using Twofish, which supports 128-bit keys, which of-course, results in my key being 128 bits or 16 bytes.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>So, how would one implement a cipher using the GNU Crypto algorithms?<\/p>\n<p>First things first, get the <a href=\"http:\/\/www.gnu.org\/software\/gnu-crypto\/\">jars<\/a> at the very least.<\/p>\n<p>Next, to ensure that your implementation of the Cipher in Java will work on all Java releases, ensure that you have the Base64Encoder\u00a0<a href=\"http:\/\/www.source-code.biz\/base64coder\/java\/Base64Coder.java.txt\">file<\/a> locally located within your project.<\/p>\n<p>From here, a good modified working example can be found here:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[29,32,35,34,30,31,28,33],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/152"}],"collection":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}