/Seam2AutenticadorExterno/src/main/exemplo/infraestrutura/CriptografiaService.java

https://github.com/rafabene/PoCs · Java · 97 lines · 84 code · 13 blank · 0 comment · 4 complexity · ed776079f5cafc26fe62f07ba774d43f MD5 · raw file

  1. package exemplo.infraestrutura;
  2. import java.io.UnsupportedEncodingException;
  3. import java.math.BigInteger;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.spec.SecretKeySpec;
  10. import org.jboss.seam.annotations.AutoCreate;
  11. import org.jboss.seam.annotations.Name;
  12. import exemplo.dominio.exceptions.DescriptografiaException;
  13. @Name("criptografiaService")
  14. @AutoCreate
  15. public class CriptografiaService {
  16. private String convertToHex(byte[] data) {
  17. StringBuffer buf = new StringBuffer();
  18. for (int i = 0; i < data.length; i++) {
  19. int halfbyte = (data[i] >>> 4) & 0x0F;
  20. int two_halfs = 0;
  21. do {
  22. if ((0 <= halfbyte) && (halfbyte <= 9))
  23. buf.append((char) ('0' + halfbyte));
  24. else
  25. buf.append((char) ('a' + (halfbyte - 10)));
  26. halfbyte = data[i] & 0x0F;
  27. } while (two_halfs++ < 1);
  28. }
  29. return buf.toString();
  30. }
  31. public String SHA1(String text) {
  32. try {
  33. MessageDigest md = MessageDigest.getInstance("SHA-1");
  34. byte[] sha1hash = new byte[40];
  35. md.update(text.getBytes("iso-8859-1"), 0, text.length());
  36. sha1hash = md.digest();
  37. return convertToHex(sha1hash);
  38. } catch (Exception e) {
  39. throw new IllegalStateException(
  40. "Problema ao gerar o SHA-1 do valor", e);
  41. }
  42. }
  43. public char[] decode(String chave, String secret)
  44. throws DescriptografiaException {
  45. byte[] kbytes = chave.getBytes();
  46. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  47. BigInteger n = new BigInteger(secret, 16);
  48. byte[] encoding = n.toByteArray();
  49. try {
  50. Cipher cipher = Cipher.getInstance("Blowfish");
  51. cipher.init(Cipher.DECRYPT_MODE, key);
  52. byte[] decode = cipher.doFinal(encoding);
  53. return new String(decode).toCharArray();
  54. } catch (Exception e) {
  55. throw new DescriptografiaException(
  56. "Problema ao descriptografar senha", e);
  57. }
  58. }
  59. public String encode(String chave, String secret) {
  60. byte[] kbytes = chave.getBytes();
  61. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  62. try {
  63. Cipher cipher = Cipher.getInstance("Blowfish");
  64. cipher.init(Cipher.ENCRYPT_MODE, key);
  65. byte[] encoding = cipher.doFinal(secret.getBytes());
  66. BigInteger n = new BigInteger(encoding);
  67. return n.toString(16);
  68. } catch (Exception e) {
  69. throw new IllegalStateException("Problema ao criptografar senha", e);
  70. }
  71. }
  72. public static void main(String[] args) throws DescriptografiaException,
  73. NoSuchAlgorithmException, UnsupportedEncodingException {
  74. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHm");
  75. CriptografiaService c = new CriptografiaService();
  76. String chave = "SALrafael";
  77. String valorCifrado = c.encode(chave,
  78. "rafael|" + sdf.format(new Date()) + "|123");
  79. System.out.println(valorCifrado);
  80. char[] valorDecrifrado = c.decode(chave, valorCifrado);
  81. System.out.println(valorDecrifrado);
  82. System.out.println(c.SHA1(new String(valorDecrifrado)));
  83. }
  84. }