-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesarCipher.go
105 lines (96 loc) · 3.55 KB
/
caesarCipher.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
###=====================================================================###
### Author: Avinash Ghadshi ###
### Language: GO 1.14 ###
### Repository: https://github.com/avinash-ghadshi/cryptographyScripts ###
### Details: This script demonstrates the working of Caesor Cipher ###
### Usage: ###
### go run caesarCipher.go ###
###=====================================================================###
*/
package main
import (
"bufio"
"fmt"
"os"
"strings"
"strconv"
_"reflect"
)
func encrypt(text string, key int) {
var aEncryptedText = make([]string, len(text))
aText := strings.Split(text,"")
for i := 0; i < len(aText); i++ {
tmp := []rune(aText[i])[0]
if tmp == 9 || tmp == 32 {
aEncryptedText = append(aEncryptedText, aText[i])
} else if tmp >= rune('a') && tmp <= rune('z') {
if tmp + rune(key) > rune('z') {
aEncryptedText = append(aEncryptedText,string(tmp + rune(key) - 26))
} else {
aEncryptedText = append(aEncryptedText,string(tmp + rune(key)))
}
} else if tmp >= rune('A') && tmp <= rune('Z') {
if tmp + rune(key) > rune('Z') {
aEncryptedText = append(aEncryptedText,string(tmp + rune(key) - 26))
} else {
aEncryptedText = append(aEncryptedText,string(tmp + rune(key)))
}
}
}
sEncryptedText := strings.Join(aEncryptedText, "")
fmt.Println("[+] Encrypted Text = "+sEncryptedText)
}
func decrypt(text string, key int) {
var aEncryptedText = make([]string, len(text))
aText := strings.Split(text,"")
for i := 0; i < len(aText); i++ {
tmp := []rune(aText[i])[0]
if tmp == 9 || tmp == 32 {
aEncryptedText = append(aEncryptedText, aText[i])
} else if tmp >= rune('a') && tmp <= rune('z') {
if tmp - rune(key) < rune('a') {
aEncryptedText = append(aEncryptedText,string(tmp - rune(key) + 26))
} else {
aEncryptedText = append(aEncryptedText,string(tmp - rune(key)))
}
} else if tmp >= rune('A') && tmp <= rune('Z') {
if tmp - rune(key) < rune('A') {
aEncryptedText = append(aEncryptedText,string(tmp - rune(key) + 26))
} else {
aEncryptedText = append(aEncryptedText,string(tmp - rune(key)))
}
}
}
sEncryptedText := strings.Join(aEncryptedText, "")
fmt.Println("[+] Plain Text = "+sEncryptedText)
}
func main() {
fmt.Println("###------------------------------------NOTE----------------------------------------###")
fmt.Println("### Text should not contains numbers and special characters other than Space / Tab.###")
fmt.Println("### Key should be number and between 1 to 25. ###")
fmt.Println("### Action should be 1 for encrytion and 2 for decryption. ###")
fmt.Println("###--------------------------------------------------------------------------------###\n")
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text to encrypt / decrypt: ")
text,_ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
fmt.Print("Enter key to encrypt / decrypt the text: ")
skey,_ := reader.ReadString('\n')
key,_ := strconv.Atoi(strings.Replace(skey, "\n", "", -1))
if key < 1 || key > 25 {
fmt.Println("Invalid Key\nPlease read above NOTE")
os.Exit(1)
}
fmt.Print("Enter action (1: Encryption\t2: Decryption): ")
action,_ := reader.ReadString('\n')
action = strings.Replace(action, "\n", "", -1)
switch action {
case "1":
encrypt(text,key)
case "2":
decrypt(text,key)
default:
fmt.Println("Invalid action.\nAction should be 1 or 2")
}
}