-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_decryption.go
59 lines (49 loc) · 1.24 KB
/
data_decryption.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"os"
)
func main() {
// Read the encrypted JSON data from the file
encryptedFile, err := os.Open("encrypted_data.json")
if err != nil {
fmt.Println(err)
}
defer encryptedFile.Close()
var encryptedData []byte
if err := json.NewDecoder(encryptedFile).Decode(&encryptedData); err != nil {
fmt.Println(err)
}
// Generate a key and an initialization vector (IV) for decryption
key := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
fmt.Println(err)
}
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
fmt.Println(err)
}
// Use the key and IV to create a CBC cipher
block, err := aes.NewCipher(key)
if err != nil {
fmt.Println(err)
}
stream := cipher.NewCTR(block, iv)
// Decrypt the encrypted data using the cipher
decryptedData := make([]byte, len(encryptedData))
stream.XORKeyStream(decryptedData, encryptedData)
// Write the decrypted data to a new file
decryptedFile, err := os.Create("decrypted_data.json")
if err != nil {
fmt.Println(err)
}
defer decryptedFile.Close()
if _, err := decryptedFile.Write(decryptedData); err != nil {
fmt.Println(err)
}
}