-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere_encrypt.go
74 lines (60 loc) · 1.81 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
package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
"os"
"regexp"
"bytes"
)
func main() {
if (len(os.Args[1:]) != 2) {
fmt.Println(" Use the command line specification mentioned in the Assignment\n <executable_name> <keys_file_name> <plaintext_file_name> \n")
} else {
key := os.Args[1]
if (len(key) > 32) {
fmt.Println(" Invalid key length \n Key length should be upto 32 characters \n")
}
file_name := os.Args[2]
file, err_data_file := ioutil.ReadFile(file_name)
/* Error handling if file wasn't opened successfully */
if (err_data_file != nil) {
log.Fatal(err_data_file)
}
/* Need to perform below operation as ReadFile() returns a byte array */
str := strings.ToUpper(string(file))
re := regexp.MustCompile("[^[:alpha:]]")
s := re.ReplaceAllLiteralString(str,"")
vignere_encryption(s,key)
}
}
func vignere_encryption(str string, key string) {
// Define an array to hold ascii characters belonging to the key
var key_ascii []int
key_length := len(key)
key_ascii = make([]int, key_length, key_length)
for j:=0; j < key_length; j++ {
key_ascii[j] = int(key[j])
}
// Define an array to hold ascii characters belonging to the plaintext
var ascii []int
str_length := len(str)
ascii = make([]int, str_length, str_length)
for i:=0 ; i < str_length; i++ {
for z:=0; z < key_length; z++ {
/* Logic here is I try all commbinations and check if the mod of the
index in the plaintext matches the index for the key. If yes, I encode
that character */
if ((i % key_length) == z) {
ascii[i] = (((int(str[i]) - 65) + (key_ascii[z] -65)) % 26) + 65
}
}
}
// Convert the ascii array back to a string and write to a buffer
var buffer bytes.Buffer
for k:= 0; k < len(ascii); k++ {
buffer.WriteString(string(ascii[k]))
}
fmt.Println(buffer.String())
}