PageRenderTime 115ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/com.documentum.devprog/src/com/documentum/devprog/eclipse/common/BlowfishJC.java

https://github.com/aldago/DQLScriptExecutor
Java | 180 lines | 66 code | 26 blank | 88 comment | 2 complexity | 8b286f0dc9f70e7369c17dd8f98b3f7b MD5 | raw file
  1. /*
  2. * Created on Jan 29, 2007
  3. *
  4. * EMC Developer Network
  5. */
  6. package com.documentum.devprog.eclipse.common;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.spec.SecretKeySpec;
  9. /**
  10. *
  11. * Blowfish encrypt/decrypt algorithm wrappers around the Java Crypto API
  12. * implementation
  13. *
  14. *
  15. * @author Aashish Patil(patil_aashish@emc.com)
  16. */
  17. public class BlowfishJC
  18. {
  19. private BlowfishJC()
  20. {
  21. //blank ctor
  22. //no need for a ctor
  23. }
  24. /**
  25. * Encrypts the data using the provided key.
  26. *
  27. * <br/>
  28. * Note that when converting from bytes to String and vice versa the default platform
  29. * encoding is used.
  30. *
  31. * <br/>
  32. * Algo Specs:
  33. * Blowfish/ECB/PKCS5Padding
  34. *
  35. * @param key shared key
  36. * @param data data to be encrypted.
  37. * @return encrypted data as a string.
  38. * @throws Exception
  39. */
  40. public static String encryptString(String key,String data) throws Exception
  41. {
  42. byte[] dataBytes = data.getBytes();
  43. return new String(encryptData(key,dataBytes));
  44. }
  45. /**
  46. * Decrypts the encrypted data using the provided key.
  47. *
  48. *
  49. * <br/>
  50. * Note that when converting from bytes to String and vice versa the default platform
  51. * encoding is used.
  52. *
  53. * <br/>
  54. * Algo Specs:
  55. * Blowfish/ECB/PKCS5Padding
  56. *
  57. * @param key shared key
  58. * @param encrString Encrypted data
  59. * @return Decrypted data
  60. * @throws Exception
  61. *
  62. *
  63. */
  64. public static String decryptString(String key,String encrString) throws Exception
  65. {
  66. byte[] encrData = encrString.getBytes();
  67. return new String(decryptData(key,encrData));
  68. }
  69. /**
  70. * Encrypts a byte array using the specified key.
  71. * <br/>
  72. * Algo Specs:
  73. * Blowfish/ECB/PKCS5Padding
  74. *
  75. * @param key encryption key
  76. * @param dataBytes bytes
  77. * @return encrypted bytes
  78. * @throws Exception
  79. */
  80. public static byte[] encryptData(String key, byte[] dataBytes)
  81. throws Exception
  82. {
  83. byte[] keyBytes = key.getBytes();
  84. String algo = "Blowfish/ECB/PKCS5Padding";
  85. SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "Blowfish");
  86. Cipher bfCipher = Cipher.getInstance(algo);
  87. bfCipher.init(Cipher.ENCRYPT_MODE, secretKey);
  88. byte[] encrData = bfCipher.doFinal(dataBytes);
  89. return encrData;
  90. }
  91. /**
  92. * Decrypts an encrypted byte array.
  93. *
  94. * <br/>
  95. * Algo Specs:
  96. * Blowfish/ECB/PKCS5Padding
  97. * @param key shared key
  98. * @param encrData
  99. * @return
  100. * @throws Exception
  101. */
  102. public static byte[] decryptData(String key, byte[] encrData)
  103. throws Exception
  104. {
  105. byte[] keyBytes = key.getBytes();
  106. String algo = "Blowfish/ECB/PKCS5Padding";
  107. SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "Blowfish");
  108. Cipher bfCipher = Cipher.getInstance(algo);
  109. bfCipher.init(Cipher.DECRYPT_MODE, keySpec);
  110. byte[] decryptedData = bfCipher.doFinal(encrData);
  111. return decryptedData;
  112. }
  113. //////////////////////////////////////////////////////////////
  114. //////UTILITY METHODS////////////////////////////////////////
  115. /////////////////////////////////////////////////////////////
  116. /**
  117. * Converts a string of Hex characters into a corresponding byte array
  118. *
  119. */
  120. public static byte[] convertHexToBytes(String hexString)
  121. {
  122. byte[] byteArr = new byte[hexString.length() / 2];
  123. for (int i = 0; i < byteArr.length; i++)
  124. {
  125. byteArr[i] = (byte) Integer.parseInt(hexString.substring(2 * i,
  126. 2 * i + 2), 16);
  127. }
  128. return byteArr;
  129. }
  130. /**
  131. * Converts a string of bytes into the corresponding hex string.
  132. *
  133. * @param val
  134. * @return
  135. */
  136. public static String convertBytesToHex(byte[] val)
  137. {
  138. char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  139. 'a', 'b', 'c', 'd', 'e', 'f' };
  140. // A hex char corresponds to 4 bits. Hence a byte (8 bits)
  141. // corresponds to two hex chars.
  142. StringBuffer bufHexVal = new StringBuffer(val.length * 2);
  143. for (int i = 0; i < val.length; i++)
  144. {
  145. byte b = val[i];
  146. int hiNibble = ((b & 0xf0) >> 4);
  147. int loNibble = (b & 0x0f);
  148. bufHexVal.append(hexChars[hiNibble]).append(hexChars[loNibble]);
  149. }
  150. String hexString = bufHexVal.toString();
  151. // System.out.println(hexString);
  152. return hexString;
  153. }
  154. }