PageRenderTime 167ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/TestLadder/src/main/java/com/ickik/ladder/utils/TestLadderEncoding.java

https://github.com/ickik/TestLadder
Java | 131 lines | 84 code | 10 blank | 37 comment | 4 complexity | 64e657a74188f622b5adba604be1b346 MD5 | raw file
  1. package com.ickik.ladder.utils;
  2. import java.security.InvalidAlgorithmParameterException;
  3. import java.security.InvalidKeyException;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import javax.crypto.BadPaddingException;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.IllegalBlockSizeException;
  9. import javax.crypto.NoSuchPaddingException;
  10. import javax.crypto.SecretKey;
  11. import javax.crypto.spec.IvParameterSpec;
  12. import javax.crypto.spec.SecretKeySpec;
  13. import com.ickik.ladder.exception.LadderException;
  14. /**
  15. * Abstract class that contains encryption and decryption common methods.
  16. * @author Patrick Allgeyer (patrick.allgeyer@alcatel-lucent.com)
  17. * @version 0.1.000, 08/03/2010
  18. */
  19. public final class TestLadderEncoding {
  20. private static final String ALGORITHM = "Blowfish";
  21. private static final String TRANSFORMATION = "Blowfish/CBC/PKCS5Padding";
  22. private static final byte[] iv = { (byte) 0xc9, (byte) 0x36, (byte) 0x78, (byte) 0x99,
  23. (byte) 0x52, (byte) 0x3e, (byte) 0xea, (byte) 0xf2 };
  24. /**
  25. * Encrypts the password given by argument with the login as key. This
  26. * method is only used to encrypt the password for the .properties files
  27. * stored in local.
  28. * <br>The algorithm chooses is blowfish.
  29. * @param login the login of the user used as key.
  30. * @param password the password associates to the login.
  31. * @return the password encrypted with blowfish algorithm.
  32. * @throws LadderException this exception is thrown when an error occurs
  33. * during the execution of this method. The problem could be that the
  34. * algorithm was not found or the password or/and the key is/are null.
  35. */
  36. public static final String encryptionPassword(String login, String password) throws LadderException {
  37. IvParameterSpec salt = new IvParameterSpec(iv);
  38. SecretKey skeySpec = new SecretKeySpec(login.getBytes(), ALGORITHM);
  39. try {
  40. Cipher c = Cipher.getInstance(TRANSFORMATION);
  41. c.init(Cipher.ENCRYPT_MODE, skeySpec, salt);
  42. byte[] buffer = c.doFinal(password.getBytes());
  43. return new String(buffer);
  44. } catch (NoSuchAlgorithmException e) {
  45. throw new LadderException("");
  46. } catch (NoSuchPaddingException e) {
  47. throw new LadderException("");
  48. } catch (InvalidKeyException e) {
  49. throw new LadderException("");
  50. } catch (InvalidAlgorithmParameterException e) {
  51. throw new LadderException("");
  52. } catch (IllegalBlockSizeException e) {
  53. throw new LadderException("");
  54. } catch (BadPaddingException e) {
  55. throw new LadderException("");
  56. }
  57. }
  58. /**
  59. * Decrypts the password given by argument with the login as key. This
  60. * method is only used to decrypt the password for the .properties files
  61. * stored in local.
  62. * <br>The algorithm chooses is blowfish.
  63. * @param login the login of the user used as key.
  64. * @param password the password associates to the login.
  65. * @return the password decrypted with blowfish algorithm.
  66. * @throws LadderException this exception is thrown when an error occurs
  67. * during the execution of this method. The problem could be that the
  68. * algorithm was not found or the password or/and the key is/are null.
  69. */
  70. public static final String decryptionPassword(String login, String password) throws LadderException {
  71. IvParameterSpec salt = new IvParameterSpec(iv);
  72. SecretKey skeySpec = new SecretKeySpec(login.getBytes(), ALGORITHM);
  73. try {
  74. Cipher c = Cipher.getInstance(TRANSFORMATION);
  75. c.init(Cipher.DECRYPT_MODE, skeySpec, salt);
  76. byte[] buffer = c.doFinal(password.getBytes());
  77. return new String(buffer);
  78. } catch (NoSuchAlgorithmException e) {
  79. throw new LadderException("decryption.algorithm.invalid");
  80. } catch (NoSuchPaddingException e) {
  81. throw new LadderException("decryption.padding.invalid");
  82. } catch (InvalidKeyException e) {
  83. throw new LadderException("decryption.key.invalid");
  84. } catch (InvalidAlgorithmParameterException e) {
  85. throw new LadderException("decryption.algorithm.parameter.invalid");
  86. } catch (IllegalBlockSizeException e) {
  87. throw new LadderException("decryption.blocksize.illegal");
  88. } catch (BadPaddingException e) {
  89. e.printStackTrace();
  90. //throw new LadderException("decryption.padding.bad");
  91. }
  92. return password;
  93. }
  94. /**
  95. * Encrypts the password with MD5 algorithm. This method is used
  96. * the encrypt password to insert or check the users in database.
  97. * @param password the password to encrypt with MD5 algorithm.
  98. * @return the encrypted password under String format.
  99. * @throws LadderException
  100. */
  101. public static String encryptionPassword(String password) throws LadderException {
  102. byte[] uniqueKey = password.getBytes();
  103. StringBuilder hashString = new StringBuilder();
  104. try {
  105. byte[] hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
  106. int len = hash.length;
  107. for (int i = 0; i < len; i++) {
  108. String hex = Integer.toHexString(hash[i]);
  109. if (hex.length() == 1) {
  110. hashString.append('0');
  111. hashString.append(hex.charAt(hex.length() - 1));
  112. } else {
  113. hashString.append(hex.substring(hex.length() - 2));
  114. }
  115. }
  116. } catch (NoSuchAlgorithmException e) {
  117. throw new LadderException("", e);
  118. }
  119. return hashString.toString();
  120. }
  121. }