使用RSA解密不起作用

时间:2013-11-18 06:29:17

标签: java encryption

我正在加密客户端的消息并在服务器端解密它,而解密它则进入无限循环并终止。你能告诉我代码中有什么问题。加密工作正常但是当它解密时它会卡住并终止。

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

}

1 个答案:

答案 0 :(得分:1)

我们不是社区调试器,但我会注意到一个问题很明显:您将密文视为String。密文可以包含任何值,包括非UTF-8的值。因此,只要您拨打new String(ciphertext),就会丢失数据。