This commit is contained in:
jiangyong 2026-06-18 09:14:18 +08:00
parent c86c3b40a3
commit 1cdf6cb507
1 changed files with 7 additions and 7 deletions

View File

@ -66,38 +66,38 @@ func DecryptID(data, salt string) int64 {
}
// 先压缩再加密(适合长文本)
func Encrypt(plaintext string, key string) (string, error) {
func Encrypt(plaintext string, key string) string {
// 1. 压缩
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
if _, err := gz.Write([]byte(plaintext)); err != nil {
return "", err
return ""
}
if err := gz.Close(); err != nil {
return "", err
return ""
}
compressed := buf.Bytes()
// 2. 加密使用AES-GCM
block, err := aes.NewCipher([]byte(Md5(key)))
if err != nil {
return "", err
return ""
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
return ""
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
return ""
}
ciphertext := gcm.Seal(nonce, nonce, compressed, nil)
// 使用RawURLEncoding
return base64.RawURLEncoding.EncodeToString(ciphertext), nil
return base64.RawURLEncoding.EncodeToString(ciphertext)
}
func Decrypt(encoded string, key string) (string, error) {