5 changed files with 148 additions and 0 deletions
-
22crypto/CryptoInputStream.java
-
24crypto/CryptoOutputStream.java
-
70crypto/CryptoSocket.java
-
31crypto/W25519.java
-
1crypto/WeierstrassCurve.java
@ -0,0 +1,22 @@ |
|||
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(); |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
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); |
|||
} |
|||
} |
@ -0,0 +1,70 @@ |
|||
package crypto; |
|||
|
|||
import javafx.scene.shape.Ellipse; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.io.OutputStream; |
|||
import java.math.BigInteger; |
|||
import java.net.Socket; |
|||
import java.util.Random; |
|||
|
|||
public class CryptoSocket |
|||
{ |
|||
private Socket socket; |
|||
private OutputStream out; |
|||
private InputStream in; |
|||
|
|||
public CryptoSocket(Socket socket) throws IOException |
|||
{ |
|||
this.socket = socket; |
|||
this.out = socket.getOutputStream(); |
|||
this.in = socket.getInputStream(); |
|||
|
|||
WeierstrassCurve w25519 = W25519.getW25519(); |
|||
WeierstrassPoint g = W25519.getGenerator(); |
|||
BigInteger x = new BigInteger(w25519.mod.bitLength(), new Random()); |
|||
|
|||
// 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); |
|||
|
|||
BigInteger newx = receiveBigInteger(in); |
|||
BigInteger newy = receiveBigInteger(in); |
|||
WeierstrassPoint p = new WeierstrassPoint(w25519, newx, newy); |
|||
|
|||
WeierstrassPoint c = p.mult(x); |
|||
|
|||
System.out.println("result at: " + c.x.toString() + ", " + c.y.toString()); |
|||
} |
|||
|
|||
private void sendBigInteger(OutputStream o, BigInteger i) throws java.io.IOException |
|||
{ |
|||
byte[] ba = i.toByteArray(); |
|||
|
|||
if (ba.length >= 32768) |
|||
throw new IOException("Integer too large to send over a socket"); |
|||
|
|||
o.write((byte) (ba.length >> 8)); |
|||
o.write((byte) ba.length); |
|||
|
|||
for (byte b : ba) |
|||
o.write(b); |
|||
} |
|||
|
|||
private BigInteger receiveBigInteger(InputStream i) throws java.io.IOException |
|||
{ |
|||
int l = i.read(); |
|||
l = l << 8; |
|||
l = l | i.read(); |
|||
|
|||
byte[] bytes = new byte[l]; |
|||
for (int ii = 0; ii < l; ii++) |
|||
{ |
|||
bytes[ii] = (byte) i.read(); |
|||
} |
|||
|
|||
return new BigInteger(bytes); |
|||
} |
|||
} |
@ -0,0 +1,31 @@ |
|||
package crypto; |
|||
|
|||
import java.math.BigInteger; |
|||
|
|||
public class W25519 |
|||
{ |
|||
static WeierstrassCurve w25519 = null; |
|||
static WeierstrassPoint generator = null; |
|||
|
|||
static WeierstrassCurve getW25519() |
|||
{ |
|||
if (w25519 == null) |
|||
{ |
|||
w25519 = new WeierstrassCurve(new BigInteger("19298681539552699237261830834781317975544997444273427339909597334573241639236"), |
|||
new BigInteger("55751746669818908907645289078257140818241103727901012315294400837956729358436"), |
|||
new BigInteger("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed", 16)); |
|||
} |
|||
return w25519; |
|||
} |
|||
|
|||
static WeierstrassPoint getGenerator() |
|||
{ |
|||
if (generator == null) |
|||
{ |
|||
generator = new WeierstrassPoint(getW25519(), |
|||
new BigInteger("19298681539552699237261830834781317975544997444273427339909597334652188435546"), |
|||
new BigInteger("43114425171068552920764898935933967039370386198203806730763910166200978582548")) |
|||
} |
|||
return generator; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue