G

 2a)


Program:

package java_cryptography;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.SecureRandom;

import javax.xml.bind.DatatypeConverter;

public class Asymmetric

{

private static final String RSA= "RSA";

public static KeyPair generateRSAKkeyPair()

throws Exception

{

SecureRandom secureRandom = new SecureRandom();

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA);

keyPairGenerator.initialize(2048, secureRandom);

return keyPairGenerator.generateKeyPair();

}

public static void main(String args[])

throws Exception

{ KeyPair keypair = generateRSAKkeyPair();

System.out.println("Public Key is: " + DatatypeConverter.printHexBinary(

keypair.getPublic().getEncoded()));

System.out.println("Private Key is: " + DatatypeConverter.printHexBinary(

keypair.getPrivate().getEncoded()));

}

}


2b)


Program:

class DiffieHellman

{

private static long power(long a, long b, long p)

{

if (b == 1)

return a;

else

return (((long)Math.pow(a, b)) % p);

}

public static void main(String[] args)

{

long P, G, x, a, y, b, ka, kb;

P = 23;

System.out.println("The value of P:" + P);

G = 9;

System.out.println("The value of G:" + G);

a = 4;

System.out.println("The private key a for Alice:" + a);

x = power(G, a, P);

b = 3;

System.out.println("The private key b for Bob:" + b);

y = power(G, b, P);

ka = power(y, a, P);

kb = power(x, b, P);

System.out.println("Secret key for the Alice is:" + ka);

System.out.println("Secret key for the Bob is:" + kb);

}

}

Comments