Compare commits

..

No commits in common. "da4d1ebf3a5b04d100e246d5a8cb46e4563d917e" and "c083d6e8e00cd5decf82b8e178c2579c8f878850" have entirely different histories.

3 changed files with 9 additions and 72 deletions

View File

@ -3,7 +3,7 @@
Golang 中可以使用的常用辅助功能工具箱。主要配备以下功能:
- 加解密算法
- [x] 螺旋随机密钥自解密算法
- [ ] 螺旋随机密钥自解密算法
- [x] AES-CBC 便捷加解密算法
- [x] No Padding
- [x] ZerosPadding

View File

@ -1,56 +0,0 @@
// 提供自定义的随机密钥自解密加密算法支持算法代号Spiral。
package spiral
import (
"errors"
"fmt"
"strings"
"archgrid.xyz/ag/toolsbox/encryption"
"archgrid.xyz/ag/toolsbox/encryption/aes"
"archgrid.xyz/ag/toolsbox/hash/sha512"
verifyCode "archgrid.xyz/ag/toolsbox/random/verify_code"
"archgrid.xyz/ag/toolsbox/serialize/base64"
)
// 根据给定的密钥字符串生成加解密使用的密钥。
func generateKey(key string) []byte {
keyBytes := sha512.Sha512([]byte(key))
return keyBytes[4:36]
}
// 对给定的数据进行加密。
func Encrypt(data string) (string, error) {
key := verifyCode.RandStr(20)
keyBytes := generateKey(key)
cipherData, err := aes.Encrypt([]byte(data), keyBytes, encryption.PKCS7Padding)
if err != nil {
return "", fmt.Errorf("加密计算失败,%w", err)
}
var result strings.Builder
result.WriteString("[")
result.WriteString(key)
result.WriteString(base64.ToBase64(cipherData))
return result.String(), nil
}
// 对给定的数据进行解密。
func Decrypt(data string) (string, error) {
if message, found := strings.CutPrefix(data, "["); found {
if len(message) > 20 {
keySeed := message[:20]
key := generateKey(keySeed)
cipherData, err := base64.FromBase64(message[20:])
if err != nil {
return "", fmt.Errorf("密文损坏无法解析,%w", err)
}
plainText, err := aes.Decrypt(cipherData, key, encryption.PKCS7Padding)
if err != nil {
return "", fmt.Errorf("密文解密计算失败,%w", err)
}
return string(plainText), nil
}
return "", errors.New("密文缺损,无法完成解密。")
}
return data, nil
}

View File

@ -5,7 +5,6 @@ import (
"crypto/cipher"
"crypto/des"
"crypto/sha256"
"fmt"
"archgrid.xyz/ag/toolsbox/encryption"
)
@ -38,7 +37,7 @@ func XorKeyGenerator(key []byte) [3][8]byte {
func rawDesEncrypt(data []byte, key [8]byte) ([]byte, error) {
block, err := des.NewCipher(key[:])
if err != nil {
return nil, fmt.Errorf("创建加密单元失败,%w", err)
return nil, err
}
iv := key[:]
cipherText := make([]byte, len(data))
@ -51,7 +50,7 @@ func rawDesEncrypt(data []byte, key [8]byte) ([]byte, error) {
func rawDesDecrypt(data []byte, key [8]byte) ([]byte, error) {
block, err := des.NewCipher(key[:])
if err != nil {
return nil, fmt.Errorf("创建加密单元失败,%w", err)
return nil, err
}
iv := key[:]
plainText := make([]byte, len(data))
@ -68,17 +67,14 @@ func Encrypt(data []byte, key []byte, padding encryption.PaddingMode, keyGenerat
plainText := encryption.Padding(data, des.BlockSize, padding)
cipher1Text, err := rawDesEncrypt(plainText, desKeys[0])
if err != nil {
return nil, fmt.Errorf("第一阶段加密失败,%w", err)
return nil, err
}
plain2Text, err := rawDesDecrypt(cipher1Text, desKeys[1])
if err != nil {
return nil, fmt.Errorf("第二阶段解密失败,%w", err)
return nil, err
}
cipher3Text, err := rawDesEncrypt(plain2Text, desKeys[2])
if err != nil {
return nil, fmt.Errorf("第三阶段加密失败,%w", err)
}
return cipher3Text, nil
return cipher3Text, err
}
// 对给定的数据进行解密。
@ -88,15 +84,12 @@ func Decrypt(data []byte, key []byte, padding encryption.PaddingMode, keyGenerat
desKeys := append(keyGenerator, XorKeyGenerator)[0](key)
plainText, err := rawDesDecrypt(data, desKeys[2])
if err != nil {
return nil, fmt.Errorf("第三阶段解密失败,%w", err)
return nil, err
}
cipher2Text, err := rawDesEncrypt(plainText, desKeys[1])
if err != nil {
return nil, fmt.Errorf("第二阶段加密失败,%w", err)
return nil, err
}
plain3Text, err := rawDesDecrypt(cipher2Text, desKeys[0])
if err != nil {
return nil, fmt.Errorf("第一阶段解密失败,%w", err)
}
return encryption.Unpadding(plain3Text, des.BlockSize, padding), nil
return encryption.Unpadding(plain3Text, des.BlockSize, padding), err
}