From 0801887236c82240ad2fee8c9c52089f71ed8ccf Mon Sep 17 00:00:00 2001 From: Joren Schipman Date: Tue, 30 Apr 2024 17:09:14 +0200 Subject: [PATCH] If the data is to big chunk it --- Connect.kt | 37 ++++++++++++++++--------------------- main.go | 54 +++++++++++++++++------------------------------------- 2 files changed, 33 insertions(+), 58 deletions(-) diff --git a/Connect.kt b/Connect.kt index 0b12a8b..21c9863 100644 --- a/Connect.kt +++ b/Connect.kt @@ -39,18 +39,19 @@ class Connect { suspend fun main() { val host = "192.168.90.151" val port = 8080 - val secureRandom = SecureRandom() val keyBytes = ByteArray(16) - secureRandom.nextBytes(keyBytes) - val key = SecretKeySpec(keyBytes, "AES") - val ivBytes = ByteArray(16) + secureRandom.nextBytes(keyBytes) secureRandom.nextBytes(ivBytes) + val key = SecretKeySpec(keyBytes, "AES") val iv = IvParameterSpec(ivBytes) - val algorithm = "AES/CBC/PKCS5Padding" - val inputText = "abcdefghigklmnopqrstuvwxyz0123456789" + + var sendData = "Hello, World!" + + val chunkSize = 45 + val chunks = sendData.chunked(chunkSize) withContext(Dispatchers.IO) { Socket(host, port).use { socket -> @@ -58,30 +59,24 @@ class Connect { val reader = BufferedReader(InputStreamReader(socket.getInputStream())) val encodedKey = encodeBase64(keyBytes) - println(keyBytes.toHexString()) writer.println(encodedKey) - - println(reader.readLine()) - + reader.readLine() val encodedIV = encodeBase64(ivBytes) - println(ivBytes.toHexString()) writer.println(encodedIV) + reader.readLine() - println(reader.readLine()) - - val cipherText = encrypt(algorithm, inputText, key, iv) - println(cipherText) - writer.println(cipherText) + for (chunk in chunks) { + val cipherText = encrypt(algorithm, chunk, key, iv) + writer.println(cipherText) + reader.readLine() + } writer.println("END_OF_COMMUNICATION") - - println(reader.readLine()) + reader.readLine() println("Client: Ready for next operation") - writer.println("Ready for next operation") - - println(reader.readLine()) + reader.readLine() } } } diff --git a/main.go b/main.go index 4b3754f..6baa675 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ func main() { host := "0.0.0.0" port := 8080 - // Listen for incoming connections listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) if err != nil { log.Fatalf("Error listening: %v", err) @@ -46,65 +45,46 @@ func handleConnection(conn net.Conn) { log.Printf("Error reading key: %v", err) return } - - key, err := base64.StdEncoding.DecodeString(keyData[:len(keyData)-1]) - if err != nil { - log.Printf("Error decoding key: %v", err) - return - } - - fmt.Printf("Key: %x\n", key) - conn.Write([]byte("Received key\n")) + key, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(keyData)) conn.Write([]byte("Received key\n")) - ivData, err := bufio.NewReader(conn).ReadString('\n') - if err != nil { - log.Printf("Error reading iv: %v", err) - return - } - - iv, err := base64.StdEncoding.DecodeString(ivData[:len(ivData)-1]) - if err != nil { - log.Printf("Error decoding iv: %v", err) - return - } - - fmt.Printf("IV: %x\n", iv) + ivData, _ := bufio.NewReader(conn).ReadString('\n') + iv, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(ivData)) conn.Write([]byte("Received IV\n")) + var ciphertext []byte + var chunk string + var plaintext []byte + for { - textData, err := bufio.NewReader(conn).ReadString('\n') + chunk, err = bufio.NewReader(conn).ReadString('\n') if err != nil { - log.Printf("Error reading text: %v", err) + log.Printf("Error reading chunk: %v", err) return } - - if strings.TrimSpace(textData) == "END_OF_COMMUNICATION" { + if strings.TrimSpace(chunk) == "END_OF_COMMUNICATION" { fmt.Println("Client ended communication") break } + ciphertext, _ = base64.StdEncoding.DecodeString(strings.TrimSpace(chunk)) - cipherText, err := base64.StdEncoding.DecodeString(textData[:len(textData)-1]) + plaintextChunk, err := decrypt(ciphertext, key, iv) if err != nil { - log.Printf("Error decoding text: %v", err) + log.Printf("Error decrypting chunk: %v", err) return } + plaintext = append(plaintext, plaintextChunk...) - plaintext, err := decrypt(cipherText, key, iv) - if err != nil { - log.Printf("Error decrypting text: %v", err) - return - } - - fmt.Println("Decrypted text:", string(plaintext)) - conn.Write([]byte("Received and decrypted text\n")) + conn.Write([]byte("Received and decrypted chunk\n")) } + fmt.Println("Decrypted text:", string(plaintext)) conn.Write([]byte("Ready for next operation\n")) } + func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil {