Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.*;
- import java.util.*;
- class DHKey {
- BigInteger value;
- DHKey (BigInteger val) {
- value = val;
- }
- }
- class DHData {
- BigInteger value;
- DHData (BigInteger val) {
- this.value = val;
- }
- public static DHData init(BigInteger val) {
- return new DHData(val);
- }
- }
- class DHConfig {
- BigInteger p;
- BigInteger g;
- DHConfig () {
- Random r = new Random();
- this.p = BigInteger.probablePrime(100, r);
- this.g = BigInteger.probablePrime(100, r);
- }
- public static DHConfig init () {
- return new DHConfig();
- }
- }
- class DHClient {
- private DHKey privateKey;
- public DHKey publicKey;
- private DHConfig config;
- DHClient (DHConfig config) {
- this.config = config;
- this.privateKey = new DHKey (new BigInteger(4, new Random()));
- this.publicKey = new DHKey (this.modPow(config.g, this.config.p, this.privateKey.value));
- }
- public DHKey getPublicKey () {
- return this.publicKey;
- }
- public DHData decodeKey (DHKey receivedKey) {
- return DHData.init(this.modPow(receivedKey.value, this.config.p, this.privateKey.value));
- }
- public BigInteger modPow (BigInteger first, BigInteger second, BigInteger exp) {
- return first.modPow(exp, second);
- }
- }
- class DHDriver {
- public static void main(String[] args) {
- DHConfig config = DHConfig.init();
- DHClient one = new DHClient(config);
- DHClient two = new DHClient(config);
- System.out.println(two.decodeKey(one.getPublicKey()).value);
- System.out.println(one.decodeKey(two.getPublicKey()).value);
- }
- }
Add Comment
Please, Sign In to add comment