123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package aes
- import (
- cryptoAes "crypto/aes"
- "crypto/cipher"
- "encoding/base64"
- )
- 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)
- }
- type aes struct {
- key string
- iv string
- }
- func New(key, iv string) Aes {
- return &aes{
- key: key,
- iv: iv,
- }
- }
- 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
- }
- blockSize := block.BlockSize()
- encryptBytes = pkcs5Padding(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 {
- return "", err
- }
- blockSize := block.BlockSize()
- encryptBytes = pkcs5Padding(encryptBytes, blockSize)
- blockMode := cipher.NewCBCEncrypter(block, []byte(a.iv))
- encrypted := make([]byte, len(encryptBytes))
- blockMode.CryptBlocks(encrypted, encryptBytes)
- return base64.URLEncoding.EncodeToString(encrypted), nil
- }
- func (a *aes) Decrypt(decryptStr string) (string, error) {
- decryptBytes, err := base64.StdEncoding.DecodeString(decryptStr)
- if err != nil {
- return "", err
- }
- block, err := cryptoAes.NewCipher([]byte(a.key))
- if err != nil {
- return "", err
- }
- blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv))
- decrypted := make([]byte, len(decryptBytes))
- blockMode.CryptBlocks(decrypted, decryptBytes)
- decrypted = pkcs5UnPadding(decrypted)
- return string(decrypted), nil
- }
- func (a *aes) DecryptURLEncoding(decryptStr string) (string, error) {
- decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr)
- if err != nil {
- return "", err
- }
- block, err := cryptoAes.NewCipher([]byte(a.key))
- if err != nil {
- return "", err
- }
- blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv))
- decrypted := make([]byte, len(decryptBytes))
- blockMode.CryptBlocks(decrypted, decryptBytes)
- decrypted = pkcs5UnPadding(decrypted)
- return string(decrypted), nil
- }
|