Jump to content

Encryption over TCP


zelles
 Share

Recommended Posts

I figured I'd share a little example script on encrypting your data over TCP communcation. I put together a simple function that can be used to encrypt when sending, and decrypt when receiving. For added protection, one could also chop it in to segments to send seperatly and have packed back together when recieved to decode.

The example script will show you what the encrypted data looks like as well as the decrypted result. To use it simply run the script and select server, then run the script again and select client. Once you have the server and client windows open you can send data from the client to the server and and server will show a message box with the encrypted and decrypted messages that it receives.

The example script:

#include <ButtonConstants.au3>
#include <Crypt.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

OnAutoItExitRegister("Close")

Global $ServerAddress = "127.0.0.1"
Global $ServerPort = "12700"

RunDemo()

Func RunDemo()
    TCPStartup()
    Local $hGUI = GUICreate("Select Option", 250, 70)
    Local $idBtnServer = GUICtrlCreateButton("1. Server", 65, 10, 130, 22)
    Local $idBtnClient = GUICtrlCreateButton("2. Client", 65, 40, 130, 22)
    GUISetState(@SW_SHOW, $hGUI)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Close()
            Case $idBtnServer
                GUICtrlSetState($idBtnClient, $GUI_HIDE)
                GUICtrlSetState($idBtnServer, $GUI_DISABLE)
                TCPServer()
            Case $idBtnClient
                GUIDelete($hGUI)
                ClientGUI()
        EndSwitch
        Sleep(10)
    WEnd
EndFunc

Func ClientGUI()
    $Form1 = GUICreate("TCP Client", 236, 89, 192, 124)
    $InputCommand = GUICtrlCreateInput("", 8, 32, 217, 21)
    $ButtonSend = GUICtrlCreateButton("Send", 152, 56, 75, 25)
    $Label1 = GUICtrlCreateLabel("Send a command to server:", 8, 8, 134, 17)
    GUISetState(@SW_SHOW)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                TCPCloseSocket($TCPConnector)
                Close()
            Case $ButtonSend
                $Message = GUICtrlRead($InputCommand)
                $TCPConnector = TCPConnect($ServerAddress, $ServerPort)
                TCPSend($TCPConnector, TCPCrypt(True, $Message))
        EndSwitch
    WEnd
EndFunc

Func TCPServer()
    $TCPListener = TCPListen($ServerAddress, $ServerPort, 100)
    $TCPAccepted = 0
    Do
        $TCPAccepted = TCPAccept($TCPListener)
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then
            TCPCloseSocket($TCPListener)
            Close()
        EndIf
    Until $TCPAccepted <> -1
    $TCPEncrypted = TCPRecv($TCPAccepted, 2048)
    $TCPReceived = TCPCrypt(False, $TCPEncrypted)
    MsgBox(0, "Packet Received", "Received Packet" & @CRLF & @CRLF & "Decrypted:" & @CRLF & "  " & $TCPReceived & @CRLF & @CRLF & "Encrypted:" & @CRLF & "  " & $TCPEncrypted)
    TCPCloseSocket($TCPListener)
    TCPServer()
EndFunc

#Region TCPCrypt Function
Func TCPCrypt($TCPCrypt_Action, $TCPCrypt_Data)
    _Crypt_Startup()
    $TCPCrypt_Key = _Crypt_DeriveKey("YOURsuperSECRETpassKEYforDecryption", $CALG_AES_256)
    If $TCPCrypt_Action = True Then
        $TCPCrypt_Return = _Crypt_EncryptData($TCPCrypt_Data, $TCPCrypt_Key, $CALG_USERKEY)
    Else
        $TCPCrypt_Return = BinaryToString(_Crypt_DecryptData($TCPCrypt_Data, $TCPCrypt_Key, $CALG_USERKEY))
    EndIf
    _Crypt_DestroyKey($TCPCrypt_Key)
    _Crypt_Shutdown()
    Return $TCPCrypt_Return
EndFunc
#EndRegion TCPCrypt Function

Func Close()
    TCPShutdown()
    Exit
EndFunc

This is the basic function being used to encrypt and decrypt the data.

; Example Usage:

;  Send:   TCPSend($TCPSocket, TCPCrypt(True, $Message))

;  Receive:   $sReceived = TCPCrypt(False, TCPRecv($TCPSocket, 2048))


#Region TCPCrypt Function
Func TCPCrypt($TCPCrypt_Action, $TCPCrypt_Data)
    _Crypt_Startup()
    $TCPCrypt_Key = _Crypt_DeriveKey("YOURsuperSECRETpassKEYforDecryption", $CALG_AES_256)
    If $TCPCrypt_Action = True Then
        $TCPCrypt_Return = _Crypt_EncryptData($TCPCrypt_Data, $TCPCrypt_Key, $CALG_USERKEY)
    Else
        $TCPCrypt_Return = BinaryToString(_Crypt_DecryptData($TCPCrypt_Data, $TCPCrypt_Key, $CALG_USERKEY))
    EndIf
    _Crypt_DestroyKey($TCPCrypt_Key)
    _Crypt_Shutdown()
    Return $TCPCrypt_Return
EndFunc
#EndRegion TCPCrypt Function

Hope this helps...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...