· 9 years ago · Nov 22, 2016, 03:56 PM
1import java.io.IOException;
2import java.nio.charset.StandardCharsets;
3import java.security.InvalidKeyException;
4import java.util.ArrayList;
5
6import javax.crypto.BadPaddingException;
7import javax.crypto.Cipher;
8import javax.crypto.IllegalBlockSizeException;
9import javax.crypto.SecretKey;
10import javax.crypto.SecretKeyFactory;
11import javax.crypto.spec.DESKeySpec;
12
13import sun.misc.BASE64Decoder;
14import sun.misc.BASE64Encoder;
15
16
17public class Cryptography {
18
19 public static String DES_ENCRYPTION_KEY;
20 public static String INPUT;
21
22 private static final String CRYPTOGRAPHY_ALGO_DES = "DES";
23 private static Cipher cipher = null;
24 private static DESKeySpec keySpec = null;
25 private static SecretKeyFactory keyFactory = null;
26
27 public static String encrypt(String inputString, String commonKey)
28 throws InvalidKeyException, IllegalBlockSizeException,
29 BadPaddingException {
30
31 String encryptedValue = null;
32 SecretKey key = getSecretKey(commonKey);
33 cipher.init(Cipher.ENCRYPT_MODE, key);
34
35 byte[] inputBytes = compare(inputString);
36
37 byte[] outputBytes = cipher.doFinal(inputBytes);
38 encryptedValue = new BASE64Encoder().encode(outputBytes);
39 return encryptedValue;
40 }
41
42 public static String decrypt(String encryptedString, String commonKey)
43 throws InvalidKeyException, IllegalBlockSizeException,
44 BadPaddingException, IOException {
45
46 String decryptedValue = "";
47// When Base64Encoded strings are passed in URLs, '+' character gets converted to space and so we need to reconvert the space to '+' and since encoded string cannot have space in it so we are completely safe.
48 encryptedString = encryptedString.replace(' ', '+');
49 SecretKey key = getSecretKey(commonKey);
50 cipher.init(Cipher.DECRYPT_MODE, key);
51
52 byte[] recoveredBytes = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptedString));
53
54 int[] out = new int[recoveredBytes.length];
55
56 String s = "";
57
58 for (int i = 0; i < recoveredBytes.length; i++) {
59
60 out[i] = recoveredBytes[i] & 0xFF;
61 s += Integer.toBinaryString(out[i]);
62
63 }
64
65 decryptedValue = new String(s);
66 return decryptedValue;
67 }
68
69 private static SecretKey getSecretKey(String secretPassword) {
70
71 SecretKey key = null;
72 try {
73 cipher = Cipher.getInstance(CRYPTOGRAPHY_ALGO_DES);
74
75 byte[] desKeyData = compare(secretPassword);
76
77 keySpec = new DESKeySpec(desKeyData);
78
79 keyFactory = SecretKeyFactory.getInstance(CRYPTOGRAPHY_ALGO_DES);
80 key = keyFactory.generateSecret(keySpec);
81
82 } catch (Exception e) {
83 e.printStackTrace();
84 System.out.println("Error in generating the secret Key");
85 }
86 return key;
87 }
88
89 public static void main(String args[]) {
90
91 try{
92
93 INPUT = "0000101111010101111101111101101110010000010011011011100111010010";
94 DES_ENCRYPTION_KEY = "1111101011001011101100110110111001110010000011100001001110011010";
95
96 String encrypted = Cryptography.encrypt(INPUT, DES_ENCRYPTION_KEY);
97 System.out.println("encrypted: " + encrypted);
98 String decrypted = Cryptography.decrypt(encrypted, DES_ENCRYPTION_KEY);
99 System.out.println("decrypted: " + decrypted);
100
101 }
102
103 catch(Exception e){
104
105 }
106 }
107
108
109 public static byte[] compare(String value) {
110
111 try {
112
113 ArrayList<String> str = new ArrayList<String>();
114 ArrayList<Byte> inputBytes = new ArrayList<Byte>();
115
116 int countS = 0;
117 int countE = 8;
118 int i = 0;
119
120
121 while (true) {
122
123 try {
124
125 str.add(value.substring(countS, countE));
126
127 //System.out.println(str.get(i));
128
129 inputBytes.add((byte) Integer.parseInt(str.get(i), 2));
130
131 //System.out.println(i + " " + inputBytes.get(i));
132
133 countS += 8;
134 countE += 8;
135
136 i++;
137
138 } catch (Exception e2) {
139
140 countE--;
141
142 if(countE <= countS){
143 break;
144 }
145
146 }
147
148 }
149
150 byte[] array = new byte[inputBytes.size()];
151
152 for (int j=0; j < array.length; j++)
153 {
154 array[j] = inputBytes.get(j);
155 }
156 System.out.println("");
157 return array;
158
159 }
160
161 catch (Exception e1){
162
163 e1.printStackTrace();
164 return null;
165
166 }
167
168 }
169
170}