· 6 years ago · Nov 07, 2019, 10:52 AM
1package encryption;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.FileReader;
6import java.io.FileWriter;
7import java.io.IOException;
8import java.io.UnsupportedEncodingException;
9
10import java.security.AlgorithmParameters;
11import java.security.InvalidAlgorithmParameterException;
12import java.security.InvalidKeyException;
13import java.security.NoSuchAlgorithmException;
14import java.security.spec.InvalidKeySpecException;
15import java.security.spec.InvalidParameterSpecException;
16import java.security.spec.KeySpec;
17
18import java.util.logging.Level;
19import java.util.logging.Logger;
20
21import javax.crypto.BadPaddingException;
22import javax.crypto.Cipher;
23import javax.crypto.IllegalBlockSizeException;
24import javax.crypto.NoSuchPaddingException;
25import javax.crypto.SecretKey;
26import javax.crypto.SecretKeyFactory;
27import javax.crypto.spec.IvParameterSpec;
28import javax.crypto.spec.PBEKeySpec;
29import javax.crypto.spec.SecretKeySpec;
30
31/**
32 *
33 * @author jaredb
34 */
35public class PBE {
36
37 public static SecretKey deriveKey(String password, byte[] salt) {
38 try {
39 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
40 KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
41 SecretKey tmp = factory.generateSecret(spec);
42 SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
43
44 return secret;
45 } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
46 Logger.getLogger(PBE.class.getName()).log(Level.SEVERE, null, ex);
47 }
48
49 return null;
50 }
51
52 public static String encrypt(String plainText, SecretKey secret) {
53 try {
54 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
55 cipher.init(Cipher.ENCRYPT_MODE, secret);
56
57 AlgorithmParameters params = cipher.getParameters();
58 byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
59 byte[] ciphertext = cipher.doFinal(plainText.getBytes("UTF-8"));
60
61 String cipherString = new String(ciphertext);
62 String ivString = new String(iv);
63
64 try(BufferedWriter writer = new BufferedWriter(new FileWriter("stored.txt"))) {
65 writer.write(cipherString + ":" + ivString);
66 } catch(IOException ex) {
67 System.out.println(ex.getMessage());
68 }
69
70 return cipherString;
71 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException ex) {
72 Logger.getLogger(PBE.class.getName()).log(Level.SEVERE, null, ex);
73 }
74
75 return null;
76 }
77
78 public static String decrypt(String cipherText, SecretKey secret) {
79 byte[] iv = null;
80
81 try(BufferedReader reader = new BufferedReader(new FileReader("stored.txt"))) {
82 String line;
83
84 while((line = reader.readLine()) != null) {
85 cipherText = line.split(":")[0];
86 iv = line.split(":")[1].getBytes();
87 }
88 } catch(IOException ex) {
89 System.out.println(ex.getMessage());
90 }
91
92 try {
93 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
94 cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
95
96 String plaintext = new String(cipher.doFinal(cipherText.getBytes()), "UTF-8");
97 return plaintext;
98 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException ex) {
99 Logger.getLogger(PBE.class.getName()).log(Level.SEVERE, null, ex);
100 }
101
102 return null;
103 }
104
105}