Qt笔记-QSslSocket双向认证
生活随笔
收集整理的這篇文章主要介紹了
Qt笔记-QSslSocket双向认证
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
生成證書邏輯是這樣的:
邏輯是這樣的:
如果要將其導(dǎo)出成client_trust.jks文件,對應(yīng)的客戶端命令為:
@echo on keytool -genkeypair -keyalg RSA -dname "CN=localhost" -alias client -keystore client.jks -keypass cccccc -storepass cccccckeytool -exportcert -file client.cer -alias client -keystore client.jks -storepass cccccckeytool -importcert -file client.cer -alias client -keystore server_trust.jks -storepass cccccc -keypass cccccc服務(wù)端命令為:
@echo on keytool -genkeypair -keyalg RSA -dname "CN=localhost" -alias server -keystore server.jks -keypass cccccc -storepass cccccc keytool -exportcert -file server.cer -alias server -keystore server.jks -storepass cccccc keytool -importcert -file server.cer -alias server -keystore client_trust.jks -storepass cccccc -keypass cccccc如果要導(dǎo)到client.jks及server.jks對應(yīng)的命令為:
keytool -import -trustcacerts -alias client -file client.cer -keystore server.jks -storepass cccccc keytool -import -trustcacerts -alias server -file server.cer -keystore client.jks -storepass cccccc這里windows上可以使用批處理文件。做個腳本去搞,方便快捷:
這里將兩server.jks和client.jks轉(zhuǎn)成p12的格式:
jks轉(zhuǎn)p12 keytool -importkeystore -srckeystore client.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore p_client.p12 keytool -importkeystore -srckeystore server.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore p_server.p12程序運行截圖如下:
QSsl雙向認(rèn)證關(guān)鍵代碼:
服務(wù)端:
void QSSLServer::loadCertificate() {QFile p12File(":/res/p_server.p12");if(!p12File.open(QIODevice::ReadOnly)){qDebug() << "The certificate file open failed!";exit(0);}bool ok = QSslCertificate::importPkcs12(&p12File, m_key, m_privateCertificate, &m_publicCertificateList, "cccccc");if(!ok){qDebug() << "The certificate import error!";exit(0);}p12File.close(); }客戶端:
void QSSLClient::loadCertificate() {QFile p12File(":/res/p_client.p12");if(!p12File.open(QIODevice::ReadOnly)){qDebug() << "The certificate file open failed!";exit(0);}bool ok = QSslCertificate::importPkcs12(&p12File, m_key, m_privateCertificate, &m_publicCertificateList, "cccccc");if(!ok){qDebug() << "The certificate import error!";exit(0);}p12File.close(); }服務(wù)端開啟TCP服務(wù)代碼:
QSSLServer::QSSLServer(QObject *parent) : QTcpServer(parent) {m_key = new QSslKey;m_privateCertificate = new QSslCertificate;loadCertificate();if(!this->listen(QHostAddress::Any, 19999)){qCritical() << "Unable to start the TCP server";exit(0);}connect(this, &QSSLServer::newConnection, this, &QSSLServer::link);qDebug() << "The SSLServer started succeefully";qDebug() << "port: 19999"; }客戶端連接TCP代碼:
void QSSLClient::connectServer() {m_client->connectToHostEncrypted("localhost", 19999);if(m_client->waitForEncrypted(5000)){qDebug() << "Authentication Suceeded";}else{qDebug("Unable to connect to server");exit(0);} }源碼打包下載地址:
https://github.com/fengfanchen/Qt/tree/master/QSslSocket_Two_Way_Ssl
總結(jié)
以上是生活随笔為你收集整理的Qt笔记-QSslSocket双向认证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot笔记-valida
- 下一篇: canvas笔记-使用canvas画矩形