PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/acid/src/main/java/net/rizon/acid/util/Blowfish.java

https://gitlab.com/rizon/acid
Java | 294 lines | 217 code | 31 blank | 46 comment | 26 complexity | fec072483347aff412fb7f47daaa5a54 MD5 | raw file
  1. package net.rizon.acid.util;
  2. /**
  3. * Blowfish.java version 1.00.00
  4. *
  5. * Code Written by k2r (k2r.contact@gmail.com)
  6. *
  7. * Tks to Mouser for his precious help.
  8. * Tks to Murx for correct Padding and Long key.
  9. *
  10. * Use this code is very simple :
  11. *
  12. * Use "Encrypt" with the text to encrypt
  13. * -> The function will encrypt and return the text with +OK at the biginning"
  14. *
  15. * Use "Decrypt" with the text to decrypt
  16. * --> The text must include the +OK or mcps at the front"
  17. *
  18. * There are a good exemple in "Main" function
  19. *
  20. * To Use Key > 16 char, you must update two jar files in your jre or jdk.
  21. * Java Cryptography Extension (JCE)
  22. * Unlimited Strength Jurisdiction Policy Files 1.4.2
  23. * http://java.sun.com/j2se/1.4.2/download.html#docs
  24. * Update the two files in jre\lib\security
  25. * -> local_policy.jar
  26. * -> US_export_policy.jar
  27. *
  28. */
  29. import java.io.UnsupportedEncodingException;
  30. import java.security.InvalidKeyException;
  31. import javax.crypto.Cipher;
  32. import javax.crypto.spec.SecretKeySpec;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. public class Blowfish
  36. {
  37. private static final Logger log = LoggerFactory.getLogger(Blowfish.class);
  38. /*
  39. * Constructor of Blowfish class Key param
  40. */
  41. public Blowfish(String key)
  42. {
  43. skeySpec = new SecretKeySpec(key.getBytes(), "Blowfish");
  44. // Preparing Blowfish mode
  45. try
  46. {
  47. ecipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
  48. }
  49. catch (Exception e)
  50. {
  51. log.error(null, e);
  52. }
  53. }
  54. /* Encrypt function */
  55. public String Encrypt(String tocrypt)
  56. {
  57. // Mode cypher in Encrypt mode
  58. try
  59. {
  60. ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  61. }
  62. catch (InvalidKeyException e)
  63. {
  64. log.error(null, e);
  65. }
  66. String REncrypt = "";
  67. // Paddind the String
  68. byte[] BEncrypt = tocrypt.getBytes();
  69. int Taille = BEncrypt.length;
  70. int Limit = 8 - (BEncrypt.length % 8);
  71. byte[] buff = new byte[Taille + Limit];
  72. for (int i = 0; i < Taille; i++)
  73. buff[i] = BEncrypt[i];
  74. for (int i = Taille; i < Taille + Limit; i++)
  75. buff[i] = 0x0;
  76. try
  77. {
  78. // Encrypt the padding string
  79. byte[] encrypted = ecipher.doFinal(buff);
  80. // B64 ENCRYPTION (mircryption needed)
  81. REncrypt = bytetoB64(encrypted);
  82. }
  83. catch (Exception e)
  84. {
  85. log.error(null, e);
  86. }
  87. REncrypt = Begin.concat(REncrypt);
  88. return REncrypt;
  89. }
  90. /* Decrypt function */
  91. public String Decrypt(String encrypt) throws UnsupportedEncodingException
  92. {
  93. if (encrypt.startsWith("+OK "))
  94. {
  95. encrypt = encrypt.substring(4, encrypt.length());
  96. }
  97. if (encrypt.startsWith("mcps "))
  98. {
  99. encrypt = encrypt.substring(5, encrypt.length());
  100. }
  101. // B64 DECRYPTION (mircryption needed)
  102. byte[] Again = B64tobyte(encrypt);
  103. byte[] decrypted = null;
  104. try
  105. {
  106. // Mode cypher in Decrypt mode
  107. ecipher.init(Cipher.DECRYPT_MODE, skeySpec);
  108. decrypted = ecipher.doFinal(Again);
  109. // Recup exact length
  110. int leng = 0;
  111. while (decrypted[leng] != 0x0)
  112. {
  113. leng++;
  114. }
  115. byte[] Final = new byte[leng];
  116. // Format & Limit the Result String
  117. int i = 0;
  118. while (decrypted[i] != 0x0)
  119. {
  120. Final[i] = decrypted[i];
  121. i++;
  122. }
  123. // Force again the encoding result string
  124. return new String(Final, "8859_1");
  125. }
  126. catch (Exception e)
  127. {
  128. // return e.getMessage();
  129. // Exception, not necessary padding, return directly
  130. // The decypted string
  131. return new String(decrypted, "8859_1");
  132. }
  133. }
  134. public static byte[] B64tobyte(String ec)
  135. {
  136. try
  137. {
  138. String dc = "";
  139. int k = -1;
  140. while (k < (ec.length() - 1))
  141. {
  142. int right = 0;
  143. int left = 0;
  144. int v = 0;
  145. int w = 0;
  146. int z = 0;
  147. for (int i = 0; i < 6; i++)
  148. {
  149. k++;
  150. v = B64.indexOf(ec.charAt(k));
  151. right |= v << (i * 6);
  152. }
  153. for (int i = 0; i < 6; i++)
  154. {
  155. k++;
  156. v = B64.indexOf(ec.charAt(k));
  157. left |= v << (i * 6);
  158. }
  159. for (int i = 0; i < 4; i++)
  160. {
  161. w = ((left & (0xFF << ((3 - i) * 8))));
  162. z = w >> ((3 - i) * 8);
  163. if (z < 0)
  164. {
  165. z = z + 256;
  166. }
  167. dc += (char) z;
  168. }
  169. for (int i = 0; i < 4; i++)
  170. {
  171. w = ((right & (0xFF << ((3 - i) * 8))));
  172. z = w >> ((3 - i) * 8);
  173. if (z < 0)
  174. {
  175. z = z + 256;
  176. }
  177. dc += (char) z;
  178. }
  179. }
  180. byte[] Result = new byte[1024];
  181. try
  182. {
  183. // Force the encoding result string
  184. Result = dc.getBytes("8859_1");
  185. }
  186. catch (UnsupportedEncodingException e)
  187. {
  188. log.error(null, e);
  189. }
  190. return Result;
  191. }
  192. catch (StringIndexOutOfBoundsException e)
  193. {
  194. throw new IllegalArgumentException("ec is not valid base64?", e);
  195. }
  196. }
  197. public static String bytetoB64(byte[] ec)
  198. {
  199. String dc = "";
  200. int left = 0;
  201. int right = 0;
  202. int k = -1;
  203. int v;
  204. while (k < (ec.length - 1))
  205. {
  206. k++;
  207. v = ec[k];
  208. if (v < 0)
  209. v += 256;
  210. left = v << 24;
  211. k++;
  212. v = ec[k];
  213. if (v < 0)
  214. v += 256;
  215. left += v << 16;
  216. k++;
  217. v = ec[k];
  218. if (v < 0)
  219. v += 256;
  220. left += v << 8;
  221. k++;
  222. v = ec[k];
  223. if (v < 0)
  224. v += 256;
  225. left += v;
  226. k++;
  227. v = ec[k];
  228. if (v < 0)
  229. v += 256;
  230. right = v << 24;
  231. k++;
  232. v = ec[k];
  233. if (v < 0)
  234. v += 256;
  235. right += v << 16;
  236. k++;
  237. v = ec[k];
  238. if (v < 0)
  239. v += 256;
  240. right += v << 8;
  241. k++;
  242. v = ec[k];
  243. if (v < 0)
  244. v += 256;
  245. right += v;
  246. for (int i = 0; i < 6; i++)
  247. {
  248. dc += B64.charAt(right & 0x3F);
  249. right = right >> 6;
  250. }
  251. for (int i = 0; i < 6; i++)
  252. {
  253. dc += B64.charAt(left & 0x3F);
  254. left = left >> 6;
  255. }
  256. }
  257. return dc;
  258. }
  259. private static String Begin = "+OK ";
  260. private static String B64 = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  261. private Cipher ecipher;
  262. private SecretKeySpec skeySpec;
  263. }