Browse Source

Moving to a different machine

crypto
Thomas Johnson 2 years ago
parent
commit
7555b52417
  1. 5
      crypto/CryptoOutputSocket.java
  2. 63
      crypto/InterleavedOutput.java
  3. 38
      sample/Main.java

5
crypto/CryptoOutputSocket.java

@ -0,0 +1,5 @@
package crypto;
public class CryptoOutputSocket
{
}

63
crypto/InterleavedOutput.java

@ -0,0 +1,63 @@
package crypto;
import java.io.IOException;
import java.io.OutputStream;
public class InterleavedOutput
{
private byte length;
private byte buffers[][];
private int heads[];
private byte nstreams;
private byte buffer;
private OutputStream underlying;
public InterleavedOutput(OutputStream underlying, byte length, byte nstreams)
{
this.underlying = underlying;
this.length = length;
this.nstreams = nstreams;
this.buffers = new byte[Byte.toUnsignedInt(nstreams)][Byte.toUnsignedInt(length)];
}
synchronized void writeBuffer(byte which) throws IOException
{
underlying.write(which);
underlying.write(buffers[Byte.toUnsignedInt(which)]);
}
void writeTo(byte which, byte v) throws IOException
{
int idx = Byte.toUnsignedInt(which);
buffers[idx][heads[idx]] = v;
if (heads[idx] == length)
{
writeBuffer(which);
heads[idx] = 0;
}
}
public PaddedOutputStream getStream(byte which) throws IOException
{
if (Byte.toUnsignedInt(which) >= nstreams)
throw new IOException("Stream number too high");
return new PaddedOutputStream(new InterleavedOutputStream(which), length);
}
class InterleavedOutputStream extends OutputStream
{
private byte which;
public InterleavedOutputStream(byte which)
{
this.which = which;
}
@Override
public void write(int i) throws IOException
{
writeTo(which, (byte) i);
}
}
}

38
sample/Main.java

@ -1,6 +1,8 @@
package sample;
import crypto.CryptoSocket;
import crypto.PaddedInputStream;
import crypto.PaddedOutputStream;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
@ -15,36 +17,44 @@ import java.net.Socket;
public class Main
{
public static void main(String[] args) {
try
Object lock = new Object();
try
{
ServerSocket sock = new ServerSocket(25519);
ServerSocket ssock = new ServerSocket(25519);
Socket s = new Socket("127.0.0.1", 25519);
System.out.println("launching other thread");
new Thread(new Runnable() { public void run() { try
{
CryptoSocket csock = new CryptoSocket(sock.accept());
DataOutputStream out = csock.getOutputStream();
DataInputStream in = csock.getInputStream();
out.writeUTF("server->client");
//CryptoSocket csock = new CryptoSocket(sock.accept());
Socket sock = ssock.accept();
System.out.println("connection accepted");
DataOutputStream out = new DataOutputStream(new PaddedOutputStream(sock.getOutputStream(), (byte) 32));
//PaddedOutputStream out = new PaddedOutputStream(sock.getOutputStream(), (byte) 32);
DataInputStream in = new DataInputStream(new PaddedInputStream(sock.getInputStream(), (byte) 32));
out.writeUTF("server->client KDKDKDDKKDKDKDKDKDDDKDKDKKDKDDKDKDKDKDKKDKDKDKDKDKDKKDKDKDKDKDKDKDK");
out.flush();
synchronized (System.out)
synchronized (lock)
{
System.out.print("server: ");
System.out.println(in.readUTF());
}
} catch (Exception e) {} } }).start();
System.out.println("done; connecting socket");
CryptoSocket csock = new CryptoSocket(s);
DataOutputStream out = csock.getOutputStream();
DataInputStream in = csock.getInputStream();
out.writeUTF("client->server");
//CryptoSocket csock = new CryptoSocket(s);
DataOutputStream out = new DataOutputStream(new PaddedOutputStream(s.getOutputStream(), (byte) 32));
DataInputStream in = new DataInputStream(new PaddedInputStream(s.getInputStream(), (byte) 32));
out.writeUTF("client->server REEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
out.flush();
synchronized (System.out)
synchronized (lock)
{
System.out.println("client: ");
System.out.print("client: ");
System.out.println(in.readUTF());
System.out.println("done printing");
// while (true)
// System.out.println("byte: " + in.read());
}
System.out.println("done");
//System.out.println("done");
} catch (IOException e)
{
e.printStackTrace();

Loading…
Cancel
Save