Browse Source

Finished (?) crypto

crypto
Thomas Johnson 3 years ago
parent
commit
57c3189cae
  1. 22
      crypto/CryptoInputStream.java
  2. 24
      crypto/CryptoOutputStream.java
  3. 60
      crypto/CryptoSocket.java
  4. 2
      crypto/W25519.java

22
crypto/CryptoInputStream.java

@ -1,22 +0,0 @@
package crypto;
import java.io.IOException;
import java.io.InputStream;
import java.security.Key;
public class CryptoInputStream extends InputStream
{
private InputStream underlying;
private Key key;
public CryptoInputStream(InputStream underlying, Key key)
{
this.underlying = underlying;
this.key = key;
}
@Override
public int read() throws IOException {
return underlying.read();
}
}

24
crypto/CryptoOutputStream.java

@ -1,24 +0,0 @@
package crypto;
import java.io.IOException;
import java.io.OutputStream;
import java.security.Key;
// TODO
public class CryptoOutputStream extends OutputStream
{
private OutputStream underlying;
private Key key;
public CryptoOutputStream(OutputStream underlying, Key key)
{
this.underlying = underlying;
this.key = key;
}
@Override
public void write(int i) throws IOException {
underlying.write(i);
}
}

60
crypto/CryptoSocket.java

@ -2,11 +2,14 @@ package crypto;
import javafx.scene.shape.Ellipse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.math.BigInteger;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
public class CryptoSocket
@ -14,6 +17,9 @@ public class CryptoSocket
private Socket socket;
private OutputStream out;
private InputStream in;
SecretKey key;
Cipher ciphout;
Cipher ciphin;
public CryptoSocket(Socket socket) throws IOException
{
@ -27,8 +33,8 @@ public class CryptoSocket
// Heaven forbid there's an identity point in either of these transmissions; it will be fatal anyway, so ignore the possibility.
WeierstrassPoint i = g.mult(x);
sendBigInteger(out, g.x);
sendBigInteger(out, g.y);
sendBigInteger(out, i.x);
sendBigInteger(out, i.y);
BigInteger newx = receiveBigInteger(in);
BigInteger newy = receiveBigInteger(in);
@ -36,7 +42,49 @@ public class CryptoSocket
WeierstrassPoint c = p.mult(x);
System.out.println("result at: " + c.x.toString() + ", " + c.y.toString());
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e)
{
throw new IOException("NoSuchAlgorithmException: " + e.getMessage());
}
md.update(c.x.toByteArray());
md.update(c.y.toByteArray());
byte[] keybytes = md.digest();
this.key = new SecretKeySpec(keybytes, "AES");
try {
this.ciphout = Cipher.getInstance("AES/OFB/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
throw new IOException("NoSuchAlgorithmException: " + e.getMessage());
} catch (NoSuchPaddingException e) {
throw new IOException("NoSuchPaddingException: " + e.getMessage());
}
try {
this.ciphout.init(Cipher.ENCRYPT_MODE, this.key);
this.ciphin.init(Cipher.DECRYPT_MODE, this.key);
} catch (InvalidKeyException e)
{
throw new IOException(e.getMessage());
}
this.out = this.socket.getOutputStream();
this.in = this.socket.getInputStream();
}
public DataOutputStream getOutputStream()
{
return new DataOutputStream(new CipherOutputStream(this.out, ciphout));
}
public DataInputStream getInputStream()
{
return new DataInputStream(new CipherInputStream(this.in, ciphin));
}
private void sendBigInteger(OutputStream o, BigInteger i) throws java.io.IOException

2
crypto/W25519.java

@ -24,7 +24,7 @@ public class W25519
{
generator = new WeierstrassPoint(getW25519(),
new BigInteger("19298681539552699237261830834781317975544997444273427339909597334652188435546"),
new BigInteger("43114425171068552920764898935933967039370386198203806730763910166200978582548"))
new BigInteger("43114425171068552920764898935933967039370386198203806730763910166200978582548"));
}
return generator;
}

Loading…
Cancel
Save