Shrikant Karvade
Shrikant Karvade

Reputation: 45

Decryption is not working using RSA

I am encryting a message at client side and decrypting it at server side while decrypting it goes in infinite loop and terminate. Can you guys tell me what's wrong in code.Encryption is working fine but when it decrypt it got stuck and terminate.

AuServer.java

import java.net.*;
import java.io.*;
import java.security.*;
public class AuServer {
    private Socket socket = null;
    private Socket publishingsocket = null;
    private ServerSocket server = null;
    private DataInputStream streamIn = null;
    private String line = null;
    private DataOutputStream streamOut = null;
public static final String PRIVATE_KEY_FILE = "C:/keys/private.key";
  public static final String PUBLIC_KEY_FILE = "C:/keys/public.key";
   static ObjectInputStream inputStream = null;
    public AuServer(int port) {
        try {
            System.out.println("Binding to port " + port + ", please wait  ...");
            server = new ServerSocket(port);
            System.out.println("Server started: " + server);
            System.out.println("Waiting for a client ...");
            socket = server.accept();
            System.out.println("Client accepted: " + socket);
            open();
            boolean done = false;
            while (!done) {
                try {
                    line = streamIn.readUTF();
                    System.out.println(line);
                    try{
                    EncryptionUtil eu1=new EncryptionUtil();
                     if (!eu1.areKeysPresent()) {
         //Method generates a pair of keys using the RSA algorithm and stores it
       // in their respective files
       eu1.generateKey();
     }
   byte[] l=  line.getBytes();
    inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
    final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
      final String plainText = eu1.decrypt(l, privateKey);  


            //  String cdd1=new String(plainText);      



                    done = plainText.equals("exit");
                }
                catch(Exception e) {
                e.printStackTrace();

                } 
                }catch (IOException ioe) {
                    done = true;
                }
                forward(line, 50090);
            }
            close();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    public void forward(String line, int port) {
        try {
            publishingsocket = new Socket("localhost", port);
            streamOut = new DataOutputStream(publishingsocket.getOutputStream());
            streamOut.writeUTF(line);
            streamOut.flush();
        } catch (UnknownHostException uhe) {
            System.out.println("Host unknown: " + uhe.getMessage());
        } catch (IOException ioe) {
            System.out.println("Unexpected exception: " + ioe.getMessage());
        } finally {
            try {
                publishingsocket.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public void open() throws IOException {
        streamIn = new DataInputStream(new BufferedInputStream(
                socket.getInputStream()));
    }

    public void close() throws IOException {
        if (socket != null)
            socket.close();
        if (streamIn != null)
            streamIn.close();
    }

    public static void main(String args[]) {
        @SuppressWarnings("unused")
        AuServer server = null;
        if (args.length != 1)
            System.out.println("Usage: java Server port");
        else
            server = new AuServer(Integer.parseInt(args[0]));
    }
}

Client.java

import java.net.*;
import java.io.*;
import java.security.*;

public class Client extends Thread{
    private String line = null;
    private Socket socket = null;
    private ServerSocket server = null;
    private DataInputStream console = null;
    private DataInputStream streamIn = null;
    private DataOutputStream streamOut = null;
public static final String PRIVATE_KEY_FILE = "C:/keys/private.key";
  public static final String PUBLIC_KEY_FILE = "C:/keys/public.key";
  static ObjectInputStream inputStream = null;
    @SuppressWarnings("deprecation")

    public Client(String serverName, int serverPort) {
        System.out.println("Establishing connection. Please wait ...");
        try {
            socket = new Socket(serverName, serverPort);
            System.out.println("Connected: " + socket);
            starta();
        } catch (UnknownHostException uhe) {
            System.out.println("Host unknown: " + uhe.getMessage());
        } catch (IOException ioe) {
            System.out.println("Unexpected exception: " + ioe.getMessage());
        }
        String line = "";
        while (!line.equals("exit")) {
            try {
                line = console.readLine();
                try {
                     EncryptionUtil  eu=new EncryptionUtil();
      // Check if the pair of keys are present else generate those.
      if (!eu.areKeysPresent()) {
         //Method generates a pair of keys using the RSA algorithm and stores it
       //  in their respective files
        eu.generateKey();
      }
    // Encrypt the string using the public key
     inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
      final PublicKey publicKey = (PublicKey) inputStream.readObject();
      final byte[] cipherText = eu.encrypt(line, publicKey);
      String cdd=new String(cipherText); 


                streamOut.writeUTF(cdd);
                System.out.println(cdd);
                streamOut.flush();
            }  catch (Exception e) {
                e.printStackTrace();
                         }
            }catch (IOException ioe) {
                System.out.println("Sending error: " + ioe.getMessage());
            }

            }


    }
    public Client(int port) {
        try {
            System.out.println("Binding to port " + port + ", please wait  ...");
            server = new ServerSocket(port);
            System.out.println("Server started: " + server);
            System.out.println("Waiting for a client ...");
            socket = server.accept();
            System.out.println("Client accepted: " + socket);
            open();
            boolean done = false;
            while (!done) {
                try {
                    line = streamIn.readUTF();
                    System.out.println(line);
                    done = line.equals("exit");
                } catch (IOException ioe) {
                    done = true;
                }
            }
            close();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }

    public void open() throws IOException {
        streamIn = new DataInputStream(new BufferedInputStream(
                socket.getInputStream()));
    }

    public void close() throws IOException {
        if (socket != null)
            socket.close();
        if (streamIn != null)
            streamIn.close();
    }

    public void starta() throws IOException {
        console = new DataInputStream(System.in);
        streamOut = new DataOutputStream(socket.getOutputStream());
    }

    public void stopa() {
        try {
            if (console != null)
                console.close();
            if (streamOut != null)
                streamOut.close();
            if (socket != null)
                socket.close();
        } catch (IOException ioe) {
            System.out.println("Error closing ...");
        }
    }

    public static void main(String args[]) {
        @SuppressWarnings("unused")
        Client client = null;
        /*if (args.length != 2)
            System.out.println("Usage: java Client host port");
        else*/
        //Thread cl = new Thread(new Client(args[0], Integer.parseInt(args[1])));
        //Thread sr = new Thread(new Client(12000));
        Thread cl = new Client(args[0], Integer.parseInt(args[1]));
        Thread sr = new Client(12000);
        cl.start();
        sr.start();
    }
}

EncryptionUtil.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class EncryptionUtil {
public static final String ALGORITHM = "RSA";

 /**
   * String to hold the name of the private key file.
   */
  public static final String PRIVATE_KEY_FILE = "C:/keys/private.key";

  /**
   * String to hold name of the public key file.
   */
  public static final String PUBLIC_KEY_FILE = "C:/keys/public.key";
  static ObjectInputStream inputStream = null;
public static void generateKey() {
    try {
      final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
      keyGen.initialize(1024);
      final KeyPair key = keyGen.generateKeyPair();

      File privateKeyFile = new File(PRIVATE_KEY_FILE);
      File publicKeyFile = new File(PUBLIC_KEY_FILE);

      // Create files to store public and private key
      if (privateKeyFile.getParentFile() != null) {
        privateKeyFile.getParentFile().mkdirs();
      }
      privateKeyFile.createNewFile();

      if (publicKeyFile.getParentFile() != null) {
        publicKeyFile.getParentFile().mkdirs();
      }
      publicKeyFile.createNewFile();

      // Saving the Public key in a file
      ObjectOutputStream publicKeyOS = new ObjectOutputStream(
          new FileOutputStream(publicKeyFile));
      publicKeyOS.writeObject(key.getPublic());
      publicKeyOS.close();

      // Saving the Private key in a file
      ObjectOutputStream privateKeyOS = new ObjectOutputStream(
          new FileOutputStream(privateKeyFile));
      privateKeyOS.writeObject(key.getPrivate());
      privateKeyOS.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

public static boolean areKeysPresent() {

    File privateKey = new File(PRIVATE_KEY_FILE);
    File publicKey = new File(PUBLIC_KEY_FILE);

    if (privateKey.exists() && publicKey.exists()) {
      return true;
    }
    return false;
  }



 public static byte[] encrypt(String text, PublicKey key) {
    byte[] cipherText = null;
    try {

  // get an RSA cipher object and print the provider
      final Cipher cipher = Cipher.getInstance(ALGORITHM);
      // encrypt the plain text using the public key
      cipher.init(Cipher.ENCRYPT_MODE, key);
      cipherText = cipher.doFinal(text.getBytes());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return cipherText;
  }

  /**
   * Decrypt text using private key.
   * 
   * @param text
   *          :encrypted text
   * @param key
   *          :The private key
   * @return plain text
   * @throws java.lang.Exception
   */
  public static String decrypt(byte[] line, PrivateKey key) {
    byte[] dectyptedText = null;
    try {





     inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
      final PrivateKey privateKey = (PrivateKey) inputStream.readObject();

    final String plainText = decrypt(line, privateKey);

      // get an RSA cipher object and print the provider
      final Cipher cipher = Cipher.getInstance(ALGORITHM);

      // decrypt  text using the private key
      cipher.init(Cipher.DECRYPT_MODE, key);
      dectyptedText = cipher.doFinal(line);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
 return new String(dectyptedText);

}

Upvotes: 0

Views: 885

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 94058

We are not a community debugger, but I'll note one issue that is immediately clear: you are handling ciphertext as being a String. Ciphertext can contain any value, including values that are not UTF-8. So you are loosing data anytime you call new String(ciphertext).

Upvotes: 1

Related Questions