· 4 years ago · Nov 12, 2020, 05:10 PM
1public SecretKeySpec createKey(String key) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
2 SecureRandom random = new SecureRandom();
3 byte[] sr = new byte[16];
4 random.nextBytes(sr);
5
6 KeySpec spec = new PBEKeySpec(key.toCharArray(), sr, 65536, 256); // AES-256
7 SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
8 byte[] key_bytes = f.generateSecret(spec).getEncoded();
9 SecretKeySpec keySpec = new SecretKeySpec(key_bytes, "AES");
10
11 return keySpec;
12 }
13
14 public String encrypt(String data, SecretKeySpec secret_key) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
15 Cipher cipher = Cipher.getInstance("AES");
16 cipher.init(Cipher.ENCRYPT_MODE, secret_key);
17
18 byte[] encrypt_data = data.getBytes("UTF-8");
19 byte[] encrypted_data = cipher.doFinal(encrypt_data);
20 String encrypted_string = Base64.getEncoder().encodeToString(encrypted_data);
21
22 return encrypted_string;
23 }
24
25 public String encryptRSAwithAES(PrivateKey data, SecretKeySpec secret_key) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
26 Cipher cipher = Cipher.getInstance("AES");
27 cipher.init(Cipher.ENCRYPT_MODE, secret_key);
28
29 byte[] encrypt_data = data.getEncoded();
30 byte[] encrypted_data = cipher.doFinal(encrypt_data);
31 String encrypted_string = Base64.getEncoder().encodeToString(encrypted_data);
32
33 return encrypted_string;
34 }
35
36 public String decrypt(String encrypted_string, byte[] decoded_key) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
37 SecretKeySpec secret_key = new SecretKeySpec(decoded_key, 0, decoded_key.length, "AES");
38 Cipher cipher = Cipher.getInstance("AES");
39 cipher.init(Cipher.DECRYPT_MODE, secret_key);
40
41 byte[] encrypted_data = Base64.getDecoder().decode(encrypted_string);
42 byte[] decrypted_data = cipher.doFinal(encrypted_data);
43
44 String data = new String(decrypted_data);
45
46 return data;
47 }
48
49 public KeyPair generateRSAPair() throws NoSuchAlgorithmException {
50 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
51 KeyPair kp = kpg.generateKeyPair();
52
53 return kp;
54 }
55
56 public String encryptKey(SecretKeySpec aes_key, PrivateKey rsa_private) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
57 byte[] encrypted_bytes;
58 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
59 cipher.init(Cipher.ENCRYPT_MODE, rsa_private);
60
61 encrypted_bytes = cipher.doFinal(aes_key.getEncoded());
62
63 String b_bytes = Base64.getEncoder().encodeToString(encrypted_bytes);
64
65 return b_bytes;
66 }
67
68 public byte[] decryptKey(String aes_key, String rsa_private) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
69 byte[] encrypted_bytes;
70 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(rsa_private));
71 Cipher cipher = Cipher.getInstance("RSA");
72 PrivateKey pk = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
73
74 cipher.init(Cipher.DECRYPT_MODE, pk);
75 byte[] dec_key = cipher.doFinal(aes_key.getBytes());
76
77 return dec_key;
78 }