· 4 years ago · Dec 30, 2020, 07:00 AM
1import java.security.Key;
2import java.security.KeyFactory;
3import java.security.NoSuchAlgorithmException;
4import java.security.PrivateKey;
5import java.security.spec.InvalidKeySpecException;
6import java.security.spec.PKCS8EncodedKeySpec;
7import java.util.Base64;
8
9import javax.crypto.Cipher;
10import javax.crypto.KeyGenerator;
11import javax.crypto.SecretKey;
12import javax.crypto.spec.SecretKeySpec;
13
14
15import org.json.JSONObject;
16
17
18public class ProcessData {
19
20 private PrivateKey privatekey ;
21 private String aesKey = "";
22 private String resp = "";
23 private String jsonstr = "";
24
25 public String getSecretAESKeyAsString() throws Exception {
26
27 KeyGenerator generator = KeyGenerator.getInstance("AES");
28 generator.init(256); // The AES key size in number of bits
29 SecretKey secKey = generator.generateKey();
30 String encodedKey = Base64.getEncoder().encodeToString(secKey.getEncoded());
31 return encodedKey;
32 }
33
34 // Decrypt text using AES key
35 public String decryptTextUsingAES(String aesKeyString) throws Exception {
36
37 byte[] decodedKey = Base64.getDecoder().decode(aesKeyString);
38 SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
39
40 // AES defaults to AES/ECB/PKCS5Padding in Java 7
41 Cipher aesCipher = Cipher.getInstance("AES");
42 aesCipher.init(Cipher.DECRYPT_MODE, originalKey);
43 byte[]data = Base64.getDecoder().decode(this.resp.getBytes());
44
45 byte[] bytePlainText = aesCipher.doFinal(data);
46 return new String(bytePlainText);
47 }
48
49
50 // Decrypt AES Key using RSA public key
51 private String decryptAESKey(byte[] encryptedAESKey, PrivateKey privateKey) throws Exception {
52 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
53 cipher.init(Cipher.DECRYPT_MODE, privateKey);
54
55 return new String(cipher.doFinal(encryptedAESKey));
56 }
57
58
59 public String decryptData() throws Exception {
60 return decryptAESKey(Base64.getDecoder().decode(this.aesKey.getBytes()), this.privatekey);
61 }
62
63 public Key getPublicKey(String pubkey) {
64 Key test = null;
65
66 return test;
67
68 }
69 public PrivateKey getPrivateKey(String b64) {
70
71
72 return this.privatekey;
73
74 }
75 public void setPrivateKey(String skey) {
76
77 KeyFactory kf = null;
78 try {
79 kf = KeyFactory.getInstance("RSA");
80 } catch (NoSuchAlgorithmException e) {
81 // TODO Auto-generated catch block
82 e.printStackTrace();
83 }
84 PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(skey));
85 PrivateKey privKey = null;
86 try {
87 privKey = kf.generatePrivate(keySpecPKCS8);
88 } catch (InvalidKeySpecException e) {
89 // TODO Auto-generated catch block
90 e.printStackTrace();
91 }
92 this.privatekey = privKey;
93 }
94 public static void main(String[] args) {
95
96 ProcessData pd = new ProcessData();
97
98 JSONObject json = new JSONObject(pd.jsonstr);
99 String skey = (String)json.get("partnerPrivateKey");
100 String decryptAes = null;
101 pd.setPrivateKey(skey);
102 try {
103 decryptAes = pd.decryptData();
104 } catch (Exception e) {
105 // TODO Auto-generated catch block
106 e.printStackTrace();
107 }
108 String message = null;
109 try {
110 message = pd.decryptTextUsingAES(decryptAes);
111 } catch (Exception e) {
112 // TODO Auto-generated catch block
113 e.printStackTrace();
114 }
115 System.out.println(message);
116 }
117
118}
119