You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
package crypto
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"encoding/base64"
|
|
"errors"
|
|
)
|
|
|
|
// AesEncrypt function
|
|
// original The original password, salt is an optional variable.
|
|
// If the salt exists, the passed variable is used.
|
|
// If it does not exist, the system default salt value is used.
|
|
func (e *Crypto) AesEncrypt(original string, key ...string) (string, error) {
|
|
var saltValue = defaultAesKey
|
|
if len(key) > 0 {
|
|
saltValue = key[0]
|
|
}
|
|
|
|
// Convert to byte array
|
|
origData := []byte(original)
|
|
k := []byte(saltValue)
|
|
if len(k) != 16 && len(k) != 24 && len(k) != 32 {
|
|
return "", errors.New("The length of the key should be 16 or 24 or 32")
|
|
}
|
|
// group key
|
|
block, _ := aes.NewCipher(k)
|
|
// Get the length of the key block
|
|
blockSize := block.BlockSize()
|
|
// Completion code
|
|
origData = pKCS7Padding(origData, blockSize)
|
|
// encryption mode
|
|
blockMode := cipher.NewCBCEncrypter(block, k[:blockSize])
|
|
// create array
|
|
crated := make([]byte, len(origData))
|
|
// encryption
|
|
blockMode.CryptBlocks(crated, origData)
|
|
return base64.StdEncoding.EncodeToString(crated), nil
|
|
|
|
}
|
|
|
|
// AesDecrypt function
|
|
// original The original password, salt is an optional variable.
|
|
// If the salt exists, the passed variable is used.
|
|
// If it does not exist, the system default salt value is used.
|
|
func (e *Crypto) AesDecrypt(crated string, salt ...string) (string, error) {
|
|
var saltValue = defaultAesKey
|
|
if len(salt) > 0 {
|
|
saltValue = salt[0]
|
|
}
|
|
// Convert to byte array
|
|
cratedByte, _ := base64.StdEncoding.DecodeString(crated)
|
|
k := []byte(saltValue)
|
|
if len(k) != 16 && len(k) != 24 && len(k) != 32 {
|
|
return "", errors.New("The length of the key should be 16 or 24 or 32")
|
|
}
|
|
// group key
|
|
block, _ := aes.NewCipher(k)
|
|
// Get the length of the key block
|
|
blockSize := block.BlockSize()
|
|
// encryption mode
|
|
blockMode := cipher.NewCBCDecrypter(block, k[:blockSize])
|
|
// create array
|
|
orig := make([]byte, len(cratedByte))
|
|
// decrypt
|
|
blockMode.CryptBlocks(orig, cratedByte)
|
|
// to complete the code
|
|
orig = pKCS7UnPadding(orig)
|
|
return string(orig), nil
|
|
}
|