-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
50 lines (45 loc) · 2.11 KB
/
Utils.java
File metadata and controls
50 lines (45 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Clase de Utilidades para Cifrado AES
*
* Autor: alejanf2885
* Github: https://github.com/alejanf2885
*/
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
public class Utils {
public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(n);
return keyGen.generateKey();
}
public static String encrypt(String data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
byte[] encryptedData = cipher.doFinal(data.getBytes());
byte[] encryptedDataWithIv = new byte[iv.length + encryptedData.length];
System.arraycopy(iv, 0, encryptedDataWithIv, 0, iv.length);
System.arraycopy(encryptedData, 0, encryptedDataWithIv, iv.length, encryptedData.length);
return Base64.getEncoder().encodeToString(encryptedDataWithIv);
}
public static String decrypt(String encryptedData, SecretKey key) throws Exception {
byte[] encryptedDataWithIv = Base64.getDecoder().decode(encryptedData);
byte[] iv = new byte[16];
byte[] encryptedBytes = new byte[encryptedDataWithIv.length - iv.length];
System.arraycopy(encryptedDataWithIv, 0, iv, 0, iv.length);
System.arraycopy(encryptedDataWithIv, iv.length, encryptedBytes, 0, encryptedBytes.length);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
byte[] decryptedData = cipher.doFinal(encryptedBytes);
return new String(decryptedData);
}
}