-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes_encryption_decryption.go
More file actions
137 lines (114 loc) · 3.34 KB
/
Copy pathaes_encryption_decryption.go
File metadata and controls
137 lines (114 loc) · 3.34 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
var choice string
for {
// 询问用户是要加密还是解密
fmt.Printf("\n\u001B[32m请选择操作: \u001B[0m\n")
fmt.Println("1: 加密")
fmt.Println("2: 解密")
fmt.Print("请输入选择 (1 或 2): ")
choice, _ = reader.ReadString('\n')
choice = strings.TrimSpace(choice)
if choice == "1" || choice == "2" {
break
}
fmt.Println("\u001B[31m无效的选择,请输入 1 或 2\u001B[0m")
}
// 输入密钥
if choice == "1" {
fmt.Printf("\n\u001B[32m请设置密钥(32个字符): \u001B[0m\n")
} else {
fmt.Printf("\n\u001B[32m请输入密钥(32个字符): \u001B[0m\n")
}
key, _ := reader.ReadString('\n')
key = strings.TrimSpace(key)
if len(key) != 32 {
fmt.Println("\u001B[31m密钥长度必须是32个字符\u001B[0m")
return
}
if choice == "1" {
encryptData(reader, key)
} else {
decryptData(reader, key)
}
}
func encryptData(reader *bufio.Reader, key string) {
fmt.Printf("\n\u001B[32m请输入要加密的数据,输入'###'结束:\u001B[0m\n")
var lines []string
for {
fmt.Print("> ")
line, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, "读取错误:", err)
break
}
line = strings.TrimSuffix(line, "\n")
if line == "###" {
break
}
lines = append(lines, line)
}
data := strings.Join(lines, "\n")
// 检查数据是否为空
if data == "" {
fmt.Println("\u001B[31m错误:要加密的数据不能为空\u001B[0m")
return
}
// 加密过程
block, err := aes.NewCipher([]byte(key))
if err != nil {
panic(err)
}
// 生成 IV
iv := make([]byte, block.BlockSize())
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
cbc := cipher.NewCBCEncrypter(block, iv)
padding := block.BlockSize() - len(data)%block.BlockSize()
paddedData := append([]byte(data), bytes.Repeat([]byte{byte(padding)}, padding)...)
encrypted := make([]byte, len(paddedData))
cbc.CryptBlocks(encrypted, paddedData)
encryptedDataWithIv := append(iv, encrypted...)
fmt.Printf("\n\u001B[32m加密后的数据(base64格式,包含IV): \u001B[0m\n%v\n\n", base64.StdEncoding.EncodeToString(encryptedDataWithIv))
}
func decryptData(reader *bufio.Reader, key string) {
fmt.Printf("\n\u001B[32m请输入密文(base64格式,包含IV): \u001B[0m\n")
encryptedBase64, _ := reader.ReadString('\n')
encryptedBase64 = strings.TrimSpace(encryptedBase64)
encryptedDataWithIv, err := base64.StdEncoding.DecodeString(encryptedBase64)
if err != nil {
fmt.Println("\u001B[32mbase64解码错误:\u001B[0m", err)
return
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
panic(err)
}
if len(encryptedDataWithIv) < block.BlockSize() {
fmt.Println("\u001B[32m加密数据太短\u001B[0m")
return
}
iv := encryptedDataWithIv[:block.BlockSize()]
encryptedData := encryptedDataWithIv[block.BlockSize():]
cbcDecrypter := cipher.NewCBCDecrypter(block, iv)
decrypted := make([]byte, len(encryptedData))
cbcDecrypter.CryptBlocks(decrypted, encryptedData)
// 移除填充
paddingSize := decrypted[len(decrypted)-1]
decrypted = decrypted[:len(decrypted)-int(paddingSize)]
fmt.Printf("\n\u001B[32m解密后的原始数据: \u001B[0m\n%v\n\n", string(decrypted))
}