· 5 years ago · Jun 29, 2020, 12:36 PM
1package chickencode;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.OutputStream;
6import java.security.InvalidKeyException;
7import java.security.NoSuchAlgorithmException;
8import java.security.SecureRandom;
9import java.util.Base64;
10import java.util.InvalidPropertiesFormatException;
11import java.util.Properties;
12
13import javax.crypto.Cipher;
14import javax.crypto.CipherInputStream;
15import javax.crypto.CipherOutputStream;
16import javax.crypto.KeyGenerator;
17import javax.crypto.NoSuchPaddingException;
18import javax.crypto.SecretKey;
19
20public class EncriptedProperties extends Properties {
21
22 public void storeEncripted(OutputStream os, String comment, String encoding) {
23 try {
24
25 KeyGenerator kg = KeyGenerator.getInstance("AES");
26 kg.init(new SecureRandom(new byte[] { 1, 2, 3 }));
27 final SecretKey key = kg.generateKey();
28 final Cipher c = Cipher.getInstance("AES");
29 c.init(Cipher.ENCRYPT_MODE, key);
30 CipherOutputStream output = new CipherOutputStream(os, c);
31 storeToXML(output, comment, encoding);
32 output.close();
33
34 } catch (NoSuchAlgorithmException e) {
35 e.printStackTrace();
36 } catch (NoSuchPaddingException e) {
37 e.printStackTrace();
38 } catch (InvalidKeyException e) {
39 e.printStackTrace();
40 } catch (IOException e) {
41 e.printStackTrace();
42 }
43 }
44
45 public void storeEncripted(OutputStream os, String comment) {
46 try {
47
48 KeyGenerator kg = KeyGenerator.getInstance("AES");
49 kg.init(new SecureRandom(new byte[] { 1, 2, 3 }));
50 final SecretKey key = kg.generateKey();
51 final Cipher c = Cipher.getInstance("AES");
52 c.init(Cipher.ENCRYPT_MODE, key);
53 CipherOutputStream output = new CipherOutputStream(os, c);
54 storeToXML(output, comment);
55 output.close();
56
57 } catch (NoSuchAlgorithmException e) {
58 e.printStackTrace();
59 } catch (NoSuchPaddingException e) {
60 e.printStackTrace();
61 } catch (InvalidKeyException e) {
62 e.printStackTrace();
63 } catch (IOException e) {
64 e.printStackTrace();
65 }
66 }
67
68 public void loadDecripted(InputStream is) {
69 try {
70
71 KeyGenerator kg2 = KeyGenerator.getInstance("AES");
72 kg2.init(new SecureRandom(new byte[] { 1, 2, 3 }));
73 final SecretKey key2 = kg2.generateKey();
74 final Cipher c2 = Cipher.getInstance("AES");
75 c2.init(Cipher.DECRYPT_MODE, key2);
76 CipherInputStream input = new CipherInputStream(is, c2);
77 loadFromXML(input);
78
79 } catch (NoSuchAlgorithmException e) {
80 e.printStackTrace();
81 } catch (NoSuchPaddingException e) {
82 e.printStackTrace();
83 } catch (InvalidKeyException e) {
84 e.printStackTrace();
85 } catch (InvalidPropertiesFormatException e) {
86 e.printStackTrace();
87 } catch (IOException e) {
88 e.printStackTrace();
89 }
90 }
91}