-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (106 loc) · 2.85 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"
)
func encrypt(plainstring, keystring string) string {
// Byte array of the string
plaintext := []byte(plainstring)
// Key
key := []byte(keystring)
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Empty array of 16 + plaintext length
// Include the IV at the beginning
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
// Slice of first 16 bytes
iv := ciphertext[:aes.BlockSize]
// Write 16 rand bytes to fill iv
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
// Return an encrypted stream
stream := cipher.NewCFBEncrypter(block, iv)
// Encrypt bytes from plaintext to ciphertext
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return string(ciphertext)
}
func decrypt(cipherstring string, keystring string) string {
// Byte array of the string
ciphertext := []byte(cipherstring)
// Key
key := []byte(keystring)
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Before even testing the decryption c if the size is too small
if len(ciphertext) < aes.BlockSize {
panic("Text is too short")
}
// Get the 16 byte IV
iv := ciphertext[:aes.BlockSize]
// Remove the IV from the ciphertext
ciphertext = ciphertext[aes.BlockSize:]
// Return a decrypted stream
stream := cipher.NewCFBDecrypter(block, iv)
// Decrypt bytes from ciphertext
stream.XORKeyStream(ciphertext, ciphertext)
return string(ciphertext)
}
func readline() string {
bio := bufio.NewReader(os.Stdin)
line, _, err := bio.ReadLine()
if err != nil {
fmt.Println(err)
}
return string(line)
}
func writeToFile(data, file string) {
ioutil.WriteFile(file, []byte(data), 777)
}
func readFromFile(file string) ([]byte, error) {
data, err := ioutil.ReadFile(file)
return data, err
}
func main() {
key := "testtesttesttest"
for {
fmt.Print("What would you like to do? (help/encrypt/decrypt/exit)")
line := readline()
switch line {
case "help":
fmt.Println("You can:\nencrypt\ndecrypt\nexit")
case "exit":
os.Exit(0)
case "encrypt":
fmt.Print("What would you like to encrypt: ")
line2 := readline()
ciphertext := encrypt(line2, key)
fmt.Print("What is the file name: ")
line3 := readline()
writeToFile(ciphertext, line3)
fmt.Println("Wrote to file: " + line3)
case "decrypt":
fmt.Print("What is the name of the file to decrypt: ")
line2 := readline()
if ciphertext, err := readFromFile(line2); err != nil {
// Return file not found
fmt.Println("File is not found")
} else {
plaintext := decrypt(string(ciphertext), key)
fmt.Println(plaintext)
}
}
}
}