Created
February 21, 2014 17:03
-
-
Save Sarkie/9138396 to your computer and use it in GitHub Desktop.
Sending Java Sealed Objects via Sockets
This file contains 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
import java.net.InetSocketAddress; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import javax.crypto.*; | |
import javax.crypto.spec.*; | |
import java.io.*; | |
import java.security.spec.*; | |
import java.security.*; | |
public class Client { | |
public static void main(String[] args) throws Exception { | |
String server = "localhost"; | |
if(args.length == 1){ | |
server = args[0]; | |
} | |
System.out.println("Receiver Started"); | |
// Create key | |
final char[] password = "secret_password".toCharArray(); | |
final byte[] salt = "random_salt".getBytes(); | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | |
KeySpec spec = new PBEKeySpec(password, salt, 1024, 128); | |
SecretKey tmp = factory.generateSecret(spec); | |
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, secret); | |
SocketChannel sChannel = SocketChannel.open(); | |
sChannel.configureBlocking(true); | |
if (!sChannel.connect(new InetSocketAddress(server, 12345))) { | |
System.out.println("Cannot connect to Server, make sure it is running"); | |
} | |
ObjectOutputStream oos = new ObjectOutputStream(sChannel.socket().getOutputStream()); | |
SecretObject secretObject = new SecretObjectImpl("007"); | |
SealedObject so = new SealedObject(secretObject, cipher); | |
oos.writeObject(so); | |
System.out.println("Sent Sealed Object"); | |
oos.close(); | |
System.out.println("End Receiver"); | |
} | |
} |
This file contains 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
import java.io.*; | |
public interface SecretObject extends Serializable { | |
String getSecretMessage(); | |
} |
This file contains 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
import java.io.*; | |
public class SecretObjectImpl implements SecretObject { | |
private String _secretMessage = ""; | |
public SecretObjectImpl(String secretMessage) { | |
_secretMessage = secretMessage; | |
} | |
public String getSecretMessage() { | |
return _secretMessage; | |
} | |
} |
This file contains 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
import java.net.InetSocketAddress; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import javax.crypto.*; | |
import javax.crypto.spec.*; | |
import java.io.*; | |
import java.security.spec.*; | |
import java.security.*; | |
public class Server { | |
public static void main(String[] args) throws Exception { | |
System.out.println("Server Started"); | |
// Create key | |
final char[] password = "secret_password".toCharArray(); | |
final byte[] salt = "random_salt".getBytes(); | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | |
KeySpec spec = new PBEKeySpec(password, salt, 1024, 128); | |
SecretKey tmp = factory.generateSecret(spec); | |
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); | |
Cipher dcipher = Cipher.getInstance("AES"); | |
dcipher.init(Cipher.DECRYPT_MODE, secret); | |
ServerSocketChannel ssChannel = ServerSocketChannel.open(); | |
ssChannel.configureBlocking(true); | |
int port = 12345; | |
ssChannel.socket().bind(new InetSocketAddress(port)); | |
while (true) { | |
System.out.println("Waiting for a connection..."); | |
SocketChannel sChannel = ssChannel.accept(); | |
System.out.println(sChannel.getRemoteAddress().toString() +" connected"); | |
ObjectInputStream ois = new ObjectInputStream(sChannel.socket().getInputStream()); | |
SealedObject s = (SealedObject)ois.readObject(); | |
SecretObject decryptedSecretObject = (SecretObject) s.getObject(dcipher); | |
System.out.println("Server - Packet Data is: '" + decryptedSecretObject.getSecretMessage() + "'"); | |
ois.close(); | |
System.out.println("Connection ended"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment