943,831 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4265
  • Java RSS
Sep 5th, 2009
0

Java Encryption and Decryption

Expand Post »
Hi folks, i need some help on how to get this code to work properly:
I have not had the time to fully commit to its intrinsics but i am sure there is someone
in the community who can get me some quick help.

Thanx.
Java Syntax (Toggle Plain Text)
  1. package javaapplication1;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.spec.SecretKeySpec;
  6.  
  7.  
  8. /**
  9.  
  10.  * @author dkawalya
  11.  */
  12. public class DataEncryptor {
  13.  
  14.  
  15. protected DataEncryptor(){}
  16.  
  17. protected SecretKeySpec initialize() throws Exception {
  18.  
  19. byte[] mySecretKey = {
  20. (byte)0xbb,(byte)0xbb,(byte)0xbb,(byte)0xbb,(byte)0xbb,(byte)0xbb,
  21. (byte)0xbb,(byte)0xbb,(byte)0xbb,(byte)0xbb,(byte)0xbb,(byte)0xbb,
  22. (byte)0xbb,(byte)0xdd,(byte)0xdd,(byte)0xcc };
  23.  
  24. SecretKeySpec myKeySpec = new SecretKeySpec(mySecretKey, "AES");
  25. return myKeySpec;
  26.  
  27.  
  28. }
  29. protected byte[] encryptedDataBytes(String c) throws Exception{
  30. if(c == null)
  31. return null;
  32. try{
  33. SecretKeySpec keySpec = initialize();
  34. if (keySpec == null)
  35. return null;
  36. Cipher myCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  37. myCipher.init(Cipher.ENCRYPT_MODE, keySpec);
  38. byte[] dataBytes = c.getBytes();
  39. byte[] encryptedData = myCipher.doFinal(dataBytes);
  40. return encryptedData;
  41.  
  42. }catch(Exception e){
  43. e.printStackTrace();
  44. return null;
  45. }
  46. //The rest of the throwables are not caught and it is up to the calling
  47. //routine to catch them
  48. }
  49.  
  50. protected String decryptDataBytes(byte[] encryptedBytes) throws Exception{
  51. if (encryptedBytes == null)
  52. return null;
  53. try{
  54. SecretKeySpec keySpec = initialize();//initialize is a method declared above and returns the SecretKeySpec
  55.  
  56. if (keySpec == null)
  57. return null;
  58. //Get an instance of the cipher
  59. Cipher myCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  60. myCipher.init(Cipher.ENCRYPT_MODE,keySpec);
  61. myCipher.init(Cipher.DECRYPT_MODE, keySpec,myCipher.getParameters());
  62.  
  63. byte[] decryptedData = myCipher.doFinal(encryptedBytes);
  64. return new String(decryptedData);
  65.  
  66. }catch(Exception e){
  67. e.printStackTrace();
  68. return null;
  69. }
  70. }
  71.  
  72. //The above two methods are byte based whereas the ones below are string based
  73.  
  74. protected String encryptData(String c) throws Exception{
  75. if(c == null)
  76. return null;
  77. try{
  78. SecretKeySpec keySpec = initialize();
  79. if (keySpec == null)
  80. return null;
  81. Cipher myCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  82. myCipher.init(Cipher.ENCRYPT_MODE, keySpec);
  83. byte[] dataBytes = c.getBytes();
  84. byte[] encryptedData = myCipher.doFinal(dataBytes);
  85. return new String((encryptedData));
  86. }catch(Exception e){
  87. e.printStackTrace();
  88. return null;
  89. }
  90. //The rest of the throwables are not caught and it is up to the calling
  91. //routine to catch them
  92. }
  93.  
  94. protected String decryptData(String c) throws Exception{
  95. if (c == null)
  96. return null;
  97. try{
  98. SecretKeySpec keySpec = initialize();
  99.  
  100. if (keySpec == null)
  101. return null;
  102. //Get an instance of the cipher
  103. Cipher myCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  104. myCipher.init(Cipher.ENCRYPT_MODE,keySpec);
  105. myCipher.init(Cipher.DECRYPT_MODE, keySpec,myCipher.getParameters());
  106. /*Try to implement the padding, described in http://www.faqs.org/rfcs/rfc2315.html, at line 63
  107. which is 2. Some content-encryption algorithms assume the input length is a multiple of k octets, where k > 1, and let the application
  108. define a method for handling inputs whose lengths are not a multiple of k octets. For such algorithms, the method shall be to pad the
  109. input at the trailing end with k - (l mod k) octets all having value k -
  110. (l mod k), where l is the length of the input. In other words, the input is padded at the trailing end with one of the following strings:
  111. 01 -- if l mod k = k-1
  112. 02 02 -- if l mod k = k-2
  113. > .
  114. k k ... k k -- if l mod k = 0 The padding can be removed unambiguously since all input is padded and no padding string is a suffix of another. This
  115. padding method is well-defined if and only if k < 256; methods for larger k are an open issue for further study.*/
  116. //Pad up the bytes as according to the convention above
  117. ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
  118. byteArray.write(c.getBytes());
  119.  
  120. for(int i = ((c.length() == 16) ? 16 : (16 - c.length() % 16)); i>0; i--){
  121. byteArray.write((byte)(16 -(c.length() % 16)));
  122. }
  123. byte[] bytesToDecrypt = byteArray.toByteArray(); //This should be the padded up array of //bytes
  124.  
  125. byte[] decryptedData = myCipher.doFinal(bytesToDecrypt);// i get this error on this line: javax.crypto.IllegalBlockSizeException:
  126. //Input length must be multiple of 16 when decrypting with //padded cipher
  127. return new String(decryptedData);
  128.  
  129. }catch(Exception e){
  130. e.printStackTrace();
  131. return null;
  132. }
  133. }
  134.  
  135. public static void main(String[] args){
  136. try{
  137. DataEncryptor davis = new DataEncryptor();
  138. //Lets use the byte based routines first
  139. byte[] encryptedBytes = davis.encryptedDataBytes("My brains seem to be in normal mode");
  140. System.out.println(new String(encryptedBytes)); //print the string of encrypted bytes
  141. String decrypt = davis.decryptDataBytes(encryptedBytes);//decrypt the data bytes returned by //the above routine
  142. System.out.println(decrypt);// print the decrypted bytes as a string, but this is what i get(the //string is decrypted half way): #H�F��=���#]�Do be in normal mode
  143.  
  144. //Now lets use the string based routines
  145. String encryptedString = davis.encryptData("My brains seem to be in normal mode");//encrypt //this string
  146. System.out.println(encryptedString);//print the encrypted string
  147. String decryptedString = davis.decryptData(encryptedString);//decrypt the above string
  148. System.out.println(decryptedString);//print out the decrypted string and get this error:
  149.  
  150. }catch(Exception e){
  151. System.out.println(e.getMessage());
  152. e.printStackTrace();
  153. }
  154.  
  155. }
  156.  
  157. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
kawalya is offline Offline
6 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: instantiating array objects
Next Thread in Java Forum Timeline: Client and server with objects





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC