java - 使用 RSA 解密时收到错误

标签 java sockets encryption cryptography rsa

我正在做一个小型学校“项目”,它是一个小型聊天程序。您启动服务器,服务器将公钥发送给客户端,以便客户端可以发回加密的数据。

问题是,当我尝试在服务器端解密它时,我收到一个错误。

javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)

我的服务器等待 socket.accept(),当客户端连接时,它会向此套接字发送公钥。

(这不是完整的代码)

private Client client;
private JSONObject json;

//Connector is used as a listener for messages.
public Connector(Socket socket, ServerBroadCast SBC) throws IOException {
    DIS = new DataInputStream(socket.getInputStream());        
    this.SBC = SBC;
    this.client = SBC.AddClient(socket);        
    //Sending public RSA key to client to use to encrypt their message
    this.client.sendPublicKey(RSA.getPublicKey()); //This is where I am sending RSA public key to client
}

我只是向服务器发送一条消息,看看它是否已加密以及服务器是否可以解密它。

使用此类发送给 CLIENT

DataOutputStream DOS;
public Client(Socket s) throws IOException {
    DOS = new DataOutputStream(s.getOutputStream());
}
public void sendPublicKey(PublicKey publicKey) throws IOException {
    JSONObject json = new JSONObject();

    json.put(JSONKeys.TYPE, JSONTypes.PUBLIC_KEY);
    json.put(JSONKeys.MESSAGE, RSA.getEncodedPublicKey());
    this.send(json);
}

public void send(JSONObject json) throws IOException{
    this.DOS.writeUTF(json.toString());
    System.out.println(json);
}

这是我要加密和解密的 RSA 类

private static PrivateKey privateKey = null;
private static PublicKey publicKey = null;

public static void generateKeyPair() {
    KeyPairGenerator keyPairGenerator = null;
    KeyPair keyPair = null;

    try {
        keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    keyPairGenerator.initialize(1024);
    keyPair = keyPairGenerator.generateKeyPair();

    RSA.privateKey = keyPair.getPrivate();
    RSA.publicKey = keyPair.getPublic();
}

public static String getEncodedPublicKey() {
    return (new String(Base64.encode(RSA.getPublicKey().getEncoded())));
}

public static String encrypt(String string) {
    byte[] encrypted = null;

    try {
        Cipher cipher = Cipher.getInstance("RSA");//174 bytes

        cipher.init(Cipher.ENCRYPT_MODE, RSA.getPublicKey());
        encrypted = cipher.doFinal(string.getBytes());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return (new String(Base64.encode(encrypted)));
}

public static String decrypt(String string) {
    byte[] decrypted = null;

    try {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

        cipher.init(Cipher.DECRYPT_MODE, RSA.getPrivateKey());
        decrypted = cipher.doFinal(Base64.decode(string));
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (Base64DecodingException e) {
        e.printStackTrace();
    }

    return (new String(decrypted));
}

我知道我的填充有一些“错误”,因为错误表明了这一点,但我不明白是什么。

我从客户端返回的加密消息的长度是 174 字节长。我知道它太长了,但为什么它会从我的纯文本中创建如此巨大的加密消息。

private final String MESSAGE = "Hello from client side";

最佳答案

您似乎有两个问题需要回答:

a) 客户端的公钥是否正确?

b) 服务器端的密文是否正确?

尝试单独回答问题。例如,您可以尝试:

  1. 在服务器上获取公钥,并加密消息“hello world”。然后在服务器上获取私钥并解密消息。有效吗?然后...

  2. 从 #1 中获取密文并将其硬编码在客户端中,这样无论服务器向客户端发送什么内容,客户端都会将此已知正确的密文发送回服务器。现在,服务器是否再次解密?

如果服务器可以正常解密,那么您可以:

  • 从服务器获取公钥并加密一些数据(在客户端)。现在,在客户端使用私钥解密数据。它能解密吗?

  • 好的,现在将密文发送到服务器并解密。

  • 限制每一步的未知数。服务器实际上可以解密正确的密文(也许由于某些错误而不能)?客户端真的能收到正确的公钥吗?客户端能否发送正确的密文?

    关于java - 使用 RSA 解密时收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213153/

    相关文章:

    java - 如何使用SubjectPublicKeyInfo加密数据?

    java - 在 Java 中使用 AES 256 加密 RSA 私钥

    java - spring boot 应用程序作为可执行 jar 运行,但不能作为部署在 jboss EAP 6 上的 war

    java - 我要把我的 socket 改成什么?

    Chomsky 形式的 CFG 算法的 Java 实现

    c - 为什么客户端的 close() 套接字不会导致服务器的 select() 返回

    sockets - libuv 如何在对等方(linux)重置连接时忽略 SIGPIPE

    java - 如何使用 DES 在 Java 中加密数据并将该数据解密为 Object-c

    java - 富有表现力的 Java 函数

    java - 使用 AssetManager 以编程方式在 DAM 中上传文件?我应该使用什么 MimeType?