伺服器 模式 SSL 必須 使用 具有 關聯 私密金鑰 的憑證

使用VB.NET建立二個程式,一個是server端,一個是client端,連線方式為ssl加密,連線時server端會出現:{"伺服器模式 SSL 必須使用具有關聯私密金鑰的憑證。"}的錯誤,有人知道問題出在那嗎?
憑證是使用makecert建立一個test.cer後,放在硬碟E:\底下,
server端程式 form1

Imports System.Net Imports System.Net.Sockets Imports System.Threading Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim serverIP As String = "127.0.0.1" Dim Port As String = "992" Dim tcpListener As New TcpListener(IPAddress.Parse(serverIP), Int32.Parse(Port)) tcpListener.Start() ListBox1.Items.Clear() ListBox1.Items.Add("SSL server started Port : " + serverIP.ToString() + ":" + Port) Dim lc As New ListenClient(tcpListener) lc.MainForm = Me Dim serverthread As Threading.Thread serverthread = New Thread(New Threading.ThreadStart(AddressOf lc.ServerThreadProc)) serverthread.Start() Catch ex As Exception ListBox1.Items.Add(ex.StackTrace.ToString()) End Try End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Form1.CheckForIllegalCrossThreadCalls = False End Sub End Class

server端程式 listen.vb

Imports System.Net Imports System.Net.Sockets Imports System.Net.Security Imports System.Security.Cryptography.X509Certificates Public Class Listen Private tcpListener As System.Net.Sockets.TcpListener Private tcpClient As System.Net.Sockets.TcpClient Friend MainForm As Form1 Public Sub New(ByVal tcpListener As TcpListener) Me.tcpListener = tcpListener End Sub Public Sub ServerThreadProc() Dim sslStream As SslStream = Nothing Dim bytes(256) As Byte Dim data As String = Nothing Try Do While True MainForm.ListBox1.Items.Add("Waiting for a connection... ") Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient() Dim serverInfo As IPEndPoint = CType(tcpListener.LocalEndpoint, IPEndPoint) MainForm.ListBox1.Items.Add("Connection OK to SSL Server: " + serverInfo.Address.ToString() + ":" + serverInfo.Port.ToString()) data = Nothing sslStream = New SslStream(tcpClient.GetStream()) Dim certificate As System.Security.Cryptography.X509Certificates.X509Certificate = X509Certificate.CreateFromCertFile("E:\test.cer") sslStream.AuthenticateAsServer(certificate) Dim byteData As Integer = sslStream.Read(bytes, 0, bytes.Length) While byteData <> 0 data = System.Text.Encoding.ASCII.GetString(bytes, 0, byteData) MainForm.ListBox1.Items.Add("Rec data : " + data) Dim msg() As Byte = System.Text.Encoding.ASCII.GetBytes(data) sslStream.Write(msg, 0, msg.Length) MainForm.ListBox1.Items.Add("Send data: " + data) byteData = sslStream.Read(bytes, 0, bytes.Length) End While Loop ' Catch ex As Exception ' MainForm.ListBox1.Items.Add(ex.StackTrace.ToString()) Finally If sslStream IsNot Nothing Then sslStream.Close() End If End Try End Sub Public Class ListenClient Private tcpListener As System.Net.Sockets.TcpListener Private tcpClient As System.Net.Sockets.TcpClient Friend MainForm As Form1 Public Sub New(ByVal tcpListener As System.Net.Sockets.TcpListener) Me.tcpListener = tcpListener End Sub Public Sub ServerThreadProc() Dim sslStream As SslStream = Nothing Dim bytes(256) As Byte Dim data As String = Nothing Try Do While True MainForm.ListBox1.Items.Add("Waiting for a connection... ") Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient() Dim serverInfo As IPEndPoint = CType(tcpListener.LocalEndpoint, IPEndPoint) MainForm.ListBox1.Items.Add("Connection OK to SSL Server: " + serverInfo.Address.ToString() + ":" + serverInfo.Port.ToString()) data = Nothing sslStream = New SslStream(tcpClient.GetStream()) Dim certificate As System.Security.Cryptography.X509Certificates.X509Certificate = X509Certificate.CreateFromCertFile("E:\test.cer") sslStream.AuthenticateAsServer(certificate) Dim byteData As Integer = sslStream.Read(bytes, 0, bytes.Length) While byteData <> 0 data = System.Text.Encoding.ASCII.GetString(bytes, 0, byteData) MainForm.ListBox1.Items.Add("Rec data : " + data) Dim msg() As Byte = System.Text.Encoding.ASCII.GetBytes(data) sslStream.Write(msg, 0, msg.Length) MainForm.ListBox1.Items.Add("Send data: " + data) byteData = sslStream.Read(bytes, 0, bytes.Length) End While Loop ' Catch ex As Exception ' MainForm.ListBox1.Items.Add(ex.StackTrace.ToString()) Finally If sslStream IsNot Nothing Then sslStream.Close() End If End Try End Sub End Class End Class

client程式 form1

Imports System.Net.Security Imports System.Net.Sockets Imports System.Text Imports System.Threading Public Class Form1 Dim myPort As Integer = 992 Dim myLocalAddr As String = "127.0.0.1" Dim sslStream As SslStream = Nothing Dim data As String = Nothing Dim myThread As New Thread(New ThreadStart(AddressOf StartClient)) Private Sub Fom1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Form1.CheckForIllegalCrossThreadCalls = False End Sub Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click End End Sub Private Sub cmdConnect_Click(sender As Object, e As EventArgs) Handles cmdConnect.Click cmdConnect.Enabled = False cmdDisConnect.Enabled = True myThread.Start() End Sub Private Sub cmdDisConnect_Click(sender As Object, e As EventArgs) Handles cmdDisConnect.Click cmdConnect.Enabled = True cmdDisConnect.Enabled = False If sslStream IsNot Nothing Then sslStream.Close() End If End Sub Private Sub StartClient() Dim tcpClient As TcpClient = New TcpClient(myLocalAddr, myPort) sslStream = New SslStream(tcpClient.GetStream()) Try Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes("data") sslStream.AuthenticateAsClient(myLocalAddr) If sslStream.IsAuthenticated Then txtMsg.Text = txtMsg.Text & "IsAuthenticated: {0}" & sslStream.IsAuthenticated & vbCrLf txtMsg.Text = txtMsg.Text & "IsMutuallyAuthenticated: {0}" & sslStream.IsMutuallyAuthenticated & vbCrLf txtMsg.Text = txtMsg.Text & "IsEncrypted: {0}" & sslStream.IsEncrypted & vbCrLf txtMsg.Text = txtMsg.Text & "IsSigned: {0}" & sslStream.IsSigned & vbCrLf txtMsg.Text = txtMsg.Text & "IsServer: {0}" & sslStream.IsServer & vbCrLf Else txtMsg.Text = txtMsg.Text & "auth fail" & vbCrLf End If ReDim data(256) Dim responseData As String = String.Empty Dim bytes As Integer = sslStream.Read(data, 0, data.Length) responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes) txtMsg.Text = txtMsg.Text & "Received: {0}" & responseData & vbCrLf Catch ec As Exception Console.WriteLine(ec.StackTrace.ToString()) Finally If sslStream IsNot Nothing Then sslStream.Close() End If End Try End Sub Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim strTest As String = "send test" & vbCrLf Dim myBytes() As Byte = Encoding.Default.GetBytes(strTest) sslStream.Write(myBytes, 0, myBytes.Length) End Sub End Class

執行時的畫面

Toplist

最新的帖子

標籤