Hello friends, after Cryptography Part I back with my first example of crypto. Golang has a package AES. In the further blog, I will write a Client Server application, which runs on the encrypted data. So, ready to share some secret over network. Shhh…..
Client Code
package main import ( "net" "fmt" "crypto/aes" ) //this example just tests the data length of block size 16 var key = "12345678912345678912345678912345" func encryptWriteData(conn net.Conn, data []byte) { //creating a block cipher block, err := aes.NewCipher([]byte(key)) if err != nil { fmt.Println(err.Error()) } fmt.Println(block.BlockSize()) mydata := make([]byte,16) if len(data) > 16 { fmt.Println("Max 16 bytes can be sent in this example...") return } //copy bytes because, even if message is less then 16 bytes, we need 16 bytes. copy(mydata,data) block.Encrypt(mydata,mydata) //sending encrypted data conn.Write(mydata) } func printDecryptedData(conn net.Conn) { data := make([]byte,16) _,err := conn.Read(data) if err != nil { fmt.Println(err.Error()) } block, err := aes.NewCipher([]byte(key)) if err != nil { fmt.Println(err.Error()) } mydata := make([]byte,16) if len(data) > 16 { fmt.Println("Max 16 bytes can be sent in this example...") return } //copy bytes because, even if message is less then 16 bytes, we need 16 bytes. copy(mydata,data) block.Decrypt(mydata,mydata) fmt.Println(string(mydata)) } func main() { conn,err := net.Dial("tcp","127.0.0.1:4908") if err != nil { fmt.Println(err.Error()) return } var input int for { fmt.Scanf("%d",&input) switch input { case 1: encryptWriteData(conn,[]byte("Hello Server")) case 2: encryptWriteData(conn,[]byte("Password")) case 3: encryptWriteData(conn,[]byte("1234")) case 4: encryptWriteData(conn,[]byte("Quit")) default: encryptWriteData(conn,[]byte("Invalid")) } printDecryptedData(conn) } }
Server Code
package main import ( "net" "fmt" "crypto/aes" ) var key = "12345678912345678912345678912345" func encryptWriteData(conn net.Conn, data []byte) { //creating a block cipher block, err := aes.NewCipher([]byte(key)) if err != nil { fmt.Println(err.Error()) } mydata := make([]byte,16) if len(data) > 16 { fmt.Println("Max 16 bytes can be sent in this example...") return } //copy bytes because, even if message is less then 16 bytes, we need 16 bytes. copy(mydata,data) block.Encrypt(mydata,mydata) //sending encrypted data conn.Write(mydata) } func printDecryptedData(conn net.Conn) int { data := make([]byte,16) _,err := conn.Read(data) if err != nil { fmt.Println(err.Error()) return -1 } block, err := aes.NewCipher([]byte(key)) if err != nil { fmt.Println(err.Error()) return -1 } mydata := make([]byte,16) if len(data) > 16 { fmt.Println("Max 16 bytes can be sent in this example...") return -1 } //copy bytes because, even if message is less then 16 bytes, we need 16 bytes. copy(mydata,data) block.Decrypt(mydata,mydata) k := string(mydata) fmt.Println(k) if k[:4] == "Quit" { conn.Close() return -1 } encryptWriteData(conn,[]byte("Server Message")) return 0 } func processRequest(conn net.Conn){ for { if -1 == printDecryptedData(conn) { fmt.Println("Connection closed...") return } } } func main() { conn,err := net.Listen("tcp","127.0.0.1:4908") if err != nil { fmt.Println(err.Error()) return } fmt.Println("Server started...") for { c, err := conn.Accept() if err != nil { fmt.Println(err.Error()) return } go processRequest(c) } a := 1 fmt.Scanf("%d",&a) }
==========================================================================
Our client and server, are connected over a network, in our case its local, you can change IP of course. That’s how it works:
- Client send message to server.
- Server sends a encrypted reply to it.
- On Client sending Quit, server quits.
Code is commented, soon I will upload it too my Github. Happy Coding.
Advertisements