Last active
November 13, 2016 19:03
-
-
Save lnlonSA/01d1a7780848356d96826bb90da0ccfc to your computer and use it in GitHub Desktop.
وظيفة آمن نظم المعلومات - المرحلة1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package bitcion; | |
import java.security.*; | |
import java.security.spec.InvalidKeySpecException; | |
import javax.crypto.*; | |
import javax.crypto.spec.SecretKeySpec; | |
import sun.misc.*; | |
/** | |
* | |
* @author LaNa Sa | |
*/ | |
public class AES { | |
private static final String ALGO = "AES"; | |
public static String encrypt(String Data,byte[] keyValue) throws Exception { | |
Key key = generateKey(keyValue); | |
Cipher c = Cipher.getInstance(ALGO); | |
c.init(Cipher.ENCRYPT_MODE, key); | |
byte[] encVal = c.doFinal(Data.getBytes()); | |
String encryptedValue = new BASE64Encoder().encode(encVal); | |
return encryptedValue; | |
} | |
public static String decrypt(String encryptedData,byte[] keyValue) throws Exception { | |
Key key = generateKey(keyValue); | |
Cipher c = Cipher.getInstance(ALGO); | |
c.init(Cipher.DECRYPT_MODE, key); | |
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); | |
byte[] decValue = c.doFinal(decordedValue); | |
String decryptedValue = new String(decValue); | |
return decryptedValue; | |
} | |
private static Key generateKey(byte[] keyValue) throws Exception { | |
Key key = new SecretKeySpec(keyValue, ALGO); | |
return key; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package bitcion; | |
import java.io.DataInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.Socket; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author LaNa Sa | |
*/ | |
public class Attacker { | |
public static void main(String[] argc) | |
{ | |
String server_name="localhost"; | |
int port_number=6000; | |
System.out.println("Connecting to "+server_name+" on port "+port_number); | |
try { | |
Socket client = new Socket(server_name,port_number); | |
System.out.println(client.getRemoteSocketAddress()); | |
InputStream fromServer = client.getInputStream(); | |
DataInputStream in = new DataInputStream(fromServer); | |
String recvData=in.readUTF().toString(); | |
System.out.println("Query Result :\n"+recvData); | |
client.close(); | |
} catch (IOException ex) { | |
Logger.getLogger(ClientClass.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (Exception ex) { | |
Logger.getLogger(ClientClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package bitcion; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.Socket; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author LaNa Sa | |
*/ | |
public class ClientClass { | |
private static final byte[] secretKeyValue = | |
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', | |
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; | |
public static void main(String[] argc) | |
{ | |
String server_name="localhost"; | |
int port_number=6000; | |
System.out.println("Connecting to "+server_name+" on port "+port_number); | |
try { | |
Socket client = new Socket(server_name,port_number); | |
System.out.println(client.getRemoteSocketAddress()); | |
InputStream fromServer = client.getInputStream(); | |
DataInputStream in = new DataInputStream(fromServer); | |
String recvData=in.readUTF().toString(); | |
recvData = AES.decrypt(recvData,secretKeyValue); | |
System.out.println("Query Result :\n"+recvData); | |
client.close(); | |
} catch (IOException ex) { | |
Logger.getLogger(ClientClass.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (Exception ex) { | |
Logger.getLogger(ClientClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package bitcion; | |
/** | |
* | |
* @author LaNa Sa | |
*/ | |
import com.sun.corba.se.spi.activation.Server; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.net.*; | |
import java.io.*; | |
public class ServerClass extends Thread{ | |
private ServerSocket serverSocket; | |
private static final byte[] secretKeyValue = | |
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', | |
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; | |
public ServerClass(int port) throws IOException { | |
serverSocket = new ServerSocket(port); | |
} | |
public void run() { | |
while(true) { | |
try { | |
System.out.println("Waiting for client on port " + | |
serverSocket.getLocalPort() + "..."); | |
Socket server = serverSocket.accept(); | |
System.out.println("Just connected to " + server.getRemoteSocketAddress()); | |
String sendData = AES.encrypt(getClientTableQuery(),secretKeyValue); | |
DataOutputStream out = new DataOutputStream(server.getOutputStream()); | |
out.writeUTF(sendData); | |
server.close(); | |
}catch(SocketTimeoutException s) { | |
System.out.println("Socket timed out!"); | |
break; | |
}catch(IOException e) { | |
e.printStackTrace(); | |
break; | |
} catch (Exception ex) { | |
Logger.getLogger(ServerClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} | |
public String getClientTableQuery() | |
{ | |
String result=""; | |
String host = "jdbc:derby://localhost:1527/Bitcion"; | |
String username = "bitcion"; | |
String password = "bitcion"; | |
try { | |
Connection con = DriverManager.getConnection( host, username, password ); | |
Statement stmt = con.createStatement(); | |
String SQL = "SELECT * FROM CLIENTS"; | |
ResultSet rs = stmt.executeQuery(SQL); | |
while(rs.next()) | |
{ | |
result +="Client IP :"+rs.getString("IP")+'\t'; | |
result +="Account Value :"+rs.getString("VALUE")+'\n'; | |
} | |
} catch (SQLException ex) { | |
Logger.getLogger(ServerClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
try { | |
ServerClass server = new ServerClass(6000); | |
server.start(); | |
// System.out.println(server.getClientTableQuery()); | |
} catch (IOException ex) { | |
Logger.getLogger(ServerClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment