C ssl使用自定義的crt證書和key金鑰

2021-10-11 14:28:40 字數 1778 閱讀 3616

首先你得有乙個證書和金鑰,把它們轉換成windows支援的pfx格式證書,這個證書就帶有金鑰

或者linux命令轉換(需要安裝openssl)

openssl pkcs12 -export -out test.pfx -inkey test.key -in test.crt
之後去這裡匯入證書,匯入證書要選擇我的使用者賬戶,不能選擇我的計算機賬戶,不然下次重啟證書就失效

前奏工作做完,就可以做接下來的事情了。(mmc匯入證書不儲存,一段時間後會失效)

原始碼參考

的單向認證

這裡精簡了ssl的**,僅供測試,不保證不會出錯。

客戶端**如下

警告:中間有個validateservercertificate函式,如果證書認證失敗就會呼叫這個函式(如果是官方認證的證書就不會出現這種情況),如果返回false就會導致ssl通訊無法進行下去,如果你的證書開啟就是——不能保證該證書完整性,推薦把返回值都改為true。

需要改的地方:ip、埠號、證書認證方(testserver)

", sslpolicyerrors);

return false;

}public static void runclient()

catch (authenticationexception e)

", e.message);

if (e.innerexception != null)

", e.innerexception.message);

}console.writeline("authentication failed - closing the connection.");

client.close();

return;

}byte messsage = encoding.utf8.getbytes("hello from the client.");

sslstream.write(messsage);

sslstream.flush();

client.close();

}public static void main(string args)

catch (exception ex)

console.readline();}}

}} 服務端**

需要改的地方:ip、埠號、證書認證方(testserver)

using system;

using system.net;

using system.net.sockets;

using system.net.security;

using system.text;

using system.security.authentication;

using system.security.cryptography.x509certificates;

", messagedata);

}sslstream.close();

client.close();

}static string readmessage(sslstream sslstream)

}while (bytes != 0);

return messagedata.tostring();

}public static void main(string args)

catch (exception ex)

console.readline();}}

}

自定義Attribute的使用

attribute類是所有屬性型別的基類。個人理解attribute型別的主要作用是為某些需要進行特殊注釋的型別快速新增備註資訊。例如 using system using system.collections.generic using system.reflection 標示某個方法是否是aja...

使用自定義的UIView

其實使用自定義的uiview非常簡單,就是利用模板生成的基於view的ios程式,然後在xib檔案中,將對應view的對應類修改為你的自定義類就可以了。當然也可以使用直接建立的方式,就是在loadview當中直接使用的方式進行繪製。myview view myview alloc initwithf...

自定義Listview的使用

listview是android一系列元件之一,它以垂直列表的方式列出所需的列表項。實現方式有兩種 使用listview元件 當前的activity繼承listactivity。listview是需要繫結資料來源的。根據列表的介面卡型別,列表分為三種,arrayadapter,adapter和 cu...