· 7 years ago · Jun 25, 2018, 04:10 PM
1public static void encrypt(String input, MetaData meta) throws Exception {
2
3 byte[] inputBytes = toByteArray(input);
4
5 meta.setText(inputBytes);
6
7 if(meta.getEncryptionType() != EncryptionType.none) {
8
9 if(!isValid(meta)) {
10
11 meta.setHashValue(generateHash(meta.getHashFunction(), inputBytes));
12 meta.setKey(new byte[] {});
13 }
14 else {
15
16 Cipher cipher = null;
17 Key key = null;
18 IvParameterSpec iv = null;
19
20 switch(meta.getOperation()) {
21
22 case Symmetric:
23 key = generateKey(meta);
24 iv = generateIvParameterSpec(meta);
25
26 if(!meta.getEncryptionMode().getType().equals("block"))
27 meta.setIV(iv.getIV());
28
29 cipher = generateCipher(Cipher.ENCRYPT_MODE, meta, key, iv);
30
31 meta.setKey(key.getEncoded());
32 break;
33
34 case Asymmetric:
35
36 SecureRandom rnd = new SecureRandom();
37 KeyPair keyPair = generateKeyPair(meta, rnd);
38
39 key = keyPair.getPublic();
40 meta.setKey(keyPair.getPrivate().getEncoded());
41
42 cipher = generateCipher(Cipher.ENCRYPT_MODE, key, rnd);
43 break;
44
45 case Password:
46 SecureRandom rndSalt = new SecureRandom();
47 byte[] salt = new byte[meta.getEncryptionType().getBlockSize()];
48
49 rndSalt.nextBytes(salt);
50 meta.setSalt(salt);
51
52 SecretKeyFactory sKeyFactory = SecretKeyFactory.getInstance(meta.getEncryptionType().toString(), "BC");
53 SecretKey sKey = sKeyFactory.generateSecret(new PBEKeySpec(meta.getPassword().toCharArray(), salt, iterationCount));
54
55 cipher = generateCipher(Cipher.ENCRYPT_MODE, meta, sKey);
56 break;
57
58 default:
59 break;
60 }
61
62 byte[] ciphertext = applyCipher(cipher, inputBytes);
63
64 meta.setText(ciphertext);
65 meta.setHashValue(generateHash(meta.getHashFunction(), ciphertext));
66 }
67 }
68 else {
69 meta.setHashValue(generateHash(meta.getHashFunction(), inputBytes));
70 }
71 }
72
73 public static void decrypt(MetaData meta) throws Exception {
74
75 if(meta.getEncryptionType() != EncryptionType.none) {
76
77 byte[] key = meta.getKey();
78 byte[] inputBytes = meta.getText();
79
80 if(meta.getHashValue() != null)
81 if(isHashValid(meta.getHashFunction(), inputBytes, meta.getHashValue()))
82 if(isValid(meta)) {
83
84 Cipher cipher = null;
85 switch(meta.getOperation()) {
86
87 case Symmetric:
88 IvParameterSpec iv = null;
89
90 if(!meta.getEncryptionMode().getType().equals("block"))
91 iv = new IvParameterSpec(meta.getIV());
92
93 cipher = generateCipher(Cipher.DECRYPT_MODE, meta, new SecretKeySpec(key, "BC"), iv);
94 break;
95 case Asymmetric:
96 cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
97
98 PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(key);
99 PrivateKey privKey = KeyFactory.getInstance("RSA", "BC").generatePrivate(privKeySpec);
100
101 cipher.init(Cipher.DECRYPT_MODE, privKey);
102 break;
103
104 case Password:
105 SecretKeyFactory sKeyFactory = SecretKeyFactory.getInstance(meta.getEncryptionType().toString(), "BC");
106 SecretKey sKey = sKeyFactory.generateSecret(new PBEKeySpec(meta.getPassword().toCharArray(), meta.getSalt(), iterationCount));
107
108 cipher = generateCipher(Cipher.DECRYPT_MODE, meta, sKey);
109 break;
110
111 default:
112 break;
113 }
114
115 meta.setText(cutLeftovers(applyCipher(cipher, meta.getText())));
116 }
117 }
118 else
119 System.out.println("Hash empty, hash function should be NONE");
120 }