· 7 years ago · Nov 20, 2018, 10:30 AM
1
2import java.security.SecureRandom;
3import java.util.Scanner;
4import javax.crypto.Cipher;
5import javax.crypto.KeyGenerator;
6import javax.crypto.SecretKey;
7import javax.crypto.spec.SecretKeySpec;
8
9/*
10 * To change this license header, choose License Headers in Project Properties.
11 * To change this template file, choose Tools | Templates
12 * and open the template in the editor.
13 */
14/**
15 *
16 * @author xyz abc
17 */
18public class DiffieHellman {
19
20 /**
21 * @param args the command line arguments
22 */
23 public static void main(String[] args) throws Exception {
24 Scanner input = new Scanner(System.in);
25 System.out.print("Enter P value: ");
26 int p = input.nextInt();
27 System.out.print("Enter G value: ");
28 int g = input.nextInt();
29
30 int aRandom = 0;
31 int bRandom = 0;
32 int aPublicKey = 0;
33 int bPublicKey = 0;
34 int aSecret = 0;
35 int bSecret = 0;
36 int aSharedSecret = 0;
37 int bSharedSecret = 0;
38 for (aSecret = 1; aSecret < p; aSecret++) {
39 for (bSecret = 1; bSecret < p; bSecret++) {
40 aRandom = (int) (Math.pow(g, aSecret));
41 bRandom = (int) (Math.pow(g, bSecret));
42 aPublicKey = aRandom % p;
43 bPublicKey = bRandom % p;
44 aSharedSecret = (int) ((Math.pow(bPublicKey, aSecret)) % 13);
45 bSharedSecret = (int) ((Math.pow(aPublicKey, bSecret)) % 13);
46 if (aSharedSecret == bSharedSecret) {
47 break;
48 }
49 }
50 }
51 System.out.println("aSecret key = " + aSecret + ", bSecret key=" + bSecret);
52 System.out.println("aRandom is " + aRandom + " and bRandom is " + bRandom);
53 System.out.println("The public key that should be sent from a to b is p= " + p + ", g= " + g + ", aPublicKey=" + aPublicKey);
54 System.out.println("The public key that should be sent from b to b is p= " + p + ", g= " + g + ", bPublicKey=" + bPublicKey);
55 System.out.println("The shared key that should be used in DES is: (" + aSharedSecret + ", " + bSharedSecret + ")");
56
57 System.out.print("Please enter a message to encrypt: ");
58 input = new Scanner(System.in);
59 String message = input.nextLine();
60 byte[] messageByte = message.getBytes();
61
62 //converting shared secret to string then bytes
63 byte[] skey = getRawKey((String.valueOf(aSharedSecret)).getBytes());
64 //encrypting
65 byte[] encryptedByte = encrypt(skey, messageByte);
66 String encryptedMessage = new String(encryptedByte);
67 System.out.println("The Cipher text is: " + encryptedMessage);
68
69 byte[] dbyte = decrypt(skey, encryptedByte);
70 String decryptedMessage = new String(dbyte);
71 System.out.println("Decrypted message " + decryptedMessage);
72
73 }
74
75 /**
76 * Method to generate encoded key using DES
77 *
78 * @param seed
79 * @return
80 * @throws Exception
81 */
82 private static byte[] getRawKey(byte[] seed) throws Exception {
83 KeyGenerator kgen = KeyGenerator.getInstance("DES");
84 SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
85 sr.setSeed(seed);
86 kgen.init(56, sr);
87 SecretKey skey = kgen.generateKey();
88 return skey.getEncoded();
89
90 }
91
92 /**
93 * Method to encrypt
94 *
95 * @param raw
96 * @param clear
97 * @return
98 * @throws Exception
99 */
100 private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
101 SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
102 Cipher cipher = Cipher.getInstance("DES");
103 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
104 byte[] encrypted = cipher.doFinal(clear);
105 return encrypted;
106 }
107
108 /**
109 * Method to decrypt
110 *
111 * @param raw
112 * @param encrypted
113 * @return
114 * @throws Exception
115 */
116 private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
117 SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
118 Cipher cipher = Cipher.getInstance("DES");
119 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
120 byte[] decrypted = cipher.doFinal(encrypted);
121 return decrypted;
122 }
123
124}