Browse Source

[add] aes加密重构

bvbej 2 years ago
parent
commit
67f0d51ee5
1 changed files with 49 additions and 33 deletions
  1. 49 33
      pkg/aes/aes.go

+ 49 - 33
pkg/aes/aes.go

@@ -10,13 +10,12 @@ var _ Aes = (*aes)(nil)
 
 type Aes interface {
 	i()
-	// Encrypt 加密
-	Encrypt(encryptStr string) (string, error)
-	EncryptURLEncoding(encryptStr string) (string, error)
 
-	// Decrypt 解密
-	Decrypt(decryptStr string) (string, error)
-	DecryptURLEncoding(decryptStr string) (string, error)
+	EncryptCBC(encryptStr string, urlEncode bool) (string, error)
+	DecryptCBC(decryptStr string, urlEncode bool) (string, error)
+
+	EncryptCFB(plain string, urlEncode bool) (string, error)
+	DecryptCFB(encrypted string, urlEncode bool) (string, error)
 }
 
 type aes struct {
@@ -33,23 +32,12 @@ func New(key, iv string) Aes {
 
 func (a *aes) i() {}
 
-func (a *aes) Encrypt(encryptStr string) (string, error) {
-	encryptBytes := []byte(encryptStr)
-	block, err := cryptoAes.NewCipher([]byte(a.key))
-	if err != nil {
-		return "", err
+func (a *aes) EncryptCBC(encryptStr string, urlEncode bool) (string, error) {
+	encoder := base64.StdEncoding
+	if urlEncode {
+		encoder = base64.URLEncoding
 	}
 
-	blockSize := block.BlockSize()
-	encryptBytes = pkcsPadding(encryptBytes, blockSize)
-
-	blockMode := cipher.NewCBCEncrypter(block, []byte(a.iv))
-	encrypted := make([]byte, len(encryptBytes))
-	blockMode.CryptBlocks(encrypted, encryptBytes)
-	return base64.StdEncoding.EncodeToString(encrypted), nil
-}
-
-func (a *aes) EncryptURLEncoding(encryptStr string) (string, error) {
 	encryptBytes := []byte(encryptStr)
 	block, err := cryptoAes.NewCipher([]byte(a.key))
 	if err != nil {
@@ -58,15 +46,20 @@ func (a *aes) EncryptURLEncoding(encryptStr string) (string, error) {
 
 	blockSize := block.BlockSize()
 	encryptBytes = pkcsPadding(encryptBytes, blockSize)
-
 	blockMode := cipher.NewCBCEncrypter(block, []byte(a.iv))
 	encrypted := make([]byte, len(encryptBytes))
 	blockMode.CryptBlocks(encrypted, encryptBytes)
-	return base64.URLEncoding.EncodeToString(encrypted), nil
+
+	return encoder.EncodeToString(encrypted), nil
 }
 
-func (a *aes) Decrypt(decryptStr string) (string, error) {
-	decryptBytes, err := base64.StdEncoding.DecodeString(decryptStr)
+func (a *aes) DecryptCBC(decryptStr string, urlEncode bool) (string, error) {
+	encoder := base64.StdEncoding
+	if urlEncode {
+		encoder = base64.URLEncoding
+	}
+
+	decryptBytes, err := encoder.DecodeString(decryptStr)
 	if err != nil {
 		return "", err
 	}
@@ -78,14 +71,38 @@ func (a *aes) Decrypt(decryptStr string) (string, error) {
 
 	blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv))
 	decrypted := make([]byte, len(decryptBytes))
-
 	blockMode.CryptBlocks(decrypted, decryptBytes)
 	decrypted = pkcsUnPadding(decrypted)
+
 	return string(decrypted), nil
 }
 
-func (a *aes) DecryptURLEncoding(decryptStr string) (string, error) {
-	decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr)
+func (a *aes) EncryptCFB(plain string, urlEncode bool) (string, error) {
+	encoder := base64.StdEncoding
+	if urlEncode {
+		encoder = base64.URLEncoding
+	}
+
+	block, err := cryptoAes.NewCipher([]byte(a.key))
+	if err != nil {
+		return "", err
+	}
+
+	encrypted := make([]byte, len(plain))
+	stream := cipher.NewCFBEncrypter(block, []byte(a.iv))
+	stream.XORKeyStream(encrypted, []byte(plain))
+
+	return encoder.EncodeToString(encrypted), nil
+
+}
+
+func (a *aes) DecryptCFB(encrypted string, urlEncode bool) (string, error) {
+	encoder := base64.StdEncoding
+	if urlEncode {
+		encoder = base64.URLEncoding
+	}
+
+	decryptBytes, err := encoder.DecodeString(encrypted)
 	if err != nil {
 		return "", err
 	}
@@ -95,10 +112,9 @@ func (a *aes) DecryptURLEncoding(decryptStr string) (string, error) {
 		return "", err
 	}
 
-	blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv))
-	decrypted := make([]byte, len(decryptBytes))
+	plain := make([]byte, len(decryptBytes))
+	stream := cipher.NewCFBDecrypter(block, []byte(a.iv))
+	stream.XORKeyStream(plain, decryptBytes)
 
-	blockMode.CryptBlocks(decrypted, decryptBytes)
-	decrypted = pkcsUnPadding(decrypted)
-	return string(decrypted), nil
+	return string(plain), nil
 }