From 736310d4699db4bd105622b50d17a611351c5496 Mon Sep 17 00:00:00 2001 From: Joren Schipman Date: Thu, 2 May 2024 16:41:50 +0200 Subject: [PATCH] Let decryption happen in a seperate goroutine to hopefully speed it up --- rsaserver.go | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/rsaserver.go b/rsaserver.go index 4872990..9ab8c4b 100644 --- a/rsaserver.go +++ b/rsaserver.go @@ -104,7 +104,7 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) { // Receive and handle multiple files for { - var plaintext []byte + var chunks []string for { chunk, err := bufio.NewReader(conn).ReadString('\n') if err != nil { @@ -115,23 +115,14 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) { fmt.Printf("Received file\n") break } - ciphertext, err := base64.StdEncoding.DecodeString(strings.TrimSpace(chunk)) - if err != nil { - log.Printf("Error decoding chunk: %v", err) - return - } - plaintextChunk, err := decrypt(ciphertext, key, iv) - if err != nil { - log.Printf("Error decrypting chunk: %v", err) - return - } - plaintext = append(plaintext, plaintextChunk...) + chunks = append(chunks, chunk) conn.Write([]byte("Received and decrypted chunk\n")) } - go handleDecrypted(plaintext, uid) + go decryptAndHandle(chunks, key, iv, uid) + conn.Write([]byte("Received and decrypted\n")) // Check if there are more files to receive @@ -147,6 +138,25 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) { } } +func decryptAndHandle(chunks []string, key []byte, iv []byte, uid []byte){ + var plaintext []byte + for _, chunk := range chunks{ + ciphertext, err := base64.StdEncoding.DecodeString(strings.TrimSpace(chunk)) + if err != nil { + log.Printf("Error decoding chunk: %v", err) + return + } + + plaintextChunk, err := decrypt(ciphertext, key, iv) + + if err != nil { + log.Printf("Error decrypting chunk: %v", err) + return + } + plaintext = append(plaintext, plaintextChunk...) + } + handleDecrypted(plaintext, uid) +} func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) { block, err := aes.NewCipher(key)