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");

        int keySize1 = Integer.parseInt(array[0]);

        String output1 = array[1];
        String algorithm1 = array[2];

        try {
         catch (Exception e) {
         System.out.println("Exception: "+e);

    } private static void getKeysgenerate(int keySize1,String output1,String algorithm1) throws Exception {
      KeyPairGenerator kgone = KeyPairGenerator.getInstance(algorithm1);
      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();

       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();

       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

Recommended Answers

All 2 Replies

I can't figure out why it is not working

Too vague. It's your program, tell us what's going on. Does it not compile? Does it compile, then crash? Does it throw an Exception? Does it run to completion, but produces bad output? If so, what? What command line arguments should we provide? Give us some command line arguments and the output you got and tell us what SHOULD happen. Does the program work properly with some command line arguments, but not others, or does it always fail? Again, be specific (exact error message, line number, input, output, theories about what the problem is or if you have no idea, etc.)

Details, details, details. It saves you and us time and requires fewer messages to get down to the problem, plus describing exactly what the problem is, succinctly yet completely, puts you in the debugging/problem-solving frame of mind. And I've seen more than one individual actually solve his OWN problem in the process of crafting the statement of the "problem" on a forum for others to solve. He thought of a list of things that people trying to help might ask him and experimented with his code in order to pre-answer those questions and found his bug in the process.

As an example of this, as far as what command line arguments are needed, knowing nothing about your program and what it's supposed to do, I immediately zoomed in on the fact that you clearly expect no fewer than four command-line arguments, yet I see an attempt to extract only three of them, at indexes 0, 1, and 2 of your command-line array. In particular, I see no attempt to extract array[3].

I compiled and ran your code with "2 HelloWorld DSA FakeParameter" (the last one to bring the number of arguments up to 4. Line 30 threw an InvalidParameterException. Apparently DSA needs a key size of something other than 2. That's not surprising because I know nothing about this algorithm, including what a good key size is for it, so I picked a random value for the algorithm and the key size. Obviously I guessed wrong.

Why did I guess? Well, I had to because you did not specify what I should have entered.

Now, perhaps I violated the "If you don't know anything about the KeyPairGenerator class and the algorithms/key sizes that are required, ignore the thread and someone who DOES know will answer it" rule. That's true, but very often I and others can answer questions about topics outside of our expertise because something pops out at us or we'll research the question. The larger point is that I answered this thread since no one else has yet and because I think it's worth pointing out the vagueness of your post and ways to correct it for the future. If you had provided command line arguments and pinpointed the error, I would have done some research on encryption, then answered the question, and the odds go up that someone who IS an expert at Java's encryption might have weighed in. It's very likely that THAT person would have asked you what command line arguments to use as a way to guide you rather than just give you the answer (part of the assignment is for you to figure out what command line arguments to use, not just the coding).

So I don't know what the problem is, but come back with a little more detail and I'll give it a shot. But I don't want to spend time researching encryption techniques and have it turn out that the problem was something unrelated. Again, be succinct, but be complete in specifying your problem.

I think best guide for w3schools.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.