2024-04-30 16:27:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"encoding/base64"
|
|
|
|
"strings"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
host := "0.0.0.0"
|
|
|
|
port := 8080
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error listening: %v", err)
|
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
fmt.Printf("Server listening on %s:%d\n", host, port)
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error accepting connection: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go handleConnection(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleConnection(conn net.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
fmt.Println("Got conn")
|
|
|
|
|
|
|
|
keyData, err := bufio.NewReader(conn).ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2024-04-30 17:09:14 +02:00
|
|
|
key, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(keyData))
|
2024-04-30 16:27:44 +02:00
|
|
|
conn.Write([]byte("Received key\n"))
|
|
|
|
|
2024-04-30 17:09:14 +02:00
|
|
|
ivData, _ := bufio.NewReader(conn).ReadString('\n')
|
|
|
|
iv, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(ivData))
|
2024-04-30 16:27:44 +02:00
|
|
|
conn.Write([]byte("Received IV\n"))
|
|
|
|
|
2024-04-30 17:09:14 +02:00
|
|
|
var ciphertext []byte
|
|
|
|
var chunk string
|
|
|
|
var plaintext []byte
|
|
|
|
|
2024-04-30 16:27:44 +02:00
|
|
|
for {
|
2024-04-30 17:09:14 +02:00
|
|
|
chunk, err = bufio.NewReader(conn).ReadString('\n')
|
2024-04-30 16:27:44 +02:00
|
|
|
if err != nil {
|
2024-04-30 17:09:14 +02:00
|
|
|
log.Printf("Error reading chunk: %v", err)
|
2024-04-30 16:27:44 +02:00
|
|
|
return
|
|
|
|
}
|
2024-04-30 17:09:14 +02:00
|
|
|
if strings.TrimSpace(chunk) == "END_OF_COMMUNICATION" {
|
2024-04-30 16:27:44 +02:00
|
|
|
fmt.Println("Client ended communication")
|
|
|
|
break
|
|
|
|
}
|
2024-04-30 17:09:14 +02:00
|
|
|
ciphertext, _ = base64.StdEncoding.DecodeString(strings.TrimSpace(chunk))
|
2024-04-30 16:27:44 +02:00
|
|
|
|
2024-04-30 17:09:14 +02:00
|
|
|
plaintextChunk, err := decrypt(ciphertext, key, iv)
|
2024-04-30 16:27:44 +02:00
|
|
|
if err != nil {
|
2024-04-30 17:09:14 +02:00
|
|
|
log.Printf("Error decrypting chunk: %v", err)
|
2024-04-30 16:27:44 +02:00
|
|
|
return
|
|
|
|
}
|
2024-04-30 17:09:14 +02:00
|
|
|
plaintext = append(plaintext, plaintextChunk...)
|
2024-04-30 16:27:44 +02:00
|
|
|
|
2024-04-30 17:09:14 +02:00
|
|
|
conn.Write([]byte("Received and decrypted chunk\n"))
|
2024-04-30 16:27:44 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 17:09:14 +02:00
|
|
|
fmt.Println("Decrypted text:", string(plaintext))
|
2024-04-30 16:27:44 +02:00
|
|
|
conn.Write([]byte("Ready for next operation\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-30 17:09:14 +02:00
|
|
|
|
2024-04-30 16:27:44 +02:00
|
|
|
func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cipherText) < aes.BlockSize {
|
|
|
|
return nil, fmt.Errorf("ciphertext too short")
|
|
|
|
}
|
|
|
|
|
|
|
|
mode := cipher.NewCBCDecrypter(block, iv)
|
|
|
|
mode.CryptBlocks(cipherText, cipherText)
|
|
|
|
|
|
|
|
cipherText = PKCS5Unpadding(cipherText)
|
|
|
|
|
|
|
|
return cipherText, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PKCS5Unpadding(data []byte) []byte {
|
|
|
|
length := len(data)
|
|
|
|
unpadding := int(data[length-1])
|
|
|
|
return data[:(length - unpadding)]
|
|
|
|
}
|
|
|
|
|