· 8 years ago · Dec 12, 2017, 06:36 PM
1
2package javaapplication3;
3
4import java.nio.charset.StandardCharsets;
5import javax.crypto.Cipher;
6import javax.crypto.spec.SecretKeySpec;
7
8public class EncClass{
9
10 public byte[] encrypt(byte[] plainText, String chave, String ALGORITHM) throws Exception
11 {
12
13 byte[] encryptionKey = chave.getBytes(StandardCharsets.UTF_8);
14 SecretKeySpec secretKey = new SecretKeySpec(encryptionKey, ALGORITHM);
15 Cipher cipher = Cipher.getInstance(ALGORITHM);
16 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
17
18 return cipher.doFinal(plainText);
19 }
20
21 public byte[] decrypt(byte[] cipherText, String chave, String ALGORITHM) throws Exception
22 {
23 byte[] encryptionKey = chave.getBytes(StandardCharsets.UTF_8);
24 SecretKeySpec secretKey = new SecretKeySpec(encryptionKey, ALGORITHM);
25 Cipher cipher = Cipher.getInstance(ALGORITHM);
26 cipher.init(Cipher.DECRYPT_MODE, secretKey);
27
28 return cipher.doFinal(cipherText);
29 }
30}