解释:read PEM private key done: type <unknown>

本文探讨了在使用SSH进行多台服务交互时遇到的错误,并详细解释了私钥与公钥观念的误解。通过实例演示了正确生成和使用私钥的方法,以及在遇到SSH连接请求输入私钥时的解决步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     SSH不需要登陆密码来实现多台服务的交互,发现了这个错误:
ssh -v localhost
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to localhost [::1] port 22.
debug1: Connection established.
debug1: identity file /home/app/.ssh/identity type -1
debug1: identity file /home/app/.ssh/identity-cert type -1
debug1: identity file /home/app/.ssh/id_rsa type 1
debug1: identity file /home/app/.ssh/id_rsa-cert type -1
debug1: identity file /home/app/.ssh/id_dsa type -1
debug1: identity file /home/app/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'localhost' is known and matches the RSA host key.
debug1: Found key in /home/app/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/app/.ssh/identity
debug1: Offering public key: /home/app/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key '/home/app/.ssh/id_rsa':


         错误的根源是误解了公钥/私钥的观念:
       1.  如何你使用passphrase产生了公钥和私钥,私钥不是空字符串的情况下,交互链接
方始终要求你提供私钥。也就会出现上面的提示输入私钥, 提供了私钥才能交互。
      2.   除非你将私钥为空字符串,即empty passphrase.

       解决方法:
   [app@hadoop-master ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/app/.ssh/id_rsa): 【press Enter button】
Created directory '/home/app/.ssh'.   
Enter passphrase (empty for no passphrase): 【press Enter button】
Enter same passphrase again: 【press Enter button】
Your identification has been saved in /home/app/.ssh/id_rsa.
Your public key has been saved in /home/app/.ssh/id_rsa.pub.
The key fingerprint is:
2e:f7:e7:2d:10:63:60:63:12:16:c0:7f:ee:f3:55:63 app@hadoop-master
The key's randomart image is:
+--[ RSA 2048]----+
| ...+o |
| ... = |
| . + o |
| . . + |
| oS. o E |
| .. . o . |
| ..o .. |
| oo. .o. |
| ooo... |
+-----------------+







<think>我们正在解决如何正确加载嵌入在Qt资源文件(.qrc)中的public_key.pem文件的问题。根据之前的讨论,我们已经将公钥文件添加到了资源文件中,但在程序运行时无法正确加载。 问题分析: 在Qt中,资源文件通过资源系统进行访问,路径格式为":/前缀/文件名"。如果使用普通的文件操作函数(如open)来访问资源文件,将无法找到,因为资源文件已经被编译到可执行文件中。 解决方案: 我们需要使用Qt提供的资源访问机制来读取资源文件。具体步骤如下: 1. 确保资源文件已正确添加到项目中,并已编译。 2. 在代码中使用QFile来打开资源文件,并读取内容。 步骤详解: 步骤1:创建资源文件(.qrc)并添加公钥文件 例如,我们有一个名为`resources.qrc`的文件,内容如下: ```xml <RCC> <qresource prefix="/keys"> <file>public_key.pem</file> </qresource> </RCC> ``` 这里,我们将`public_key.pem`文件添加到了前缀为`/keys`的资源路径下。 步骤2:在项目文件中包含资源文件(如.pro文件) 在Qt项目文件(.pro)中,确保添加了资源文件: ```qmake RESOURCES += resources.qrc ``` 步骤3:在代码中通过Qt资源路径访问文件 使用QFile来读取资源文件,注意路径格式为":/keys/public_key.pem"。 示例代码: ```cpp #include <QFile> #include <QTextStream> // ... QFile keyFile(":/keys/public_key.pem"); if (!keyFile.open(QIODevice::ReadOnly | QIODevice::Text)) { // 处理打开失败 qDebug() << "Failed to open public key file from resource."; return; } QTextStream in(&keyFile); QString keyData = in.readAll(); keyFile.close(); // 如果需要QByteArray,可以使用 // QByteArray keyData = keyFile.readAll(); ``` 步骤4:处理读取到的公钥数据 将读取到的字符串或字节数组传递给加密库进行加载。 注意:由于资源文件是只读的,所以打开文件时使用`QIODevice::ReadOnly`。 常见问题及解决方案: 1. 资源文件未编译:确保在.pro文件中添加了资源文件,并重新构建项目。 2. 路径错误:检查资源文件中的前缀和文件名是否正确,以及代码中的路径是否匹配。 3. 文件打开模式:如果文件是文本文件,使用`QIODevice::Text`模式;如果是二进制文件,则不需要。 如果按照上述步骤操作,应该能够成功加载资源文件中的公钥。 另外,参考引用[4]中,用户使用了RESOURCES += photo.qrc,说明资源文件已经正确添加到项目中,所以问题可能出现在代码中访问资源文件的方式上。 因此,请检查代码中是否使用QFile并按照资源路径格式访问。 如果问题仍然存在,可以尝试使用`QFileInfo`检查文件是否存在: ```cpp QFileInfo checkFile(":/keys/public_key.pem"); if (!checkFile.exists()) { qDebug() << "Resource file does not exist."; } ``` 如果文件不存在,可能是资源文件未正确编译,或者路径拼写错误。 最后,确保在代码中使用的资源路径前缀和文件名与.qrc文件中定义的一致。 通过以上步骤,应该能解决资源文件加载失败的问题。</think>### 解决Qt资源文件中public_key.pem加载失败的问题 当在Qt程序中使用QRC资源文件加载public_key.pem失败时,通常是由于资源路径错误或加载方式不当导致的。以下是完整的解决方案: #### 1. **确认资源文件配置** [^4] 在`.pro`文件中确保正确声明资源文件: ```qmake RESOURCES += keys.qrc # 资源文件名 ``` 在`keys.qrc`文件中正确配置公钥路径: ```xml <RCC> <qresource prefix="/security"> <file>keys/public_key.pem</file> <!-- 实际文件路径 --> </qresource> </RCC> ``` #### 2. **正确的资源加载方式** [^1][^3] 使用Qt资源系统加载公钥: ```cpp // 方法1:使用QFile直接加载 QFile keyFile(":/security/keys/public_key.pem"); if(keyFile.open(QIODevice::ReadOnly)) { QByteArray keyData = keyFile.readAll(); // 使用keyData初始化加密库 keyFile.close(); } else { qDebug() << "Key load error:" << keyFile.errorString(); // 输出详细错误[^3] } ``` ```cpp // 方法2:使用资源路径初始化(推荐) QByteArray loadPublicKey() { QFile res(":/security/keys/public_key.pem"); if(!res.open(QIODevice::ReadOnly)) { qFatal("Failed to load public key: %s", res.errorString().toUtf8().constData()); } return res.readAll(); } ``` #### 3. **常见问题排查表 | 问题现象 | 解决方案 | 验证方法 | |---------|---------|---------| | 文件路径错误 | 检查`.qrc`中的实际路径 | `qDebug() << ":/security/keys/public_key.pem";` | | 资源未编译 | 清理并重建项目 | 检查生成目录中的`qrc_keys.cpp`文件 | | 权限问题 | 确保文件可读 | 使用`QFileInfo::isReadable()`验证 | | 编码问题 | 指定文本模式 | `open(QIODevice::ReadOnly | QIODevice::Text)` | | 资源前缀错误 | 检查`prefix`定义 | 使用`:security/keys/public_key.pem` | #### 4. **加密库集成示例 ```cpp #include <openssl/pem.h> void initCrypto() { QByteArray keyData = loadPublicKey(); // 从资源加载 // 使用OpenSSL加载PEM公钥 BIO* bio = BIO_new_mem_buf(keyData.constData(), keyData.size()); RSA* rsa = PEM_read_bio_RSA_PUBKEY(bio, nullptr, nullptr, nullptr); if(!rsa) { qFatal("Failed to parse public key"); } // 使用rsa进行加密/验证... } ``` #### 5. **部署注意事项** [^2] 打包时确保包含所有依赖: 1. 将`public_key.pem`添加到资源文件 2. 包含必要的Qt DLL: ```bash libgcc_s_dw2-1.dll libstdc++-6.dll Qt5Core.dll Qt5Gui.dll Qt5Widgets.dll ``` 3. 包含`platforms/qwindows.dll` #### 6. **调试技巧** 在代码中添加资源检查: ```cpp // 检查资源是否已注册 qDebug() << "Registered resources:" << QDir(":/").entryList(); // 检查具体路径是否存在 qDebug() << "Key exists:" << QFile::exists(":/security/keys/public_key.pem"); ``` > **关键公式**: > 资源加载成功 = 正确路径(`:/prefix/path`) × 资源注册 × 文件可读 > $$ \text{Success} = \text{Path}_{correct} \times \text{Registered} \times \text{Readable} $$ ### 相关问题 1. Qt资源系统中如何处理二进制文件的读取优化? 2. 如何在不同平台(Windows/Linux/macOS)下确保资源文件路径兼容性? 3. 使用OpenSSL加载PEM密钥时常见错误码有哪些? 4. Qt程序打包时如何自动包含资源文件? 5. 如何实现资源文件的运行时热更新?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值