Write a program to generate a pair of private and public keys. You program should also be able to encrypt and decrypt messages using the public and private keys.
here is my program:
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.interfaces.*;
class generateKeyPair {
public static void main(String[] array){
if (array.length<4){
System.out.println("The KeySize output is" +" algorithm");
return;
}
int keySize1 = Integer.parseInt(array[0]);
String output1 = array[1];
String algorithm1 = array[2];
try {
getKeysgenerate(keySize1,output1,algorithm1);
}
catch (Exception e) {
System.out.println("Exception: "+e);
return;
}
} private static void getKeysgenerate(int keySize1,String output1,String algorithm1) throws Exception {
KeyPairGenerator kgone = KeyPairGenerator.getInstance(algorithm1);
kgone.initialize(keySize1);
System.out.println();
System.out.println("KeyPairGenerator Object Info is the following: ");
System.out.println("The Algorithm = "+kgone.getAlgorithm());
System.out.println("The Provider = "+kgone.getProvider());
System.out.println("The Key Size is = "+keySize1);
System.out.println("toString function = "+kgone.toString());
KeyPair pairone = kgone.generateKeyPair();
PrivateKey privateKey = pairone.getPrivate();
PublicKey publicKey = pairone.getPublic();
String file1 = output1+".private";
FileOutputStream out = new FileOutputStream(file1);
byte[] kyone = privateKey.getEncoded();
out.write(kyone);
out.close();
System.out.println();
System.out.println("The Private Key Information is: ");
System.out.println("The Algorithm = "+privateKey.getAlgorithm());
System.out.println(" The Saved File is = "+file1);
System.out.println(" The Size = "+kyone.length);
System.out.println("The Format = "+privateKey.getFormat());
System.out.println("toString functon = "+privateKey.toString());
file1 = output1+".public";
out = new FileOutputStream(file1);
kyone = publicKey.getEncoded();
out.write(kyone);
out.close();
System.out.println();
System.out.println(" ThePublic Key Infotmation is : ");
System.out.println("The Algorithm = "+publicKey.getAlgorithm());
System.out.println("The Saved File is = "+file1);
System.out.println("The Size = "+kyone.length);
System.out.println(" The Format = "+publicKey.getFormat());
System.out.println("toString = "+publicKey.toString());
}
}
I can't figure out why it is not working