-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathCaesar_Cipher.go
73 lines (59 loc) · 1.69 KB
/
Caesar_Cipher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* This code is an implementation of caesar cipher. Here the
plaintext is shifted by a fixed amount. The shift is chosen by
the user. Each alphabet of plaintext is then converted to decimal,
shifted, and then again converted back to ascii to form the
ciphertext. Here the input plaintext must be all in lowercase*/
package main
import (
"fmt"
"bufio"
"os"
)
// This is the encryption function
func caesar(plaintext string, shift int) string {
/* Updating the plaintext by replacing all
the spaces with underscores.*/
var message string
for j:= 0; j < len(plaintext); j++ {
if(string(plaintext[j]) == " ") {
message += string("_")
} else {
message += string(plaintext[j])
}
}
// Cipher text string
var cipher string
// Iterating through each alphabet of plaintext
for i:=0; i < len(message); i++ {
/* Shift the alphabets of plaintext. If it is
an underscore, add a space in ciphertext*/
if(string(message[i]) != "_") {
var temp int = (int(plaintext[i]) + shift - 97) % 26
temp = temp + 97
cipher += string(temp)
} else {
cipher += " "
}
}
// Return the ciphertext
return cipher
}
func main() {
// Taking plaintext as input from the user
fmt.Print("Enter a plaintext: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
plaintext := scanner.Text()
// Taking the shift amount as input from user
var shift int
fmt.Print("Enter the shift : ")
fmt.Scan(&shift)
// Calling the encryption function, print the ciphertext
var ciphertext string = caesar(plaintext, shift)
fmt.Print("\nEncrypted plaintext is: ", ciphertext, "\n")
}
/* Sample I/O:
Enter a plaintext: this is a ciphertext
Enter the shift : 7
Encrypted plaintext is: aopz pz h jpwolyalea
*/