From cb34f6f9f4015bdf9dd0ad26b069e8b018282482 Mon Sep 17 00:00:00 2001 From: Saravanan SJ Date: Sat, 31 Oct 2015 16:20:57 -0400 Subject: [PATCH 1/2] added playfair cipher in Go lang --- Playfair_Cipher/Go/sjs7007/playfair.go | 182 +++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 Playfair_Cipher/Go/sjs7007/playfair.go diff --git a/Playfair_Cipher/Go/sjs7007/playfair.go b/Playfair_Cipher/Go/sjs7007/playfair.go new file mode 100644 index 00000000..6d1805e5 --- /dev/null +++ b/Playfair_Cipher/Go/sjs7007/playfair.go @@ -0,0 +1,182 @@ +/* + +This is an implementation of the playfair cipher for encryption and decryption. + +Input : +key : which is a lowercase string +msg : the message to be encrypted which is again a lower case string + +Output : +Encrypted Message +Decryption of the encrypted message using the cipher + +References : https://en.wikipedia.org/wiki/Playfair_cipher + +Sample Output : +go run playfair.go + +Enter the key with no spaces : mykey +Enter the message to be encrypted with no spaces : thisismymessage +Input : thisismymessage +Encrypted Msg : ponqnqykyaxetefa +Decrypted Msg : thisismymesxsage +*/ + +package main + +import "fmt" +import "strings" +//import "math" + +func main() { + var key = "" + fmt.Printf("Enter the key with no spaces : ") + _, err := fmt.Scanf("%s", &key) // _ is blank identifier + if err != nil { + fmt.Println("Error : " + err.Error()) + } + + var inputMsg = "" + fmt.Printf("Enter the message to be encrypted with no spaces : ") + _, err = fmt.Scanf("%s", &inputMsg) // _ is blank identifier + if err != nil { + fmt.Println("Error : " + err.Error()) + } + + var mat = "" + for i:=0 ; i<=25;i++ { + key = key + string(97+i) + } + for i := 0; i < len(key); i++ { + //fmt.Println(string(key[i])=="a") + var tmp = string(key[i]) + if(tmp=="j") { + tmp="i" + } + if(!strings.Contains(mat,tmp)) { + mat = mat + tmp + } + } + //fmt.Println(mat) + playFairMatrix := [5][5]string{} + var count = 0 + for i:=0 ; i<5;i++ { + for j:=0 ; j<5 ; j++ { + playFairMatrix[i][j]=string(mat[count]) + count++ + } + } +// fmt.Println(playFairMatrix) + var encrypted = encryptMsg(playFairMatrix,inputMsg) + var decrypted = decryptMsg(playFairMatrix,encrypted) + fmt.Printf("Input : %s\n",inputMsg) + fmt.Printf("Encrypted Msg : %s\n",encrypted) + fmt.Printf("Decrypted Msg : %s\n",decrypted) +} + +func encryptMsg(playFairMatrix [5][5]string,input string) string { + var x= "" + for i:=0 ; i Date: Sat, 31 Oct 2015 16:26:25 -0400 Subject: [PATCH 2/2] added Lamport's logical clock syncronization in Go lang --- .../Lamport_Logical_Clock_synchronization.go | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Lamport_Logical_Clock_synchronization/Go/sjs7007/Lamport_Logical_Clock_synchronization.go diff --git a/Lamport_Logical_Clock_synchronization/Go/sjs7007/Lamport_Logical_Clock_synchronization.go b/Lamport_Logical_Clock_synchronization/Go/sjs7007/Lamport_Logical_Clock_synchronization.go new file mode 100644 index 00000000..aa159420 --- /dev/null +++ b/Lamport_Logical_Clock_synchronization/Go/sjs7007/Lamport_Logical_Clock_synchronization.go @@ -0,0 +1,116 @@ +/* +Simulation of Lamport's Logical Clock Syncronization +Sample Output Simulation : https://www.showterm.io/556636ec7caa4687680c0 +References : Distributed systems: Principles and Paradigms, Andrew S. Tanenbaum + +The program takes the following input : + nProcesses : total no. of processes in the system + nIterations : no. of counter/clock iterations for which the output is generated + clockConstant : the constant rate by which counter/clock is incremented on each tick + +Output : +The program randomly generates a message between any two distinct processes in the system on every 3rd iteration. The message is added to a message queue and read by the recipient on the next iteration. Based on the timestamp at which the message was sent and current time on the receiver, clock time of receiver is adjusted if timestamp of message is >= receiver time else left the same. >= because it will take a finite amount of time to send the message always. +*/ + +package main + +import ( + "fmt" //for println,scanf + "math/rand" //for random number generator + "time" //for sleep +) + +func main() { + var nProcesses, nIterations int // Number of processes, Number of iterations + fmt.Printf("Enter number of Processes and iterations you want to simulate : ") + _, err := fmt.Scanf("%d %d", &nProcesses, &nIterations) // _ is blank identifier + if err != nil { + fmt.Println("Error : " + err.Error()) + } + clockTable := [][]int{} //table to store final counter/logical clock values + var clockConstants []int //slice to store values by which clock are incremented on each iteration + clockConstants = make([]int, nProcesses, nProcesses) + + fmt.Printf("Enter the clock constants for each process : ") //user input for clock constants + for i := 0; i < nProcesses; i++ { + _, err := fmt.Scanf("%d", &clockConstants[i]) + if err != nil { + fmt.Println("Error : " + err.Error()) + } + } + + var initClock []int + initClock = make([]int, nProcesses, nProcesses) + clockTable = append(clockTable, initClock) + var msgQueue []int //used to queue messages between processes. + + //Print 1st iteration + for i := 0; i < nProcesses; i++ { + fmt.Printf(" P%d", i) + } + fmt.Println() + for j := 1; j <= nProcesses; j++ { + fmt.Printf("%4d", clockTable[0][j-1]) + } + fmt.Println() + + //Print other iterations + for i := 1; i < nIterations; i++ { //first iteration already passed so start from 2nd + + time.Sleep(2000 * time.Millisecond) //sleep to slow the program down + + var temp []int + temp = make([]int, nProcesses, nProcesses) + for j := 1; j <= nProcesses; j++ { + temp[j-1] = clockTable[i-1][j-1] + clockConstants[j-1] + } + clockTable = append(clockTable, temp) + /* generate messages to be sent in every 3rd iteration */ + if (i+1)%3 == 0 { + var senderID, receiverID int + r := rand.New(rand.NewSource(time.Now().UTC().UnixNano())) + senderID = r.Intn(nProcesses) + receiverID = r.Intn(nProcesses) + /*if receiver turns out to be same as sender, find new receiver*/ + for receiverID == senderID { + receiverID = r.Intn(nProcesses) + } + msgQueue = append(msgQueue, senderID, receiverID) // add message to queue + fmt.Printf("Message sent by P%d to P%d at time t=%d at P1.\n", senderID, receiverID, clockTable[i-1][senderID]) + } + for j := 0; j < nProcesses; j++ { //display clock values + fmt.Printf("%4d", clockTable[i][j]) + } + fmt.Println() + + for len(msgQueue) >= 2 { + var senderID, receiverID int = msgQueue[0], msgQueue[1] + var sendTime, recTime int = clockTable[i-1][senderID], clockTable[i][receiverID] + if clockTable[i-1][senderID] >= clockTable[i][receiverID] { + fmt.Printf("Message received by P%d at time=%d. Since %d <= %d , we adjust time from %d to %d.\n", receiverID, recTime, recTime, sendTime, recTime, (sendTime + 1)) + for j := 0; j < nProcesses; j++ { + fmt.Printf("%4d", clockTable[i][j]) + } + fmt.Printf(" -->") //display changed clock values + clockTable[i][receiverID] = clockTable[i-1][senderID] + 1 + for j := 0; j < nProcesses; j++ { + fmt.Printf("%4d", clockTable[i][j]) + } + fmt.Printf("\n") + + } else { + fmt.Printf("Message received by P%d at time=%d. Since %d >= %d , no time adjustment needed.\n", receiverID, recTime, recTime, sendTime) + for j := 0; j < nProcesses; j++ { + fmt.Printf("%4d", clockTable[i][j]) + } + fmt.Printf(" =") //display clock values without change + for j := 0; j < nProcesses; j++ { + fmt.Printf("%4d", clockTable[i][j]) + } + fmt.Printf("\n") + } + msgQueue = append(msgQueue[:0], msgQueue[2:]...) //remove sender and receiver from msg queue + time.Sleep(2000 * time.Millisecond) + } + } +}