具有加密格式的数据传输的套接字 [英] Socket with data transfer in encrypted format

查看:63
本文介绍了具有加密格式的数据传输的套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器:

Server :

Module Module1

    Sub Main()

        Dim serverSocket As New TcpListener(8888)
        Dim requestCount As Integer

        Dim clientSocket As TcpClient

        serverSocket.Start()
        msg("Server Started")

        clientSocket = serverSocket.AcceptTcpClient()
        msg("Accept connection from client")

        requestCount = 0

        While (True)

            requestCount = requestCount + 1

            '' Data reading from client
            Dim networkStream As NetworkStream = clientSocket.GetStream()

            Dim bytesFrom(clientSocket.ReceiveBufferSize) As Byte
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))

            Dim dataFromClient As String = Encoding.ASCII.GetString(bytesFrom)


            msg("Data from client -  " + TripleDES.DecryptString(dataFromClient))
          
            '' Data writting to client
            Dim serverResponse As String = "Server response " + Convert.ToString(requestCount)
            Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(serverResponse))
           
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            networkStream.Flush()

            msg(serverResponse)

        End While


        clientSocket.Close()
        serverSocket.Stop()

        msg("exit")
        Console.ReadLine()

    End Sub

    Sub msg(ByVal mesg As String)
        mesg.Trim()
        Console.WriteLine(" >> " + mesg)
    End Sub

End Module





客户:



Client:

Public Shared Function SendRequest(ByVal InXML As String, ByRef OutXML As String) As Boolean
        Try
            Dim nwStream As NetworkStream = clientSocket.GetStream()

            '' write to network stream
            Dim outStream As Byte() = Encoding.ASCII.GetBytes(TripleDES.EncryptString(InXML))
            ' Dim outStream As Byte() = Encoding.ASCII.GetBytes(InXML)
            nwStream.Write(outStream, 0, outStream.Length)
            nwStream.Flush()

            '' data reading from server
            Dim inStream(clientSocket.ReceiveBufferSize) As Byte
            nwStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))

            Dim tmpStr As String = Encoding.ASCII.GetString(inStream)
            OutXML = TripleDES.DecryptString(tmpStr)

            'OutXML = tmpStr

            Return True

        Catch ex As Exception
            OutXML = ex.Message
            Return False

        End Try

    End Function

End Class







加密类






Encryption Class

Public Class TripleDES

    '' Initilization vector
    Private Shared IV() As Byte = Encoding.UTF8.GetBytes("password")

    '' Encryption key - base 64
    Private Shared EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5")

    '' Encryption method
    Public Shared Function EncryptString(ByVal Input As String) As String
        Dim buffer() As Byte = Encoding.UTF8.GetBytes(Input)

        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
        des.Mode = CipherMode.ECB
        des.Key = EncryptionKey
        des.IV = IV

        Dim output() As Byte = des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length())

        Return Convert.ToBase64String(output)

    End Function

    '' Descryption method
    Public Shared Function DecryptString(ByVal Input As String) As String
        Dim buffer() As Byte = Convert.FromBase64String(Input)

        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
        des.Mode = CipherMode.ECB
        des.Key = EncryptionKey
        des.IV = IV

        Dim output() As Byte = des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length())

        Return Encoding.UTF8.GetString(output)

    End Function


End Class









加密部分成功完成。但转移和解密没有发生。我检查了没有加密传输文件的代码,它正在工作。加密和解密类在单独使用时也可以正常工作,无需文件传输。有人可以指出我在哪里错了。我很抱歉,如果代码太长,但必须提供我尝试的内容。





The encryption part is done successfully. But the transferring and decryption are not happening. I checked the code for transferring the file without encryption, it is working. The encryption and decryption classes are also working when used separately without file transfer. Can someone please point out where am I going wrong here. I am sorry if the code is too long but had to present what I tried.

推荐答案

我很确定这是你的文本编码:



I''m pretty sure it''s your text encoding:

Dim dataFromClient As String = Encoding.ASCII.GetString(bytesFrom)





您的加密代码可能生成无法在ascII中表示的字符。尝试使用:





Your encryption code is probably generating characters that can not be represented in ascII. try using:

Encoding.Unicode...




两端的



on both ends instead.


这篇关于具有加密格式的数据传输的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆