| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 | 
							- package tools
 
- import (
 
- 	"bytes"
 
- 	"crypto/aes"
 
- 	"crypto/cipher"
 
- 	"encoding/base64"
 
- 	"errors"
 
- )
 
- // AES 加密
 
- func AesEncrypt(orig string, _Aeskey string) (string, error) {
 
- 	origData := []byte(orig)
 
- 	key := []byte(_Aeskey)
 
- 	block, err := aes.NewCipher(key)
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	blockSize := block.BlockSize()
 
- 	origData = func(ciphertext []byte, blockSize int) []byte {
 
- 		padding := blockSize - len(ciphertext)%blockSize
 
- 		padtext := bytes.Repeat([]byte{byte(padding)}, padding)
 
- 		return append(ciphertext, padtext...)
 
- 	}(origData, blockSize)
 
- 	blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
 
- 	crypted := make([]byte, len(origData))
 
- 	blockMode.CryptBlocks(crypted, origData)
 
- 	return base64.StdEncoding.EncodeToString(crypted), nil
 
- }
 
- // AES 解密
 
- func AesDecrypt(cryted string, _Aeskey string) (string, error) {
 
- 	if cryted == "" {
 
- 		return "", errors.New("错误的数据")
 
- 	}
 
- 	crypted, err := base64.StdEncoding.DecodeString(cryted)
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	key := []byte(_Aeskey)
 
- 	block, err := aes.NewCipher(key)
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	blockSize := block.BlockSize()
 
- 	blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
 
- 	origData := make([]byte, len(crypted))
 
- 	blockMode.CryptBlocks(origData, crypted)
 
- 	origData = func(origData []byte) []byte {
 
- 		length := len(origData)
 
- 		unpadding := int(origData[length-1])
 
- 		return origData[:(length - unpadding)]
 
- 	}(origData)
 
- 	return string(origData[:]), nil
 
- }
 
 
  |