-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.go
80 lines (67 loc) · 1.92 KB
/
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
75
76
77
78
79
80
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
"io"
"os"
)
func EncryptFile(filePath string, encryptedPath string, secretKey string, iv string) error {
// Convert the secret key and IV from hex strings to byte slices
key, err := hex.DecodeString(secretKey)
if err != nil {
return fmt.Errorf("failed to decode secret key: %v", err)
}
ivBytes, err := hex.DecodeString(iv)
if err != nil {
return fmt.Errorf("failed to decode IV: %v", err)
}
// Create a new AES cipher block and CBC encrypter
block, err := aes.NewCipher(key)
if err != nil {
return fmt.Errorf("failed to create AES cipher: %v", err)
}
cipherStream := cipher.NewCBCEncrypter(block, ivBytes)
// Open the input file
inputFile, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open input file: %v", err)
}
defer inputFile.Close()
// Open the output file
outputFile, err := os.Create(encryptedPath)
if err != nil {
return fmt.Errorf("failed to create output file: %v", err)
}
defer outputFile.Close()
// Create buffers for reading from the input file and writing to the output
buffer := make([]byte, aes.BlockSize*1024)
for {
bytesRead, readErr := inputFile.Read(buffer)
if bytesRead > 0 {
data := buffer[:bytesRead]
// Apply padding if it's the last read
if bytesRead < len(buffer) || readErr == io.EOF {
data = pkcs7Padding(data, aes.BlockSize)
}
cipherStream.CryptBlocks(data, data)
if _, writeErr := outputFile.Write(data); writeErr != nil {
return fmt.Errorf("failed to write to output file: %v", writeErr)
}
}
if readErr != nil {
if readErr == io.EOF {
break
}
return fmt.Errorf("failed to read input file: %v", readErr)
}
}
return nil
}
func pkcs7Padding(data []byte, blockSize int) []byte {
paddingSize := blockSize - len(data)%blockSize
padding := bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)
return append(data, padding...)
}