本页内容

QSslKey Class

QSslKey 类为私钥和公钥提供了一个接口。更多内容...

头文件: #include <QSslKey>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network

注意:该类中的所有函数均为可重入函数。

公共函数

QSslKey()
QSslKey(Qt::HANDLE handle, QSsl::KeyType type = QSsl::PrivateKey)
QSslKey(QIODevice *device, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat encoding = QSsl::Pem, QSsl::KeyType type = QSsl::PrivateKey, const QByteArray &passPhrase = QByteArray())
QSslKey(const QByteArray &encoded, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat encoding = QSsl::Pem, QSsl::KeyType type = QSsl::PrivateKey, const QByteArray &passPhrase = QByteArray())
QSslKey(const QSslKey &other)
~QSslKey()
QSsl::KeyAlgorithm algorithm() const
void clear()
Qt::HANDLE handle() const
bool isNull() const
int length() const
void swap(QSslKey &other)
QByteArray toDer(const QByteArray &passPhrase = QByteArray()) const
QByteArray toPem(const QByteArray &passPhrase = QByteArray()) const
QSsl::KeyType type() const
bool operator!=(const QSslKey &other) const
QSslKey &operator=(const QSslKey &other)
bool operator==(const QSslKey &other) const

详细说明

QSslKey 提供了一个用于管理密钥的简单 API。

另请参阅 QSslSocket 、QSslCertificate 和QSslCipher 。

成员函数文档

QSslKey::QSslKey()

构建一个空键。

另请参阅 ` isNull()`。

[explicit] QSslKey::QSslKey(Qt::HANDLE handle, QSsl::KeyType type = QSsl::PrivateKey)

根据有效的本机密钥handle 构建一个 QSslKey 对象。type 指定该密钥是公钥还是私钥。

QSslKey 将拥有该密钥的所有权,您不得使用本机库释放该密钥。

QSslKey::QSslKey(QIODevice *device, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat encoding = QSsl::Pem, QSsl::KeyType type = QSsl::PrivateKey, const QByteArray &passPhrase = QByteArray())

通过使用指定的algorithm 和encoding 格式,从device 中读取并解码数据,从而构建一个QSslKey。type 用于指定密钥是公钥还是私钥。

如果密钥是加密的,则使用passPhrase 对其进行解密。

构建完成后,请使用isNull() 检查device 提供的密钥是否有效。

QSslKey::QSslKey(const QByteArray &encoded, QSsl::KeyAlgorithm algorithm, QSsl::EncodingFormat encoding = QSsl::Pem, QSsl::KeyType type = QSsl::PrivateKey, const QByteArray &passPhrase = QByteArray())

通过使用指定的algorithm 和encoding 格式,对字节数组encoded 中的字符串进行解码,从而构建一个QSslKey对象。type 用于指定密钥是公钥还是私钥。

如果密钥已加密,则使用passPhrase 对其进行解密。

构建完成后,请使用isNull() 检查encoded 是否包含有效的密钥。

QSslKey::QSslKey(const QSslKey &other)

创建other 的完全相同的副本。

[noexcept] QSslKey::~QSslKey()

销毁QSslKey 对象。

QSsl::KeyAlgorithm QSslKey::algorithm() const

返回密钥算法。

void QSslKey::clear()

清除该键的内容,使其成为空键。

另请参阅 isNull()。

Qt::HANDLE QSslKey::handle() const

返回指向本机密钥句柄的指针(如果存在的话),否则返回nullptr 。

您可以将此句柄与原生 API 结合使用,以访问有关该键的扩展信息。

警告:使用 此函数很可能导致代码不可移植,且其返回值可能会因平台不同或 Qt 次要版本的不同而有所差异。

bool QSslKey::isNull() const

如果该键为空,则返回true ;否则返回false。

另请参阅 clear()。

int QSslKey::length() const

返回密钥的长度(以位为单位);如果密钥为空,则返回 -1。

[noexcept] void QSslKey::swap(QSslKey &other)

将此 SSL 密钥与other 进行交换。此操作速度极快,且从不会失败。

QByteArray QSslKey::toDer(const QByteArray &passPhrase = QByteArray()) const

返回采用DER编码的密钥。

应省略passPhrase 参数,因为DER无法进行加密。该参数将在Qt的未来版本中被移除。

QByteArray QSslKey::toPem(const QByteArray &passPhrase = QByteArray()) const

返回采用PEM编码的密钥。如果该密钥是私钥,且passPhrase 不为空,则结果将使用passPhrase 进行加密。

QSsl::KeyType QSslKey::type() const

返回密钥的类型(即 PublicKey 或 PrivateKey)。

bool QSslKey::operator!=(const QSslKey &other) const

如果该键不等于 keyother ,则返回true ;否则返回false 。

QSslKey &QSslKey::operator=(const QSslKey &other)

将other 的内容复制到此键中,使这两个键的内容完全一致。

返回对该QSslKey 的引用。

bool QSslKey::operator==(const QSslKey &other) const

如果该键等于other ,则返回true ;否则返回false 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.