-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrypter.java
30 lines (25 loc) · 892 Bytes
/
Encrypter.java
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
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* Based on the DevMedia article:
* https://www.devmedia.com.br/utilizando-criptografia-simetrica-em-java/31170
*
* @author Matheus Teles
*/
public class Encrypter {
private String IV;
private String originalText;
private String cryptographyKey;
public Encrypter(String IV, String originalText, String cryptographyKey) {
this.IV = IV;
this.originalText = originalText;
this.cryptographyKey = cryptographyKey;
}
public byte[] encrypt() throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(cryptographyKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
return cipher.doFinal(originalText.getBytes("UTF-8"));
}
}