· 7 years ago · Apr 09, 2018, 12:56 PM
1public class AdminKeyGenerator {
2
3 private static final String ALGORITHM = "AES";
4
5
6 public static SecretKey generateKey() throws NoSuchAlgorithmException,
7 NoSuchPaddingException {
8
9 KeyGenerator genarator = KeyGenerator.getInstance(ALGORITHM);
10 SecretKey secretkey = genarator.generateKey();
11
12 return secretkey;
13
14 }
15
16 public static void saveKey(SecretKey key) {
17
18 File keyFile = new File("/home/thamiz/workspace/keyFile.txt");
19 try{
20
21 keyFile.createNewFile();
22 FileWriter keyWriter = new FileWriter(keyFile);
23 BufferedWriter buffKey = new BufferedWriter(keyWriter);
24
25 char[] hex = encodeHex( key.getEncoded() );
26
27 buffKey.write(hex);
28 buffKey.flush();
29 buffKey.close();
30
31
32 }catch(FileNotFoundException e){
33 System.out.println("Decryption fails");
34
35 } catch (IOException e) {
36 System.out.println("Decryption fails");
37 }
38
39 }
40
41
42
43 public static SecretKey loadKey() throws DecoderException{
44
45
46 File keyFile = new File("/home/thamiz/workspace/keyFile.txt");
47 String data = null;
48 byte[] encoded = null;
49
50 try{
51 FileReader keyReader = new FileReader( keyFile );
52 BufferedReader buffKeyRead = new BufferedReader(keyReader);
53
54 data = buffKeyRead.readLine();
55 encoded = decodeHex(data.toCharArray());
56 buffKeyRead.close();
57
58
59 }catch( IOException e ){
60 System.out.println("Decryption fails");
61 }
62
63
64 return new SecretKeySpec(encoded, ALGORITHM);
65
66
67
68 }
69
70
71 public static void main(String[] args) throws NoSuchAlgorithmException,
72 NoSuchPaddingException {
73
74 SecretKey secretkey = AdminKeyGenerator.generateKey();
75
76 AdminKeyGenerator.saveKey(secretkey);
77
78
79
80 }
81
82
83Following is the client side code. client used the admin key and encrypt his card details.
84
85
86 private static String debitcardType;
87 private static int debitCardNumber;
88 private static int debitcardCVV;
89 private static Date debitcardExpiryDate;
90
91
92 public static void storeCardDetails() throws ParseException {
93
94 Scanner in = new Scanner(System.in);
95 boolean anotherCardDetail = false;
96 SimpleDateFormat expiry = new SimpleDateFormat("dd-MM-yyyy");
97
98 do {
99
100 System.out.println("Enter your debit card type:");
101 debitcardType = in.next();
102
103 System.out.println("Enter your debit card number:");
104 debitCardNumber = in.nextInt();
105
106 System.out.println("Enter your debit card cvv number:");
107 debitcardCVV = in.nextInt();
108
109 System.out.println("Enter your debit card expiry date in the format of dd-MM-yyyy:");
110 String date = in.next();
111 debitcardExpiryDate = expiry.parse(date);
112
113 System.out.println("Do you want to enter another card detail");
114 System.out.println("Enter 1 for another card details and 0 for exit ");
115 int option = in.nextInt();
116 if (!(option == 1) && (option == 0)) {
117 anotherCardDetail = true;
118 System.out.println("Exit from entering card details");
119 }
120
121 } while (!anotherCardDetail);
122
123}
124
125 private static String encryptCardDetails(int cardNumber, int cardCVV,Date expiryDate, SecretKey key)
126 {
127
128 Cipher cipherencrypt = Cipher.getInstance("AES");
129
130 byte[] plainCardNumber = String.valueOf(cardNumber).getBytes();
131 byte[] plainCardCVV = String.valueOf(cardCVV).getBytes();
132 byte[] plainExpiryDate = expiryDate.toString().getBytes();
133
134 cipherencrypt.init( cipherencrypt.ENCRYPT_MODE, key);
135 byte[] encryptedCardNumber = cipherencrypt.doFinal(plainCardNumber);
136 byte[] encryptedCardCVV = cipherencrypt.doFinal(plainCardCVV);
137 byte[] encryptedExpiryDate = cipherencrypt.doFinal(plainExpiryDate);
138
139 String encryptedCard = Base64.encodeBase64String(encryptedCardNumber)
140 + Base64.encodeBase64String(encryptedCardCVV)
141 + Base64.encodeBase64String(encryptedExpiryDate);
142
143 return encryptedCard;
144
145}
146
147
148 public static String decryptCardDetails( File file, SecretKey key ) {
149
150 byte[] finalString1 = null;
151 byte[] finalString2 = null;
152 byte[] finalString3 = null;
153
154 String final1 = null,final2 = null, final3 = null;
155 Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
156
157 try{
158 FileReader decryptFile = new FileReader(file);
159 BufferedReader buff = new BufferedReader(decryptFile);
160
161 String decryptcontent = buff.readLine();
162
163 Scanner scan = new Scanner(decryptcontent).useDelimiter(",");
164
165 String cardType = scan.next();
166 System.out.println(cardType);
167 String decryptString = scan.next();
168 System.out.println(decryptString);
169
170 byte[] decrypt1 = Base64.decodeBase64(decryptString);
171 cipher.init(Cipher.DECRYPT_MODE, key);
172
173 byte[] decryptedText = cipher.doFinal(decrypt1);
174
175 final3 = new String(decryptedText, "UTF-8");
176
177 decryptedCardNumber.toString()+decryptedCardCVV.toString()+decryptedExpiryDate.toString();
178
179 }catch( IOException e ) {
180 System.out.println("Decryption fails");
181 }
182
183 return final3;
184
185
186}
187
188public static void main(String[] args) {
189
190 File cardDetailsFile = new File("/home/thamiz/workspace/cardFile.txt");
191
192 try {
193 cardDetailsFile.createNewFile();
194
195 FileWriter fileOut = new FileWriter(cardDetailsFile);
196 BufferedWriter buffer = new BufferedWriter(fileOut);
197
198 ClientCardDetails.storeCardDetails();
199
200 AdminKeyGenerator.generateKey();
201
202
203 String card = ClientCardDetails.encryptCardDetails(debitCardNumber, debitcardCVV,
204 debitcardExpiryDate, AdminKeyGenerator.generateKey());
205
206 buffer.write(debitcardType);
207 buffer.write(",");
208 buffer.write(card);
209 buffer.newLine();
210 buffer.flush();
211
212 SecretKey key = AdminKeyGenerator.loadKey();
213 String card1 = ClientCardDetails.decryptCardDetails(cardDetailsFile, key );
214 System.out.println("Decryption sucessful");
215 System.out.println(card1);
216
217
218 } catch (ParseException e) {
219 System.out.println("Enter incorrect card details");
220 } catch (IOException e) {
221 System.out.println("Encryption fails");
222 }
223
224}
225
226String card = ClientCardDetails.encryptCardDetails(
227 debitCardNumber,
228 debitcardCVV,
229 debitcardExpiryDate,
230 AdminKeyGenerator.generateKey()
231);