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