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() {
|
suspend fun main() {
|
||||||
val host = "192.168.90.151"
|
val host = "192.168.90.151"
|
||||||
val port = 8080
|
val port = 8080
|
||||||
|
|
||||||
val secureRandom = SecureRandom()
|
val secureRandom = SecureRandom()
|
||||||
val keyBytes = ByteArray(16)
|
val keyBytes = ByteArray(16)
|
||||||
secureRandom.nextBytes(keyBytes)
|
|
||||||
val key = SecretKeySpec(keyBytes, "AES")
|
|
||||||
|
|
||||||
val ivBytes = ByteArray(16)
|
val ivBytes = ByteArray(16)
|
||||||
|
secureRandom.nextBytes(keyBytes)
|
||||||
secureRandom.nextBytes(ivBytes)
|
secureRandom.nextBytes(ivBytes)
|
||||||
|
val key = SecretKeySpec(keyBytes, "AES")
|
||||||
val iv = IvParameterSpec(ivBytes)
|
val iv = IvParameterSpec(ivBytes)
|
||||||
|
|
||||||
val algorithm = "AES/CBC/PKCS5Padding"
|
val algorithm = "AES/CBC/PKCS5Padding"
|
||||||
val inputText = "abcdefghigklmnopqrstuvwxyz0123456789"
|
|
||||||
|
var sendData = "Hello, World!"
|
||||||
|
|
||||||
|
val chunkSize = 45
|
||||||
|
val chunks = sendData.chunked(chunkSize)
|
||||||
|
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
Socket(host, port).use { socket ->
|
Socket(host, port).use { socket ->
|
||||||
@ -58,30 +59,24 @@ class Connect {
|
|||||||
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
|
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
|
||||||
|
|
||||||
val encodedKey = encodeBase64(keyBytes)
|
val encodedKey = encodeBase64(keyBytes)
|
||||||
println(keyBytes.toHexString())
|
|
||||||
writer.println(encodedKey)
|
writer.println(encodedKey)
|
||||||
|
reader.readLine()
|
||||||
println(reader.readLine())
|
|
||||||
|
|
||||||
val encodedIV = encodeBase64(ivBytes)
|
val encodedIV = encodeBase64(ivBytes)
|
||||||
println(ivBytes.toHexString())
|
|
||||||
writer.println(encodedIV)
|
writer.println(encodedIV)
|
||||||
|
reader.readLine()
|
||||||
|
|
||||||
println(reader.readLine())
|
for (chunk in chunks) {
|
||||||
|
val cipherText = encrypt(algorithm, chunk, key, iv)
|
||||||
val cipherText = encrypt(algorithm, inputText, key, iv)
|
|
||||||
println(cipherText)
|
|
||||||
writer.println(cipherText)
|
writer.println(cipherText)
|
||||||
|
reader.readLine()
|
||||||
|
}
|
||||||
|
|
||||||
writer.println("END_OF_COMMUNICATION")
|
writer.println("END_OF_COMMUNICATION")
|
||||||
|
reader.readLine()
|
||||||
println(reader.readLine())
|
|
||||||
|
|
||||||
println("Client: Ready for next operation")
|
println("Client: Ready for next operation")
|
||||||
|
|
||||||
writer.println("Ready for next operation")
|
writer.println("Ready for next operation")
|
||||||
|
reader.readLine()
|
||||||
println(reader.readLine())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
52
main.go
52
main.go
@ -16,7 +16,6 @@ func main() {
|
|||||||
host := "0.0.0.0"
|
host := "0.0.0.0"
|
||||||
port := 8080
|
port := 8080
|
||||||
|
|
||||||
// Listen for incoming connections
|
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error listening: %v", err)
|
log.Fatalf("Error listening: %v", err)
|
||||||
@ -46,65 +45,46 @@ func handleConnection(conn net.Conn) {
|
|||||||
log.Printf("Error reading key: %v", err)
|
log.Printf("Error reading key: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
key, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(keyData))
|
||||||
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"))
|
|
||||||
conn.Write([]byte("Received key\n"))
|
conn.Write([]byte("Received key\n"))
|
||||||
|
|
||||||
ivData, err := bufio.NewReader(conn).ReadString('\n')
|
ivData, _ := bufio.NewReader(conn).ReadString('\n')
|
||||||
if err != nil {
|
iv, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(ivData))
|
||||||
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)
|
|
||||||
conn.Write([]byte("Received IV\n"))
|
conn.Write([]byte("Received IV\n"))
|
||||||
|
|
||||||
|
var ciphertext []byte
|
||||||
|
var chunk string
|
||||||
|
var plaintext []byte
|
||||||
|
|
||||||
for {
|
for {
|
||||||
textData, err := bufio.NewReader(conn).ReadString('\n')
|
chunk, err = bufio.NewReader(conn).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading text: %v", err)
|
log.Printf("Error reading chunk: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if strings.TrimSpace(chunk) == "END_OF_COMMUNICATION" {
|
||||||
if strings.TrimSpace(textData) == "END_OF_COMMUNICATION" {
|
|
||||||
fmt.Println("Client ended communication")
|
fmt.Println("Client ended communication")
|
||||||
break
|
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 {
|
if err != nil {
|
||||||
log.Printf("Error decoding text: %v", err)
|
log.Printf("Error decrypting chunk: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
plaintext = append(plaintext, plaintextChunk...)
|
||||||
|
|
||||||
plaintext, err := decrypt(cipherText, key, iv)
|
conn.Write([]byte("Received and decrypted chunk\n"))
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error decrypting text: %v", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Decrypted text:", string(plaintext))
|
fmt.Println("Decrypted text:", string(plaintext))
|
||||||
conn.Write([]byte("Received and decrypted text\n"))
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.Write([]byte("Ready for next operation\n"))
|
conn.Write([]byte("Ready for next operation\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
|
func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
|
||||||
block, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user