|
|||||||||
Home >> All >> com >> ssttr >> [ crypto overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: ![]() ![]() ![]() |
DETAIL: FIELD | CONSTR | METHOD |
com.ssttr.crypto
Class DH

java.lang.Objectcom.ssttr.crypto.DH
- public class DH
- extends java.lang.Object
This class performs the basic essential functions for Diffie-Hellman key genereration.
Specifily, given a generator (g), exponent (x), and prime modulus (m), it performs:
gx mod m
. Of course this could also be done just as easily
with the java.math.BigInteger.modPow() method, but that class is rarely included
in J2ME and embeded JVMs this can still be usefull.
It is worth noting that this class has been tested to reliably work on key sizes >= 64 bits. It will often fail on a keys < 64 bits. This could (in theory) be fixed but since key sizes less than 64 bits are generally considered worthless (due to the ability to brute force the entire keyspace in short order) this has been classified as a featurenotbug.
To generate a shared private key:
Jack performs:
int keySize = 128; DH dh = new DH(keySize); Random rnd = new Random(); String gen = "3"; String jacksExp = Long.toString( Math.abs(rnd.nextLong()), 16 ).toUpperCase(); String prime = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF61"; String publicKey = dh.generateKey( gen, jacksExp, prime);
Then sends keySize, gen, prime, and publicKey to Jill (but exp MUST be kept secret!).
Then Jill:
DH dh = new DH(jacksKeySize); Random rnd = new Random(); String jillsExp = Long.toString( Math.abs(rnd.nextLong()), 16 ).toUpperCase(); String publicKey = dh.generateKey( jacksGen, jillsExp, jacksPrime); String jillsSecretKey = dh.generateKey( jacksPublicKey, jillsExp, jacksPrime);
And sends publicKey to Jack.
Who then:
String jacksSecretKey = dh.generateKey( jillsPublicKey, jacksExp, prime);Now if everything worked properly (and it should have) jacksSecretKey == jillsSecretKey and they can now use a shared-secret algorithm (like TEA) to communicate.
Most of the code for this class was blatently stollen (and subsequently ported to Java) from http://www.cypherspace.org/~adam/rsa/dh-in-C.html. Which is copied here in case the page goes down:
Field Summary | |
(package private) byte[] |
b
|
(package private) int |
d
|
(package private) byte[] |
e
|
(package private) byte[] |
g
|
(package private) byte[] |
key
|
(package private) int |
keySize
|
(package private) java.lang.Object |
lock
|
(package private) byte[] |
m
|
(package private) int |
n
|
(package private) int |
S
|
(package private) byte[] |
tmp
|
(package private) int |
v
|
(package private) int |
z
|
Constructor Summary | |
DH(int keySize)
Creates a new instance of DH. |
Method Summary | |
private void |
a(byte[] x,
byte[] y,
int o)
|
private void |
bzero(byte[] a,
int len)
|
private void |
generateKey()
|
byte[] |
generateKey(byte[] generator,
byte[] exponent,
byte[] prime)
|
java.lang.String |
generateKey(java.lang.String generator,
java.lang.String exponent,
java.lang.String prime)
|
private void |
h(byte[] x,
byte[] y)
|
private void |
initialize()
|
private void |
M(byte[] x,
byte[] y)
|
private void |
r(byte[] x)
|
private void |
s(byte[] x)
|
private static byte[] |
trim(byte[] bytes)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
lock
java.lang.Object lock
m
byte[] m
g
byte[] g
e
byte[] e
b
byte[] b
key
byte[] key
tmp
byte[] tmp
n
int n
v
int v
d
int d
z
int z
S
int S
keySize
int keySize
Constructor Detail |
DH
public DH(int keySize)
- Creates a new instance of DH. Each instance of DH is threadsafe in that
it uses a lock to guarentee that all calls to generateKey() are serialized.
Method Detail |
initialize
private final void initialize()
generateKey
public java.lang.String generateKey(java.lang.String generator, java.lang.String exponent, java.lang.String prime)
generateKey
public byte[] generateKey(byte[] generator, byte[] exponent, byte[] prime)
generateKey
private final void generateKey()
s
private void s(byte[] x)
r
private void r(byte[] x)
M
private void M(byte[] x, byte[] y)
a
private void a(byte[] x, byte[] y, int o)
h
private void h(byte[] x, byte[] y)
bzero
private void bzero(byte[] a, int len)
trim
private static byte[] trim(byte[] bytes)
|
|||||||||
Home >> All >> com >> ssttr >> [ crypto overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: ![]() ![]() ![]() |
DETAIL: FIELD | CONSTR | METHOD |