/foundation-security/src/main/java/com/brazoft/foundation/security/Crypto.java

https://bitbucket.org/gustavopuga/foundation · Java · 146 lines · 107 code · 36 blank · 3 comment · 5 complexity · 2483000ce511f07817dc55c625d67e05 MD5 · raw file

  1. package com.brazoft.foundation.security;
  2. import java.security.NoSuchAlgorithmException;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.SecretKey;
  6. import sun.misc.BASE64Decoder;
  7. import sun.misc.BASE64Encoder;
  8. import com.brazoft.foundation.util.ExceptionHandler;
  9. /**
  10. * @author Anderson Braz - anderson.braz@brazoft.com.br
  11. */
  12. public class Crypto
  13. {
  14. public static final String ALGORITHM_AES = "AES";
  15. public static final String ALGORITHM_BLOWFISH = "Blowfish";
  16. public static final String ALGORITHM_DES = "DES";
  17. public static final String ALGORITHM_DESede = "DESede";
  18. public static final String ALGORITHM_PBE_MD5_DES = "PBEWithMD5AndDES";
  19. public static final String ALGORITHM_PBE_HMAC_SHA1_DESede = "PBEWithHmacSHA1AndDESede";
  20. public static final String ALGORITHM_RC2 = "RC2";
  21. public static final String ALGORITHM_RC4 = "RC4";
  22. public static final String ALGORITHM_RC5 = "RC5";
  23. public static final String ALGORITHM_RSA = "RSA";
  24. public static final String MODE_NONE = "NONE";
  25. public static final String MODE_CBC = "CBC";
  26. public static final String MODE_CFB = "CFB";
  27. public static final String MODE_ECB = "ECB";
  28. public static final String MODE_OFB = "OFB";
  29. public static final String MODE_PCBC = "PCBC";
  30. public static final String PADDING_NONE = "NoPadding";
  31. public static final String PADDING_OAEP_MD5_MGF1 = "OAEPWithMD5AndMGF1Padding";
  32. public static final String PADDING_PKCS5 = "PKCS5Padding";
  33. public static final String PADDING_SSL3 = "SSL3Padding";
  34. public static final String ENCODING = "UTF-8";
  35. public static SecretKey generateKey(String algorithm)
  36. {
  37. try
  38. {
  39. return KeyGenerator.getInstance(algorithm).generateKey();
  40. }
  41. catch (NoSuchAlgorithmException e)
  42. {
  43. ExceptionHandler.handleRuntime(e);
  44. return null;
  45. }
  46. }
  47. public static String encrypt(String message, String algorithm, SecretKey key)
  48. {
  49. Cipher encryptor;
  50. try
  51. {
  52. encryptor = Cipher.getInstance(algorithm);
  53. encryptor.init(Cipher.ENCRYPT_MODE, key);
  54. return new BASE64Encoder().encode(encryptor.doFinal(message.getBytes(Crypto.ENCODING)));
  55. }
  56. catch (Exception e)
  57. {
  58. ExceptionHandler.handleRuntime(e);
  59. return null;
  60. }
  61. }
  62. public static String decrypt(String message, String algorithm, SecretKey key)
  63. {
  64. Cipher encryptor;
  65. try
  66. {
  67. encryptor = Cipher.getInstance(algorithm);
  68. encryptor.init(Cipher.DECRYPT_MODE, key);
  69. return new String(encryptor.doFinal( new BASE64Decoder().decodeBuffer(message)), Crypto.ENCODING);
  70. }
  71. catch (Exception e)
  72. {
  73. ExceptionHandler.handleRuntime(e);
  74. return null;
  75. }
  76. }
  77. public static boolean compare(String s1, String s2)
  78. {
  79. byte[] stored;
  80. byte[] typed;
  81. boolean match;
  82. try
  83. {
  84. match = true;
  85. stored = s1.getBytes(Crypto.ENCODING);
  86. typed = s2.getBytes(Crypto.ENCODING);
  87. if (stored.length != typed.length)
  88. {
  89. match = false;
  90. }
  91. else
  92. {
  93. for (int i = 0; i < stored.length; i++)
  94. {
  95. if (stored[i] != typed[i])
  96. {
  97. match = false;
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. catch(Exception e)
  104. {
  105. e.printStackTrace();
  106. match = false;
  107. }
  108. return match;
  109. }
  110. }