|
| 1 | +import java.lang.*; |
| 2 | +import java.io.*; |
| 3 | +import java.util.Scanner; |
| 4 | +import java.math.BigInteger; |
| 5 | + |
| 6 | +class DesCrypto{ |
| 7 | + public String desEncryption(String cipher, String Key){ |
| 8 | + int[] IP={ |
| 9 | + 58,50,42,34,26,18,10,2, |
| 10 | + 60,52,44,36,28,20,12,4, |
| 11 | + 62,54,46,38,30,22,14,6, |
| 12 | + 64,56,48,40,32,24,16,8, |
| 13 | + 57,49,41,33,25,17,9,1, |
| 14 | + 59,51,43,35,27,19,11,3, |
| 15 | + 61,53,45,37,29,21,13,5, |
| 16 | + 63,55,47,39,31,23,15,7}; |
| 17 | + String BinCipher = HextoBinSplit(cipher); |
| 18 | + String L = BinCipher.substring(0,32); |
| 19 | + String R = BinCipher.substring(32); |
| 20 | + String BinKey = HextoBinSplit(Key); |
| 21 | + String IPvals = new String(); |
| 22 | + for (int i=0;i<64;i++){ |
| 23 | + IPvals = IPvals+BinCipher.charAt(IP[i]-1); |
| 24 | + } |
| 25 | + String sixteenkeys[]=SixteenSubkeys(BinKey); |
| 26 | + String plainvals = processDES(IPvals,sixteenkeys); |
| 27 | + String finalHexa = new String(); |
| 28 | + String[] substrings = plainvals.split("(?<=\\G.{8})"); |
| 29 | + for(int i=0;i<substrings.length;i++){ |
| 30 | + int dec = Integer.parseInt(substrings[i],2); |
| 31 | + String dummy = Integer.toString(dec,16); |
| 32 | + int len = dummy.length(); |
| 33 | + if(len < 2) |
| 34 | + { |
| 35 | + for (int k=0;k<2-len;k++){ |
| 36 | + dummy = "0"+dummy; |
| 37 | + } |
| 38 | + } |
| 39 | + finalHexa = finalHexa + dummy; |
| 40 | + } |
| 41 | + return finalHexa; |
| 42 | + } |
| 43 | + public String desDecryption(String cipher, String Key){ |
| 44 | + int[] IP={ |
| 45 | + 58,50,42,34,26,18,10,2, |
| 46 | + 60,52,44,36,28,20,12,4, |
| 47 | + 62,54,46,38,30,22,14,6, |
| 48 | + 64,56,48,40,32,24,16,8, |
| 49 | + 57,49,41,33,25,17,9,1, |
| 50 | + 59,51,43,35,27,19,11,3, |
| 51 | + 61,53,45,37,29,21,13,5, |
| 52 | + 63,55,47,39,31,23,15,7}; |
| 53 | + String BinCipher = HextoBinSplit(cipher); |
| 54 | + String L = BinCipher.substring(0,32); |
| 55 | + String R = BinCipher.substring(32); |
| 56 | + String BinKey = HextoBinSplit(Key); |
| 57 | + String IPvals = new String(); |
| 58 | + for (int i=0;i<64;i++){ |
| 59 | + IPvals = IPvals+BinCipher.charAt(IP[i]-1); |
| 60 | + } |
| 61 | + String sixteenkeys[]=SixteenSubkeys(BinKey); |
| 62 | + String[] InverseKeys = new String[16]; |
| 63 | + for(int i=15,j=0;i>=0;i--,j++){ |
| 64 | + InverseKeys[j] = sixteenkeys[i]; |
| 65 | + } |
| 66 | + //String plainvals = processDES(IPvals,sixteenkeys); |
| 67 | + String plainvals = processDES(IPvals,InverseKeys); |
| 68 | + //binary to Hexadecimal |
| 69 | + String finalHexa = new String(); |
| 70 | + String[] substrings = plainvals.split("(?<=\\G.{8})"); |
| 71 | + for(int i=0;i<substrings.length;i++){ |
| 72 | + int dec = Integer.parseInt(substrings[i],2); |
| 73 | + String dummy = Integer.toString(dec,16); |
| 74 | + int len = dummy.length(); |
| 75 | + if(len < 2) |
| 76 | + { |
| 77 | + for (int k=0;k<2-len;k++){ |
| 78 | + dummy = "0"+dummy; |
| 79 | + } |
| 80 | + } |
| 81 | + finalHexa = finalHexa + dummy; |
| 82 | + } |
| 83 | + return finalHexa; |
| 84 | + } |
| 85 | + public String Sbox(String KE){ |
| 86 | + int[][][] S={ |
| 87 | + {{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7}, |
| 88 | + {0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8}, |
| 89 | + {4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0}, |
| 90 | + {15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13} |
| 91 | + }, |
| 92 | + { |
| 93 | + {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10}, |
| 94 | + {3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5}, |
| 95 | + {0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15}, |
| 96 | + {13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9} |
| 97 | + }, |
| 98 | + { |
| 99 | + {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8}, |
| 100 | + {13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1}, |
| 101 | + {13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7}, |
| 102 | + {1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12} |
| 103 | + }, |
| 104 | + { |
| 105 | + {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15}, |
| 106 | + {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9}, |
| 107 | + {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4}, |
| 108 | + {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14} |
| 109 | + }, |
| 110 | + { |
| 111 | + {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9}, |
| 112 | + {14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6}, |
| 113 | + {4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14}, |
| 114 | + {11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}, |
| 115 | + }, |
| 116 | + { |
| 117 | + {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11}, |
| 118 | + {10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8}, |
| 119 | + {9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6}, |
| 120 | + {4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13} |
| 121 | + }, |
| 122 | + { |
| 123 | + {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1}, |
| 124 | + {13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6}, |
| 125 | + {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2}, |
| 126 | + {6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12} |
| 127 | + }, |
| 128 | + { |
| 129 | + {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7}, |
| 130 | + {1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2}, |
| 131 | + {7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8}, |
| 132 | + {2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11} |
| 133 | + } |
| 134 | + }; |
| 135 | + int[] P={ |
| 136 | + 16,7,20,21, |
| 137 | + 29,12,28,17, |
| 138 | + 1,15,23,26, |
| 139 | + 5,18,31,10, |
| 140 | + 2,8,24,14, |
| 141 | + 32,27,3,9, |
| 142 | + 19,13,30,6, |
| 143 | + 22,11,4,25 |
| 144 | + }; |
| 145 | + String[] substrings_8 = KE.split("(?<=\\G.{6})"); |
| 146 | + String S_8 = new String(); |
| 147 | + for(int l=0;l<8;l++){ |
| 148 | + String temp = substrings_8[l]; |
| 149 | + int i = Integer.parseInt(temp.substring(0,1)+temp.substring(5,6),2); |
| 150 | + int j = Integer.parseInt(temp.substring(1,5),2); |
| 151 | + int val = S[l][i][j]; |
| 152 | + String res = Integer.toString(val,2); |
| 153 | + int len = res.length(); |
| 154 | + if(len < 4) |
| 155 | + { |
| 156 | + for (int k=0;k<4-len;k++){ |
| 157 | + res = "0"+res; |
| 158 | + } |
| 159 | + } |
| 160 | + S_8 = S_8 + res; |
| 161 | + } |
| 162 | + String finalRes = new String(); |
| 163 | + for(int i=0;i<32;i++){ |
| 164 | + finalRes = finalRes +S_8.charAt(P[i]-1); |
| 165 | + } |
| 166 | + return finalRes; |
| 167 | + } |
| 168 | + public String xorFun(String L, String R, String Key){ |
| 169 | + int[] E={ |
| 170 | + 32,1,2,3,4,5, |
| 171 | + 4,5,6,7,8,9, |
| 172 | + 8,9,10,11,12,13, |
| 173 | + 12,13,14,15,16,17, |
| 174 | + 16,17,18,19,20,21, |
| 175 | + 20,21,22,23,24,25, |
| 176 | + 24,25,26,27,28,29, |
| 177 | + 28,29,30,31,32,1}; |
| 178 | + //change 32 to 48 bit; E(R) |
| 179 | + String R_48 = new String(); |
| 180 | + for(int i=0;i<48;i++){ |
| 181 | + R_48 = R_48 +R.charAt(E[i]-1); |
| 182 | + } |
| 183 | + //xor |
| 184 | + String KE = new String(); |
| 185 | + for(int i=0;i<48;i++){ |
| 186 | + KE = KE + Integer.toString(Character.getNumericValue(R_48.charAt(i))^Character.getNumericValue(Key.charAt(i))); |
| 187 | + } |
| 188 | + //Sbox |
| 189 | + String res = Sbox(KE); |
| 190 | + String resAfterXor = new String(); |
| 191 | + for(int i=0;i<32;i++){ |
| 192 | + resAfterXor = resAfterXor + Integer.toString(Character.getNumericValue(L.charAt(i))^Character.getNumericValue(res.charAt(i))); |
| 193 | + } |
| 194 | + return resAfterXor; |
| 195 | + } |
| 196 | + public String processDES(String IP,String[] sixteenkeys){ |
| 197 | + int[] IPinverse={ |
| 198 | + 40,8,48,16,56,24,64,32, |
| 199 | + 39,7,47,15,55,23,63,31, |
| 200 | + 38,6,46,14,54,22,62,30, |
| 201 | + 37,5,45,13,53,21,61,29, |
| 202 | + 36,4,44,12,52,20,60,28, |
| 203 | + 35,3,43,11,51,19,59,27, |
| 204 | + 34,2,42,10,50,18,58,26, |
| 205 | + 33,1,41,9,49,17,57,25 |
| 206 | + }; |
| 207 | + String L_0 = IP.substring(0,32); |
| 208 | + String R_0 = IP.substring(32); |
| 209 | + for(int i=0;i<16;i++){ |
| 210 | + String L_temp = R_0; |
| 211 | + String R_temp = xorFun(L_0,R_0,sixteenkeys[i]); |
| 212 | + L_0 = L_temp; |
| 213 | + R_0 = R_temp; |
| 214 | + } |
| 215 | + String temp = new String(); |
| 216 | + temp = R_0 + L_0; |
| 217 | + //IPinverse |
| 218 | + String final_vals = new String(); |
| 219 | + for (int i=0;i<64;i++){ |
| 220 | + final_vals = final_vals+temp.charAt(IPinverse[i]-1); |
| 221 | + } |
| 222 | + return final_vals; |
| 223 | + } |
| 224 | + public String PCOnePermutation(String key){ |
| 225 | + int[] PC_1={ |
| 226 | + 57,49,41,33,25,17,9, |
| 227 | + 1,58,50,42,34,26,18, |
| 228 | + 10,2,59,51,43,35,27, |
| 229 | + 19,11,3,60,52,44,36, |
| 230 | + 63,55,47,39,31,23,15, |
| 231 | + 7,62,54,46,38,30,22, |
| 232 | + 14,6,61,53,45,37,29, |
| 233 | + 21,13,5,28,20,12,4 |
| 234 | + }; |
| 235 | + String fiveSixkey = new String(); |
| 236 | + for (int i=0;i<56;i++){ |
| 237 | + fiveSixkey = fiveSixkey+key.charAt(PC_1[i]-1); |
| 238 | + } |
| 239 | + return fiveSixkey; |
| 240 | + } |
| 241 | + public String[] SixteenSubkeys(String key){ |
| 242 | + String[] c = new String[16]; |
| 243 | + String[] d = new String[16]; |
| 244 | + String[] subKeys = new String[16]; |
| 245 | + int[] PC_2={ |
| 246 | + 14,17,11,24,1,5, |
| 247 | + 3,28,15,6,21,10, |
| 248 | + 23,19,12,4,26,8, |
| 249 | + 16,7,27,20,13,2, |
| 250 | + 41,52,31,37,47,55, |
| 251 | + 30,40,51,45,33,48, |
| 252 | + 44,49,39,56,34,53, |
| 253 | + 46,42,50,36,29,32 |
| 254 | + }; |
| 255 | + String fiveSixkey = PCOnePermutation(key); |
| 256 | + String c_0 = fiveSixkey.substring(0,fiveSixkey.length()/2); |
| 257 | + String d_0 = fiveSixkey.substring(fiveSixkey.length()/2); |
| 258 | + for(int i=0;i<16;i++){ |
| 259 | + if(i==0 || i==1 || i==8 || i==15){ |
| 260 | + c_0 = c_0.substring(1)+c_0.charAt(0); |
| 261 | + d_0 = d_0.substring(1)+d_0.charAt(0); |
| 262 | + } |
| 263 | + else{ |
| 264 | + c_0 = c_0.substring(2)+c_0.charAt(0)+c_0.charAt(1); |
| 265 | + d_0 = d_0.substring(2)+d_0.charAt(0)+d_0.charAt(1); |
| 266 | + } |
| 267 | + c[i]=c_0; |
| 268 | + d[i]=d_0; |
| 269 | + } |
| 270 | + for(int j=0;j<16;j++){ |
| 271 | + String tempKey = c[j]+d[j]; |
| 272 | + String finalkey = new String(); |
| 273 | + for (int i=0;i<48;i++){ |
| 274 | + finalkey = finalkey + tempKey.charAt(PC_2[i]-1); |
| 275 | + } |
| 276 | + subKeys[j]=finalkey; |
| 277 | + } |
| 278 | + |
| 279 | + return subKeys; |
| 280 | + } |
| 281 | + public String HextoBinSplit(String cipher){ |
| 282 | + int len = cipher.length(); |
| 283 | + String res=new String(); |
| 284 | + for (int i=0;i<len;i=i+2){ |
| 285 | + res = res + HextoBin(cipher.substring(i, i+2)); |
| 286 | + } |
| 287 | + return res; |
| 288 | + } |
| 289 | + public String HextoBin(String cipher){ |
| 290 | + BigInteger hexVal = new BigInteger(cipher,16); |
| 291 | + String res = hexVal.toString(2); |
| 292 | + int len = hexVal.bitLength(); |
| 293 | + if(len < 8) |
| 294 | + { |
| 295 | + for (int i=0;i<8-len;i++){ |
| 296 | + res = "0"+res; |
| 297 | + } |
| 298 | + } |
| 299 | + return res; |
| 300 | + } |
| 301 | +} |
| 302 | + |
| 303 | +class desprog1 |
| 304 | +{ |
| 305 | + public static void main(String args[]){ |
| 306 | + Scanner input = new Scanner(System.in); |
| 307 | + System.out.println(" ------ DES cryptosystem ------"); |
| 308 | + System.out.println(" 1. Encryption"); |
| 309 | + System.out.println(" 2. Decryption"+"\n"); |
| 310 | + System.out.print(" Enter the choice: "); |
| 311 | + String choice = input.next(); |
| 312 | + System.out.println("Enter Hexadecimal key:"); |
| 313 | + String key = input.next(); |
| 314 | + if(choice.equals("1")){ |
| 315 | + System.out.println("Encryption selected"); |
| 316 | + String ciphertext = new String(); |
| 317 | + System.out.println("Enter plain:"); |
| 318 | + String plain = input.next(); |
| 319 | + DesCrypto ob = new DesCrypto(); |
| 320 | + String res = ob.desEncryption(plain,key); |
| 321 | + System.out.println("Hexadecimal cipher: "+res.toUpperCase( )); |
| 322 | + } |
| 323 | + else if(choice.equals("2")){ |
| 324 | + System.out.println("Decryption selected"); |
| 325 | + try{ |
| 326 | + String ciphertext = new String(); |
| 327 | + System.out.println("Enter file name:"); |
| 328 | + File filename = new File(input.next()); |
| 329 | + Scanner filereader = new Scanner(filename); |
| 330 | + while(filereader.hasNextLine()){ |
| 331 | + ciphertext += filereader.nextLine(); |
| 332 | + } |
| 333 | + String cipherSubstrings[]=ciphertext.split("(?<=\\G.{16})"); |
| 334 | + DesCrypto ob = new DesCrypto(); |
| 335 | + //create file |
| 336 | + PrintWriter writer = new PrintWriter("desDecryptionResult.txt", "UTF-8"); |
| 337 | + for(int i=0;i<cipherSubstrings.length;i++){ |
| 338 | + String finalres=ob.desDecryption(cipherSubstrings[i],key); |
| 339 | + System.out.println(HexaToText(finalres)); |
| 340 | + writer.print(HexaToText(finalres)); |
| 341 | + } |
| 342 | + writer.close(); |
| 343 | + } |
| 344 | + catch (Exception e){ |
| 345 | + System.out.println(e); |
| 346 | + } |
| 347 | + }else{ |
| 348 | + System.out.println("Select correct choise"); |
| 349 | + } |
| 350 | + } |
| 351 | + public static String HexaToText(String hexaVals){ |
| 352 | + String res = new String(); |
| 353 | + for(int i=0;i<hexaVals.length();i+=2){ |
| 354 | + String temp = hexaVals.substring(i,i+2); |
| 355 | + res += (char)Integer.parseInt(temp, 16); |
| 356 | + } |
| 357 | + return res; |
| 358 | + } |
| 359 | +} |
0 commit comments