6 public class Curve25519Donna {
8 final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
10 public static String bytesToHex(byte[] bytes) {
11 char[] hexChars = new char[bytes.length * 2];
13 for ( int j = 0; j < bytes.length; j++ ) {
15 hexChars[j * 2] = hexArray[v >>> 4];
16 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
18 return new String(hexChars);
21 public native byte[] curve25519Donna(byte[] a, byte[] b);
22 public native byte[] makePrivate(byte[] secret);
23 public native byte[] getPublic(byte[] privkey);
24 public native byte[] makeSharedSecret(byte[] privkey, byte[] theirPubKey);
25 public native void helowrld();
27 // Uncomment if your Java is 32-bit:
28 //static { System.loadLibrary("Curve25519Donna"); }
30 // Otherwise, load this 64-bit .jnilib:
31 static { System.loadLibrary("Curve25519Donna_64"); }
34 To give the old tires a kick (OSX):
35 java -cp `pwd` Curve25519Donna
37 public static void main (String[] args) {
39 Curve25519Donna c = new Curve25519Donna();
41 // These should be 32 bytes long
42 byte[] user1Secret = "abcdefghijklmnopqrstuvwxyz123456".getBytes();
43 byte[] user2Secret = "654321zyxwvutsrqponmlkjihgfedcba".getBytes();
46 // You can use the curve function directly...
48 //byte[] o = c.curve25519Donna(a, b);
49 //System.out.println("o = " + bytesToHex(o));
52 // ... but it's not really necessary. Just use the following
53 // convenience methods:
55 byte[] privKey = c.makePrivate(user1Secret);
56 byte[] pubKey = c.getPublic(privKey);
58 byte[] privKey2 = c.makePrivate(user2Secret);
59 byte[] pubKey2 = c.getPublic(privKey2);
61 System.out.println("'user1' privKey = " + bytesToHex(privKey));
62 System.out.println("'user1' pubKey = " + bytesToHex(pubKey));
63 System.out.println("===================================================");
65 System.out.println("'user2' privKey = " + bytesToHex(privKey2));
66 System.out.println("'user2' pubKey = " + bytesToHex(pubKey2));
67 System.out.println("===================================================");
70 byte[] ss1 = c.makeSharedSecret(privKey, pubKey2);
71 System.out.println("'user1' computes shared secret: " + bytesToHex(ss1));
73 byte[] ss2 = c.makeSharedSecret(privKey2, pubKey);
74 System.out.println("'user2' computes shared secret: " + bytesToHex(ss2));