Oppure

Loading
05/08/09 8:37
klez91
Ti ringrazio per la risposta, ma comunque non sono riuscito a convertirla. Posto il sorgente della classe che ho scritto magari se gentilmente mi puoi dare una mano...
In pratica ho solamente aggiunto delle funzioni per crittografare la connessione...
Imports System.Net.Sockets

Public Class MySocket
    Inherits System.Net.Sockets.Socket

    Private _BytesPassword() As Byte = Nothing
    Private _Authenticated As Boolean = False
    Private Rsa As New System.Security.Cryptography.RSACryptoServiceProvider

    Public ReadOnly Property BytesPassword() As Byte()
        Get
            Return _BytesPassword
        End Get
    End Property

    Public ReadOnly Property Authenticated() As Boolean
        Get
            Return _Authenticated
        End Get
    End Property

    Sub New(ByVal addressFamily As System.Net.Sockets.AddressFamily, ByVal socketType As System.Net.Sockets.SocketType, ByVal protocolType As System.Net.Sockets.ProtocolType)
        MyBase.New(addressFamily, socketType, protocolType)
    End Sub

    Private IV32 As Byte() = New Byte() {133, 206, 56, 64, 110, 158, 132, 22, _
99, 190, 35, 129, 101, 49, 204, 248, 251, 243, 13, 194, 160, 195, 89, 152, 149, 227, 245, 5, 218, 86, 161, 124}

    Private Function Encrypt(ByVal Input() As Byte, ByVal Length As Integer) As Byte()
        Dim Provider As New System.Security.Cryptography.RijndaelManaged
        Dim BytePassword As Byte()
        Dim Encryptor As System.Security.Cryptography.ICryptoTransform
        Dim Output As Byte()

        Provider.KeySize = 256
        Provider.BlockSize = 256
        BytePassword = _BytesPassword
        Encryptor = Provider.CreateEncryptor(BytePassword, IV32)
        Output = Encryptor.TransformFinalBlock(Input, 0, Length)

        Provider.Clear()
        Encryptor.Dispose()
        Return Output
    End Function

    Private Function Decrypt(ByVal Input() As Byte, ByVal Length As Integer) As Byte()
        Dim Provider As New System.Security.Cryptography.RijndaelManaged
        Dim BytePassword As Byte()
        Dim Decryptor As System.Security.Cryptography.ICryptoTransform
        Dim Output As Byte()

        Provider.KeySize = 256
        Provider.BlockSize = 256
        BytePassword = _BytesPassword
        Decryptor = Provider.CreateDecryptor(BytePassword, IV32)

        Output = Decryptor.TransformFinalBlock(Input, 0, Length)
        Provider.Clear()
        Decryptor.Dispose()
        Return Output
    End Function

    Private Function GeneratePassword() As Byte()
        Dim Key As String = ""
        Dim Rnd As New Random

        For i As Byte = 0 To 11
            Key &= Convert.ToChar(Rnd.Next(33, 126))
        Next
        Dim SaltBytes() As Byte = {162, 21, 92, 34, 27, 239, 64, 30, 136, 102, 223}
        Dim Derive As New System.Security.Cryptography.Rfc2898DeriveBytes(Key, SaltBytes, 5)
        Dim DerivedBytes() As Byte
        DerivedBytes = Derive.GetBytes(32)
        Return DerivedBytes

    End Function

    Public Sub AuthenticateAsClient()
        If _Authenticated = False Then
            Dim Bytes() As Byte = System.Text.UTF8Encoding.UTF8.GetBytes(Rsa.ToXmlString(False))
            Send(Bytes, Bytes.Length, SocketFlags.None)
            Dim Read As Integer = 0
            Dim BytesPassword(ReceiveBufferSize) As Byte
            Read = Receive(BytesPassword, BytesPassword.Length, SocketFlags.None)
            Dim TempBytes(Read - 1) As Byte
            For i As Integer = 0 To UBound(TempBytes)
                TempBytes(i) = Bytes(i)
            Next
            Dim Data As String = System.Text.UTF8Encoding.UTF8.GetString(TempBytes)
            _BytesPassword = Convert.FromBase64String(Data)
            _Authenticated = True
        Else
            Throw New Exception
        End If
    End Sub

    Public Sub AuthenticateAsServer()
        If _Authenticated = False Then
            Dim Read As Integer = 0
            Dim Bytes(ReceiveBufferSize) As Byte
            Read = Receive(Bytes, Bytes.Length, SocketFlags.None)
            Dim TempBytes(Read - 1) As Byte
            For i As Integer = 0 To UBound(TempBytes)
                TempBytes(i) = Bytes(i)
            Next
            Dim Data As String = System.Text.UTF8Encoding.UTF8.GetString(Bytes)
            Dim Stringa() As String = Data.Split("|")
            Dim Rsa As New System.Security.Cryptography.RSACryptoServiceProvider
            Rsa.FromXmlString(Stringa(1))
            _BytesPassword = Me.GeneratePassword()
            Dim BytesPasswordEncrypted() As Byte = Rsa.Encrypt(BytesPassword, False)
            Dim Password As String = Convert.ToBase64String(BytesPasswordEncrypted)
            Send(System.Text.UTF8Encoding.UTF8.GetBytes(Password), Password.Length, SocketFlags.None)
            _Authenticated = True
        Else
            Throw New Exception
        End If
    End Sub

    Public Function SendEncryptedBytes(ByVal buffer() As Byte, ByVal size As Integer, ByVal socketFlags As System.Net.Sockets.SocketFlags) As Integer
        If _Authenticated = True Then
            Dim Read As Integer = 0
            Dim Output() As Byte = Encrypt(buffer, size)
            Read = Send(Output, Output.Length, socketFlags)
            Return Read
        Else
            Throw New Exception
        End If
    End Function

    Public Function ReceiveEncryptedBytes(ByRef buffer() As Byte, ByVal size As Integer, ByVal socketFlags As System.Net.Sockets.SocketFlags) As Integer
        If _Authenticated = True Then
            Dim Read As Integer = 0
            Dim Input(size) As Byte
            Read = Receive(Input, size, socketFlags)
            buffer = Decrypt(Input, size)
            Return Read
        Else
            Throw New Exception
        End If
    End Function
End Class
Ultima modifica effettuata da klez91 05/08/09 8:38
aaa
06/08/09 17:19
klez91
Forse ho risolto ereditando dalla classe TCPClient anziché da Socket sempre del namespace System:Net.Sockets .Eventuali altri aiuti rigurado la conversione tra le due classi saranno comunque ben accolti...Intanto, che ne pensate della classe postata precendentemente ??? In pratica client e server dopo essersi autenticati scambiandosi le password possono comunicare in forma criptata. Volevo sapere se almeno dal punto di vista della sicurezza e delle prestazioni è corretta. Ciao :k:
aaa