· 9 years ago · Oct 03, 2016, 09:56 PM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 * GRAY_GRAY HOLE CODE
6 */
7package server1;
8import java.io.*;
9import java.net.*;
10
11import java.security.Security;
12import javax.crypto.Cipher;
13import de.flexiprovider.core.FlexiCoreProvider;
14import java.security.InvalidKeyException;
15import java.security.MessageDigest;
16import java.security.NoSuchAlgorithmException;
17import java.util.Arrays;
18import java.util.Scanner;
19import java.util.logging.Level;
20import java.util.logging.Logger;
21import javax.crypto.BadPaddingException;
22import javax.crypto.IllegalBlockSizeException;
23import javax.crypto.NoSuchPaddingException;
24import javax.crypto.spec.SecretKeySpec;
25import sun.misc.BASE64Encoder;
26
27public class Server1 {
28
29private static InetAddress IPAddress;
30private static String Data;
31private static String encryptdecrypt;//
32private static String ALGORITHM;//
33private static String macinfo;
34private static String pass;//
35private static byte[] key;
36//ENCRYPT
37public static String cryp(String cryptData,String pass) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException
38{
39 Security.addProvider(new FlexiCoreProvider());
40 key=UDPData.keydata(pass);
41 MessageDigest sha = MessageDigest.getInstance("SHA512");
42 key = sha.digest(key);
43 key = Arrays.copyOf(key, 16);
44 SecretKeySpec secretKey=new SecretKeySpec(key,ALGORITHM);
45 Cipher cipher;
46 cipher = Cipher.getInstance(ALGORITHM);
47 cipher.init(Cipher.ENCRYPT_MODE, secretKey);//secretKey
48 byte[] result = cipher.doFinal(cryptData.getBytes());
49 String encryptVal;
50 encryptVal = new BASE64Encoder().encode(result);
51 System.out.println("ENCRYPT:" +encryptVal);
52
53return encryptVal;
54}
55//DECRYPT
56public static String decryp(String cryptData,String pass) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException
57{
58 String decript;
59 Security.addProvider(new FlexiCoreProvider());
60 MessageDigest sha = MessageDigest.getInstance("SHA512");
61 key=UDPData.keydata(pass);
62 key = sha.digest(key);
63 key = Arrays.copyOf(key, 16);
64 SecretKeySpec secretKey=new SecretKeySpec(key,ALGORITHM);
65 Cipher cipher;
66 cipher = Cipher.getInstance(ALGORITHM);
67 cipher.init(Cipher.DECRYPT_MODE, secretKey);
68 byte[] result = cipher.doFinal(cryptData.getBytes());
69
70 decript = new BASE64Encoder().encode(result);
71 System.out.println("DECRYPT:" +decript);
72return decript;
73}
74//FINDSTRINGS NOT SO GOOD
75public static String Findpass(String Datapass) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException
76{
77 Scanner scan = new Scanner(Datapass);
78 String password="password:";
79 String username="username:";
80 String algoritm="algoritm:";
81 String encriptdecript="encriptdecript:";//class objects and others
82
83 System.out.println(scan.findInLine(password));
84 password=scan.next();
85 System.out.println(scan.findInLine(username));
86 username=scan.next();
87 System.out.println(scan.findInLine(algoritm));
88 ALGORITHM=scan.next();
89 scan.findInLine(encriptdecript);
90 //encryptdecrypt=scan.next();
91 System.out.println(password+username);
92 return password+username;
93}
94
95 public static void main(String args[]) throws Exception
96 {
97
98 new Thread(new Runnable() {
99 @Override
100 @SuppressWarnings("empty-statement")
101public void run() {
102
103 try {
104
105 DatagramSocket serverSocket = new DatagramSocket(5556);
106 byte[] receiveData = new byte[124];
107 byte[] sendData = new byte[124];
108 String encryData;
109 while(true)
110 {
111 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
112 serverSocket.receive(receivePacket);
113 String sentence = new String(receivePacket.getData());
114 pass=Findpass(sentence);
115
116 int port = receivePacket.getPort();
117 System.out.println("RECEIVED: "+"ip:"+IPAddress
118 +"port:"+port+pass+encryptdecrypt+ALGORITHM);
119 IPAddress = InetAddress.getByName("localhost");
120
121 encryData=cryp(sentence,pass);
122 sendData=encryData.getBytes();
123
124 System.out.println("SEND: "+"ip:"+IPAddress
125 +"port:"+port+encryData);
126
127 DatagramPacket sendPacket =
128 new DatagramPacket(sendData, sendData.length, IPAddress, port);
129 serverSocket.send(sendPacket);
130 }
131
132 } catch (IOException e) {
133 e.printStackTrace();
134} catch (NoSuchAlgorithmException ex) {
135 Logger.getLogger(Server1.class.getName()).log(Level.SEVERE, null, ex);
136 } catch (NoSuchPaddingException ex) {
137 Logger.getLogger(Server1.class.getName()).log(Level.SEVERE, null, ex);
138 } catch (InvalidKeyException ex) {
139 Logger.getLogger(Server1.class.getName()).log(Level.SEVERE, null, ex);
140 } catch (BadPaddingException ex) {
141 Logger.getLogger(Server1.class.getName()).log(Level.SEVERE, null, ex);
142 } catch (IllegalBlockSizeException ex) {
143 Logger.getLogger(Server1.class.getName()).log(Level.SEVERE, null, ex);
144 }
145}
146} ).start();}
147}