-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere-encrypt.go
115 lines (90 loc) · 2.88 KB
/
vigenere-encrypt.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
106
107
108
109
110
111
112
113
114
115
package main
import "os"
import "strings"
import (
"fmt"
"io/ioutil"
"log"
"regexp"
)
func main(){
arguments := os.Args;
encipherment_key := arguments[1];
plaintext_file_name := arguments[2];
isAlpha := regexp.MustCompile(`^[A-Z]`).MatchString
if(!isAlpha(encipherment_key)){
fmt.Println("The Key should contain all uppercase letters.");
return;
}
//Declaring the Alphabet Array
alphabetArray := []string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
//fmt.Println(alphabetArray);
//Putting the File Content in a variable
file_content, err := ioutil.ReadFile(plaintext_file_name);
if err != nil {
fmt.Print(err);
}
file, err := os.Open(plaintext_file_name)
stat, err := file.Stat()
if err != nil {
return
}
if(stat.Size()>100){
fmt.Println("The File Size is greater than 100Kb");
// return;
}
//Converting content of file to string
str := string(file_content);
// Make a Regex to say we only want
reg, err := regexp.Compile("[^a-zA-Z]+")
if err != nil {
log.Fatal(err)
}
processedString := reg.ReplaceAllString(str, "")
processedString = strings.ToUpper(processedString);
//fmt.Println(processedString);
//Putting the string into a character array
contentArray := strings.Split(processedString, "");
//Putting the Key string to array
keyArray := strings.Split(encipherment_key, "");
//Creating Key Stream Array
length := len(contentArray);
streamArray := make([]string,length);
finalArray := make([]string,length);
j := 0;
for i :=0 ; i<len(contentArray) ; i++ {
streamArray[i] = keyArray[j];
j++;
if j == len(keyArray) {
j = 0;
}
}
//fmt.Println(streamArray);
//Iterating Over the Content Array and Stream Array and Fetching the new Encrypted Alphabets
for i:=0 ; i< len(contentArray) ; i++{
alphabetMap := map[string]int{"A":1,"B":2,"C":3,"D":4,"E":5,"F":6,"G":7,"H":8,"I":9,"J":10,"K":11,"L":12,"M":13,"N":14,"O":15,"P":16,"Q":17,"R":18,"S":19,"T":20,"U":21,"V":22,"W":23,"X":24,"Y":25,"Z":26};
positionContentAlphabet := 0;
positionStreamAlphabet :=0;
newPosition :=0;
characterContent := contentArray[i];
characterStream := streamArray[i];
positionContentAlphabet = alphabetMap[characterContent];
positionStreamAlphabet = alphabetMap[characterStream];
sum := (positionContentAlphabet) + (positionStreamAlphabet);
//fmt.Println(sum);
if sum > 26 {
newPosition = sum - 26 -1;
//fmt.Println(newPosition);
}else {
newPosition = sum -1;
//fmt.Println(newPosition);
}
//fmt.Println(newPosition);
if(newPosition == 0){
newPosition = 26;
}
finalArray[i] = alphabetArray[newPosition-1];
}
encryptedString := strings.Join(finalArray,"");
fmt.Println(encryptedString);
}