-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (95 loc) · 3.03 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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"io"
"net/http"
)
func generateAESKey() ([]byte, error) {
key := make([]byte, 32) // AES-256 key length
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
func encryptData(data []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
ciphertext := make([]byte, aes.BlockSize+len(data))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], data)
return ciphertext, nil
}
func decryptData(ciphertext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)
return ciphertext, nil
}
func encryptHandler(w http.ResponseWriter, r *http.Request) {
key, err := generateAESKey()
if err != nil {
http.Error(w, "Failed to generate encryption key", http.StatusInternalServerError)
return
}
// Read input data from request body
inputData, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
encryptedData, err := encryptData(inputData, key)
if err != nil {
http.Error(w, "Encryption failed", http.StatusInternalServerError)
return
}
// Respond with encrypted data as hex-encoded string
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(hex.EncodeToString(encryptedData)))
}
func decryptHandler(w http.ResponseWriter, r *http.Request) {
key, err := generateAESKey() // Use the same key management strategy
if err != nil {
http.Error(w, "Failed to generate decryption key", http.StatusInternalServerError)
return
}
// Read encrypted data (hex-encoded) from request body
encryptedHex, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
encryptedData, err := hex.DecodeString(string(encryptedHex))
if err != nil {
http.Error(w, "Failed to decode hex string", http.StatusBadRequest)
return
}
decryptedData, err := decryptData(encryptedData, key)
if err != nil {
http.Error(w, "Decryption failed", http.StatusInternalServerError)
return
}
// Respond with decrypted data
w.Header().Set("Content-Type", "text/plain")
w.Write(decryptedData)
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.HandleFunc("/encrypt", encryptHandler)
http.HandleFunc("/decrypt", decryptHandler)
http.ListenAndServe(":8080", nil)
}