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

https://bitbucket.org/gustavopuga/foundation · Java · 124 lines · 85 code · 36 blank · 3 comment · 6 complexity · 35e187eecf9832eaf462c351382fcd0a MD5 · raw file

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