java - RSA 在 Qt/C++ 中使用 OpenSSL 加密并在 Java 中解密:Bad Padding Exception
问题描述
我正在尝试一个示例程序,我在 C++ Qt 框架中使用 RSA 公钥加密字符串(使用静态链接的 OpenSSL C++ 库),并使用 javax.crypto 库解密相同的密文。我正在使用我 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 函数,我使用了 VoidRealms 教程中的示例。这是 GitHub 链接: https ://github.com/voidrealms/Qt-154
从上面的链接我使用cipher.h
并且cipher.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
Java。我究竟做错了什么?
我已经尝试过的事情
- 我是加密新手,我不知道是否应该将此密文编码为 base64 或十六进制,当我尝试此操作时,Java 抱怨密文长度超过允许的最大位数。
- 在 qts->write() 阶段,我尝试在几种数据类型和格式之间进行转换,包括 const char*、char[]、QByteArray、toUtf8().toconstData()、std::string,同时使用 QString:: 转换为 QString fromUtf8() 和 QString::fromLocal8bit()。我应该尝试 Utf16 和 Latin1 吗?
请帮我解决这个问题。
解决方案
好的,我在发布后的 20 分钟内解决了这个问题,方法是将密文转换为 Base64 并通过套接字传递,当然,在接收端,我需要将其解码回字节。
以前当我尝试这个时,我忘记将 Base64 文本解码回字节。对不起,如果我浪费了别人的时间。
推荐阅读
- networking - 使用原始套接字编程的 LLMNR 和 DNS 数据包
- android - 如何实现搜索视图模型并将其显示在 kotlin 的 recyclerview 中
- reactjs - 再次反应钩子道具未定义
- javascript - bpmn io vue 3 集成问题
- mysql - 参考外部查询 MySQL
- javascript - 如何在月份格式为字符串的“日期范围选择器”中翻译日期?
- c++ - 将继承的模板类转换为父类
- java - 使用设置文本功能后,数组适配器不显示项目
- azure-storage - 写入单独容器时节流与完全单独的 ADLS Gen2 存储
- react-i18next - react-i18next 输出中的 HTML 注释