· 7 years ago · Mar 16, 2018, 02:22 PM
1import java.security.InvalidKeyException;
2import java.security.NoSuchAlgorithmException;
3import javax.crypto.BadPaddingException;
4import javax.crypto.Cipher;
5import javax.crypto.IllegalBlockSizeException;
6import javax.crypto.KeyGenerator;
7import javax.crypto.NoSuchPaddingException;
8import javax.crypto.SecretKey;
9import javax.crypto.spec.SecretKeySpec;
10
11/**
12 * @author diogo.souza
13 */
14public class Enc {
15
16 public Enc() {
17 }
18
19 /**
20 * Gera uma chave criptografica de 128bits aleatória
21 * @return byte[] key
22 */
23 public byte[] key() throws NoSuchAlgorithmException {
24 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
25 keyGen.init(256);
26 SecretKey key = keyGen.generateKey();
27 return key.getEncoded();
28 }
29
30 /**
31 * Encripta uma mensagem usando AES para uma dada chave
32 * @param byte[] input deve estar em um tamanho multiplo de 16 (nullPadString)
33 * @param byte[] key
34 * @return
35 */
36 public byte[] encode(byte[] input, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
37 SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
38 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
39 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
40 byte[] encrypted = cipher.doFinal(input);
41 return encrypted;
42 }
43
44 /**
45 * Retorna o valor original de uma mensagem criptgrafada em AES
46 * @param byte[] input
47 * @param byte[] key
48 * @return
49 */
50 public byte[] decode(byte[] input, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
51 SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
52 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
53 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
54 byte[] decrypted = cipher.doFinal(input);
55 return decrypted;
56 }
57
58 /**
59 * Converte uma mensagem criptografada para uma string de sua representação hexadecimal.
60 * @param byte[] hex
61 * @return String str
62 */
63 public String fromHex(byte[] hex) {
64 StringBuffer sb = new StringBuffer();
65 for (int i=0; i < hex.length; i++) {
66 sb.append( Integer.toString( ( hex[i] & 0xff ) + 0x100, 16).substring( 1 ) );
67 }
68 return sb.toString();
69 }
70
71 /**
72 * Converte uma representação hexadecimal para seus bytes hexadecimal (valor encriptado)
73 * @param String s
74 * @return byte[] data
75 */
76 public byte[] toHex(String s) {
77 int len = s.length();
78 byte[] data = new byte[len / 2];
79 for (int i = 0; i < len; i += 2) {
80 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
81 + Character.digit(s.charAt(i+1), 16));
82 }
83 return data;
84 }
85
86 /**
87 * Corrige o tamanho de uma String para multiplo de 16
88 * @param String original
89 * @return String final
90 */
91 public String nullPadString(String original) {
92 StringBuffer output = new StringBuffer(original);
93 int remain = output.length() % 16;
94 if (remain != 0) {
95 remain = 16 - remain;
96 for (int i = 0; i < remain; i++)
97 output.append((char) 0);
98 }
99 return output.toString();
100 }
101}