-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
miaoyin
committed
Nov 9, 2023
1 parent
dfc858c
commit bd06bd3
Showing
8 changed files
with
120 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,33 @@ | ||
package cfg | ||
|
||
import ( | ||
"crypto/rand" | ||
) | ||
|
||
func Encrypt(str, key string) (string, error) { | ||
data, cipher, err := Parse(str) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return EncryptByCipher(data, key, cipher), nil | ||
return cipher.Stringify(cipher.EncryptBytes(data, key)), nil | ||
} | ||
|
||
// EncryptByCipher 加密. | ||
func EncryptByCipher(str []byte, key string, cipher Cipher) string { | ||
var ( | ||
ret []byte | ||
block = cipher.Block(key) | ||
srcBytes = Padding(str, block.BlockSize()) | ||
tmp = make([]byte, block.BlockSize()) | ||
) | ||
|
||
for index := 0; index < len(srcBytes); index += block.BlockSize() { | ||
block.Encrypt(tmp, srcBytes[index:index+block.BlockSize()]) | ||
ret = append(ret, tmp...) | ||
} | ||
|
||
return cipher.Stringify(ret) | ||
func EncryptByCipher(src []byte, key string, cipher Cipher) string { | ||
return cipher.Stringify(cipher.EncryptBytes(src, key)) | ||
} | ||
|
||
// Decrypt 解密. | ||
func Decrypt(src, key string) (string, error) { | ||
var ret []byte | ||
|
||
srcBytes, cipher, err := Parse(src) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var ( | ||
block = cipher.Block(key) | ||
tmp = make([]byte, block.BlockSize()) | ||
) | ||
|
||
for index := 0; index < len(srcBytes); index += block.BlockSize() { | ||
block.Decrypt(tmp, srcBytes[index:index+block.BlockSize()]) | ||
ret = append(ret, tmp...) | ||
} | ||
|
||
text, err := UnPadding(ret) | ||
ret, err := cipher.DecryptBytes(srcBytes, key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if _checkRegex.MatchString(text) { | ||
return text, err | ||
if _checkRegex.Match(ret) { | ||
return string(ret), err | ||
} | ||
|
||
return "", ErrKey | ||
} | ||
|
||
func Padding(cipherText []byte, blockSize int) []byte { | ||
var ( | ||
padding = getPaddingSize(cipherText, blockSize) | ||
padData = make([]byte, padding-1) | ||
) | ||
|
||
_, _ = rand.Read(padData) | ||
// nolint | ||
padData = append(padData, byte(padding)) | ||
|
||
return append(cipherText, padData...) | ||
} | ||
|
||
func UnPadding(cipherText []byte) (string, error) { | ||
var ( | ||
length = len(cipherText) | ||
cipherLen = int(cipherText[length-1]) | ||
) | ||
|
||
if length < cipherLen { | ||
return "", ErrKey | ||
} | ||
|
||
return string(cipherText[:length-cipherLen]), nil | ||
} | ||
|
||
func getPaddingSize(cipherText []byte, blockSize int) int { | ||
remainder := len(cipherText) % blockSize | ||
if remainder == 0 { | ||
return blockSize | ||
} | ||
|
||
return blockSize - remainder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cfg | ||
|
||
import "bytes" | ||
|
||
func pkcs5Trimming(encrypt []byte) ([]byte, error) { | ||
var ( | ||
length = len(encrypt) | ||
cipherLen = int(encrypt[length-1]) | ||
) | ||
|
||
if length < cipherLen { | ||
return nil, ErrKey | ||
} | ||
|
||
return encrypt[:length-cipherLen], nil | ||
} | ||
|
||
func pkcs5Padding(cipherText []byte, blockSize int) []byte { | ||
padding := blockSize - len(cipherText)%blockSize | ||
padText := bytes.Repeat([]byte{byte(padding)}, padding) | ||
|
||
return append(cipherText, padText...) | ||
} |