Java - 使用私钥解密邮件

时间:2017-11-02 23:39:40

标签: java sockets rsa

我一直在开发一个允许服务器和客户端之间进行通信的java应用程序。服务器和客户端都将生成公钥和私钥,然后将公钥发送给彼此。然后程序使用该公钥加密和发送消息。但是,尝试使用私钥解密这些消息时会出现问题。有谁知道如何解决这个问题?

以下是相关课程的代码:

 package com.company;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class network {

    static ServerSocket s;
    static Socket s1;
    static OutputStream s1out;
    static DataOutputStream dos;
    static InputStream s1in;

    static chatWindowScreen gui = new chatWindowScreen();
    public static aystmmetricEncryption rsa = new aystmmetricEncryption();

    static PublicKey publicKey;
    static PrivateKey privateKey;

    static String st;

    public static void serverSetup(String port) throws Exception {

        int portInt = Integer.parseInt(port);
        s = new ServerSocket(portInt);
        s1 = s.accept();

        rsa.generateKeys();
        privateKey = rsa.getPrivateKey();
        publicKey = serverKeys();
        System.out.println(publicKey);

    }

    public static PublicKey serverKeys() throws IOException, ClassNotFoundException {

        ObjectOutputStream ob0ut = new ObjectOutputStream(s1.getOutputStream());
        ObjectInputStream obIn = new ObjectInputStream(s1.getInputStream());

        ob0ut.writeObject(rsa.getPublicKey());
        ob0ut.flush();
        Object obj = obIn.readObject();
        PublicKey publicKey = (PublicKey) obj;

        return publicKey;

    }

    public static void clientSetup(String port, String ip) throws Exception {

        int portInt = Integer.parseInt(port);
        s1 = new Socket(ip, portInt);

        rsa.generateKeys();
        publicKey = clientKeys();
        privateKey = rsa.getPrivateKey();
        System.out.println(publicKey);

    }

    public static PublicKey clientKeys() throws IOException, ClassNotFoundException {

        ObjectOutputStream ob0ut = new ObjectOutputStream(s1.getOutputStream());
        ObjectInputStream obIn = new ObjectInputStream(s1.getInputStream());

        Object obj = obIn.readObject();
        PublicKey publicKey = (PublicKey) obj;
        ob0ut.writeObject(rsa.getPublicKey());
        ob0ut.flush();

        return publicKey;

    }

    public static void closeConnection() throws IOException {

        dos.close();
        s1out.close();
        s1.close();

    }

    public static void sendMsg(String message) throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {

        s1out = s1.getOutputStream();
        dos = new DataOutputStream(s1out);

        /*Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String t1 = sdf.format(cal.getTime());*/

        message = rsa.encryptMessage(message,publicKey);

        dos.writeUTF(message);

        gui.sent.append(message);
        gui.received.append("\n");

    }

    public static void receiveMsg() throws IOException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException {

        s1in = s1.getInputStream();
        DataInputStream dis = new DataInputStream(s1in);
        st = (dis.readUTF());

        String st1 = rsa.decryptMessage(privateKey,st);

        System.out.println(st1);
        System.out.println(rsa.decryptMessage(privateKey,st));

        gui.received.append(st1);
        gui.sent.append("\n");
        st = null;


    }

}

    package com.company;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class aystmmetricEncryption {

    static PrivateKey privateKey;
    static PublicKey publicKey;

    public static void generateKeys() throws Exception {

        Map<String, Object> keys = getRSAKeys();
        privateKey = (PrivateKey) keys.get("private");
        publicKey = (PublicKey) keys.get("public");

    }

    private static Map<String, Object> getRSAKeys() throws Exception {

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();
        Map<String, Object> keys = new HashMap<String, Object>();
        keys.put("private", privateKey);
        keys.put("public", publicKey);

        return keys;

    }

    public static String encryptMessage(String plainText, PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes()));

    }

    public static String decryptMessage(PrivateKey privateKey, String encryptedText) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        return new String(cipher.doFinal(Base64.getDecoder().decode(encryptedText)));

    }

    public PublicKey getPublicKey() {

        return publicKey;

    }

    public PrivateKey getPrivateKey() {

        return privateKey;

    }
}

例外的Stacktrace:

Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
    at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at com.company.aystmmetricEncryption.decryptMessage(aystmmetricEncryption.java:54)
    at com.company.network.receiveMsg(network.java:120)
    at com.company.Main.server(Main.java:121)
    at com.company.Main.contacts(Main.java:104)
    at com.company.Main.main(Main.java:16)

0 个答案:

没有答案