-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (72 loc) · 2.19 KB
/
main.go
File metadata and controls
85 lines (72 loc) · 2.19 KB
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
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"log"
"github.com/gin-gonic/gin"
)
type SecureSubmission struct {
EncryptedData string `form:"encryptedData"`
IV string `form:"iv"`
Key string `form:"key"`
}
func main() {
r := gin.Default()
gin.SetMode(gin.ReleaseMode) // Set Gin to release mode for production
r.Use(gin.Logger())
r.Use(gin.Recovery())
r.StaticFile("/", "./form.html")
r.StaticFile("/form.html", "./form.html")
r.POST("/submit-secure", func(c *gin.Context) {
var submission SecureSubmission
if err := c.ShouldBind(&submission); err != nil {
log.Println("Bind error:", err)
c.JSON(400, gin.H{"error": err.Error()})
return
}
// Decode Base64 strings
encryptedDataBytes, err := base64.StdEncoding.DecodeString(submission.EncryptedData)
if err != nil {
log.Println("Encrypted data decode error:", err)
c.JSON(500, gin.H{"error": "Failed to decode encrypted data: " + err.Error()})
return
}
ivBytes, err := base64.StdEncoding.DecodeString(submission.IV)
if err != nil {
log.Println("IV decode error:", err)
c.JSON(500, gin.H{"error": "Failed to decode IV: " + err.Error()})
return
}
keyBytes, err := base64.StdEncoding.DecodeString(submission.Key)
if err != nil {
log.Println("Key decode error:", err)
c.JSON(500, gin.H{"error": "Failed to decode key: " + err.Error()})
return
}
// Create AES cipher block
block, err := aes.NewCipher(keyBytes)
if err != nil {
log.Println("Cipher block error:", err)
c.JSON(500, gin.H{"error": "Failed to create cipher block: " + err.Error()})
return
}
// Create GCM (Galois/Counter Mode)
aesGCM, err := cipher.NewGCM(block)
if err != nil {
log.Println("GCM creation error:", err)
c.JSON(500, gin.H{"error": "Failed to create GCM: " + err.Error()})
return
}
// Decrypt the data
plaintext, err := aesGCM.Open(nil, ivBytes, encryptedDataBytes, nil)
if err != nil {
log.Println("Decryption error:", err)
c.JSON(500, gin.H{"error": "Failed to decrypt data: " + err.Error()})
return
}
log.Println("Decrypted data:", string(plaintext))
c.JSON(200, gin.H{"decryptedData": string(plaintext)})
})
r.Run(":8080")
} //main