enhance(crypto):包装3DES算法中的错误提示。
This commit is contained in:
		@@ -5,6 +5,7 @@ import (
 | 
			
		||||
	"crypto/cipher"
 | 
			
		||||
	"crypto/des"
 | 
			
		||||
	"crypto/sha256"
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"archgrid.xyz/ag/toolsbox/encryption"
 | 
			
		||||
)
 | 
			
		||||
@@ -37,7 +38,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, err
 | 
			
		||||
		return nil, fmt.Errorf("创建加密单元失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	iv := key[:]
 | 
			
		||||
	cipherText := make([]byte, len(data))
 | 
			
		||||
@@ -50,7 +51,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, err
 | 
			
		||||
		return nil, fmt.Errorf("创建加密单元失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	iv := key[:]
 | 
			
		||||
	plainText := make([]byte, len(data))
 | 
			
		||||
@@ -67,14 +68,17 @@ 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, err
 | 
			
		||||
		return nil, fmt.Errorf("第一阶段加密失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	plain2Text, err := rawDesDecrypt(cipher1Text, desKeys[1])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
		return nil, fmt.Errorf("第二阶段解密失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	cipher3Text, err := rawDesEncrypt(plain2Text, desKeys[2])
 | 
			
		||||
	return cipher3Text, err
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("第三阶段加密失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return cipher3Text, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 对给定的数据进行解密。
 | 
			
		||||
@@ -84,12 +88,15 @@ 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, err
 | 
			
		||||
		return nil, fmt.Errorf("第三阶段解密失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	cipher2Text, err := rawDesEncrypt(plainText, desKeys[1])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
		return nil, fmt.Errorf("第二阶段加密失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	plain3Text, err := rawDesDecrypt(cipher2Text, desKeys[0])
 | 
			
		||||
	return encryption.Unpadding(plain3Text, des.BlockSize, padding), err
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("第一阶段解密失败,%w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return encryption.Unpadding(plain3Text, des.BlockSize, padding), nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user