-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
87 lines (73 loc) · 2.13 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
81
82
83
84
85
86
87
// encrypt.go
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
)
// EncryptPassword encrypts a plaintext password using AES-256 with the provided PSK.
func EncryptPassword(password, psk string) (string, error) {
// Convert PSK to 32 bytes (AES-256 key size)
key := []byte(psk)
if len(key) > 32 {
key = key[:32]
} else if len(key) < 32 {
padded := make([]byte, 32)
copy(padded, key)
key = padded
}
// Create AES block cipher
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("failed to create cipher: %w", err)
}
// Generate a random IV
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", fmt.Errorf("failed to generate IV: %w", err)
}
// Encrypt the password
ciphertext := make([]byte, len(password))
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext, []byte(password))
// Prepend IV to ciphertext
combined := append(iv, ciphertext...)
// Encode the result in base64
return base64.StdEncoding.EncodeToString(combined), nil
}
// DecryptPassword decrypts an AES-256 encrypted password using the provided PSK.
func DecryptPassword(encryptedBase64, psk string) (string, error) {
// Convert PSK to 32 bytes (AES-256 key size)
key := []byte(psk)
if len(key) > 32 {
key = key[:32]
} else if len(key) < 32 {
padded := make([]byte, 32)
copy(padded, key)
key = padded
}
// Decode the base64 encoded ciphertext
combined, err := base64.StdEncoding.DecodeString(encryptedBase64)
if err != nil {
return "", fmt.Errorf("failed to decode base64: %w", err)
}
if len(combined) < aes.BlockSize {
return "", fmt.Errorf("ciphertext too short")
}
// Extract the IV and ciphertext
iv := combined[:aes.BlockSize]
ciphertext := combined[aes.BlockSize:]
// Create AES block cipher
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("failed to create cipher: %w", err)
}
// Decrypt the data
plaintext := make([]byte, len(ciphertext))
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(plaintext, ciphertext)
return string(plaintext), nil
}