簡體   English   中英

RSA 在 Qt/C++ 中使用 OpenSSL 加密並在 Java 中解密:錯誤填充異常

[英]RSA encrypt with OpenSSL in Qt/C++ and decrypt in Java: Bad Padding Exception

I am trying out a sample program where I am encrypting a string with RSA public key in C++ Qt Framework (using statically linked OpenSSL C++ library), and decrypting the same ciphertext using javax.crypto library. 我正在使用我 PC 上的空閑端口通過套接字連接將這個密文發送到本地主機。

以下是代碼:

我的 Qt/C++ 代碼:

主.cpp:

#include "cipher.h"
#include "assert.h"
#include "string.h"
#include <QApplication>
#include <QTcpSocket>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Cipher *cipher=new Cipher();

    QTcpSocket *qts=new QTcpSocket();
    qts->connectToHost("localhost",11111);

    QByteArray message, enc_key,enc_message;

    message="Elephant";
   
    enc_message=cipher->encryptRSA(cipher->getPublicKey("publickey.pem"),message);

    qDebug()<<message;
    qDebug()<<enc_message;

    if(qts->waitForConnected(300)){
        qts->write(QString::fromStdString(enc_message.toStdString()).toUtf8().constData());
        qts->write("\n");
        qts->flush();
    }

    return a.exec();
}

對於 encryptRSA function,我使用了 VoidRealms 教程中的示例。 這是 GitHub 鏈接: https://github.com/voidrealms/Qt-154

從上面的鏈接中,我使用cipher.hcipher.cpp沒有任何更改。

Java代碼:

package servertest;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import org.apache.commons.io.FileUtils;

public class ServerTest {
    static int port=11111;
    static ServerSocket ss;
    static Socket s;
    
    public static void main(String[] args) {
        System.out.println("Server Started!");
        try {
            ss = new ServerSocket(port);
            s=ss.accept();
            
            InputStreamReader isr = new InputStreamReader(new BufferedInputStream(s.getInputStream()));
            BufferedReader br = new BufferedReader(isr);
            
            String str=br.readLine();
            
            System.out.println("Received: "+str);
            byte[] encrypted = str.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            PrivateKey privateKey = loadPrivateKey();
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decrypted = cipher.doFinal(encrypted); 
            
            System.out.println("Decrypted: "+new String(decrypted));
            
        } catch (IOException ex) {
            Logger.getLogger(ServerTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(ServerTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ServerTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(ServerTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ServerTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static PrivateKey loadPrivateKey() throws Exception {
        String privateKeyPEM = FileUtils.readFileToString(new File("privatekey-pkcs8.pem"), StandardCharsets.UTF_8);

        // strip off header, footer, newlines, whitespaces
        privateKeyPEM = privateKeyPEM
                .replace("-----BEGIN PRIVATE KEY-----", "")
                .replace("-----END PRIVATE KEY-----", "")
                .replace("-----BEGIN RSA PRIVATE KEY-----", "")
                .replace("-----END RSA PRIVATE KEY-----", "")
                .replaceAll("\\s", "");
        
        //System.out.println(privateKeyPEM);
        
        // decode to get the binary DER representation
        byte[] privateKeyDER = Base64.getDecoder().decode(privateKeyPEM.getBytes("UTF-8"));

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyDER));
        return privateKey;
    }
    
}

此外,我使用標准 OpenSSL 命令生成了這些密鑰。 我嘗試使用具有不同位長度的不同密鑰,但我得到了同樣的錯誤。 我已將privatekey.pem轉換為 PKCS8 ( privatekey-pkcs8.pem )。 為了生成和使用密鑰,我點擊了下面的鏈接:

https://adangel.org/2016/08/29/openssl-rsa-java/

問題:

我收到javax.crypto.BadPaddingException: Decryption error 我究竟做錯了什么?

我已經嘗試過的事情

  • 我是加密新手,我不知道是否應該將此密文編碼為 base64 或十六進制,當我嘗試此操作時,Java 抱怨密文長於允許的最大位數。
  • 在 qts->write() 階段,我嘗試在幾種數據類型和格式之間進行轉換,包括 const char*、char[]、QByteArray、toUtf8().toconstData()、std::string,同時使用 QString:: 轉換為 QString fromUtf8() 和 QString::fromLocal8bit()。 我應該嘗試 Utf16 和 Latin1 嗎?

請幫我解決這個問題。

好的,我在發布后的 20 分鍾內解決了這個問題,方法是將密文轉換為 Base64 並通過套接字傳遞,當然,在接收端,我需要將其解碼回字節。

以前當我嘗試這個時,我忘記將 Base64 文本解碼回字節。 對不起,如果我浪費了別人的時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM