If the data is to big chunk it
This commit is contained in:
parent
f8dcb9fe5b
commit
0801887236
35
Connect.kt
35
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)
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
52
main.go
52
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
|
||||
conn.Write([]byte("Received and decrypted chunk\n"))
|
||||
}
|
||||
|
||||
fmt.Println("Decrypted text:", string(plaintext))
|
||||
conn.Write([]byte("Received and decrypted text\n"))
|
||||
}
|
||||
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user