-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncriptar.java
More file actions
45 lines (40 loc) · 1.77 KB
/
Copy pathEncriptar.java
File metadata and controls
45 lines (40 loc) · 1.77 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ModelClass;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.apache.commons.codec.binary.Base64.encodeBase64;
/**
*
* @author AlexJPZ
*/
public class Encriptar {
// Definicion del tipo de algoritmo a utilizar (AES, DES, RSA)
private final static String alg = "AES";
// Definici�n del modo de cifrado a utilizar
private final static String cI = "AES/CBC/PKCS5Padding";
private static String key = "92AE31A79FEEB2A3"; //llave
private static String iv = "0123456789ABCDEF"; // vector de inicializacion
public static String encrypt(String cleartext) throws Exception {
Cipher cipher = Cipher.getInstance(cI);
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(cleartext.getBytes());
return new String(encodeBase64(encrypted));
}
public static String decrypt(String encrypted) throws Exception {
Cipher cipher = Cipher.getInstance(cI);
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
byte[] enc = decodeBase64(encrypted);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
byte[] decrypted = cipher.doFinal(enc);
return new String(decrypted);
}
}