aes.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package aes
  2. import (
  3. cryptoAes "crypto/aes"
  4. "crypto/cipher"
  5. "encoding/base64"
  6. )
  7. var _ Aes = (*aes)(nil)
  8. type Aes interface {
  9. i()
  10. // Encrypt 加密
  11. Encrypt(encryptStr string) (string, error)
  12. EncryptURLEncoding(encryptStr string) (string, error)
  13. // Decrypt 解密
  14. Decrypt(decryptStr string) (string, error)
  15. DecryptURLEncoding(decryptStr string) (string, error)
  16. }
  17. type aes struct {
  18. key string
  19. iv string
  20. }
  21. func New(key, iv string) Aes {
  22. return &aes{
  23. key: key,
  24. iv: iv,
  25. }
  26. }
  27. func (a *aes) i() {}
  28. func (a *aes) Encrypt(encryptStr string) (string, error) {
  29. encryptBytes := []byte(encryptStr)
  30. block, err := cryptoAes.NewCipher([]byte(a.key))
  31. if err != nil {
  32. return "", err
  33. }
  34. blockSize := block.BlockSize()
  35. encryptBytes = pkcs5Padding(encryptBytes, blockSize)
  36. blockMode := cipher.NewCBCEncrypter(block, []byte(a.iv))
  37. encrypted := make([]byte, len(encryptBytes))
  38. blockMode.CryptBlocks(encrypted, encryptBytes)
  39. return base64.StdEncoding.EncodeToString(encrypted), nil
  40. }
  41. func (a *aes) EncryptURLEncoding(encryptStr string) (string, error) {
  42. encryptBytes := []byte(encryptStr)
  43. block, err := cryptoAes.NewCipher([]byte(a.key))
  44. if err != nil {
  45. return "", err
  46. }
  47. blockSize := block.BlockSize()
  48. encryptBytes = pkcs5Padding(encryptBytes, blockSize)
  49. blockMode := cipher.NewCBCEncrypter(block, []byte(a.iv))
  50. encrypted := make([]byte, len(encryptBytes))
  51. blockMode.CryptBlocks(encrypted, encryptBytes)
  52. return base64.URLEncoding.EncodeToString(encrypted), nil
  53. }
  54. func (a *aes) Decrypt(decryptStr string) (string, error) {
  55. decryptBytes, err := base64.StdEncoding.DecodeString(decryptStr)
  56. if err != nil {
  57. return "", err
  58. }
  59. block, err := cryptoAes.NewCipher([]byte(a.key))
  60. if err != nil {
  61. return "", err
  62. }
  63. blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv))
  64. decrypted := make([]byte, len(decryptBytes))
  65. blockMode.CryptBlocks(decrypted, decryptBytes)
  66. decrypted = pkcs5UnPadding(decrypted)
  67. return string(decrypted), nil
  68. }
  69. func (a *aes) DecryptURLEncoding(decryptStr string) (string, error) {
  70. decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr)
  71. if err != nil {
  72. return "", err
  73. }
  74. block, err := cryptoAes.NewCipher([]byte(a.key))
  75. if err != nil {
  76. return "", err
  77. }
  78. blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv))
  79. decrypted := make([]byte, len(decryptBytes))
  80. blockMode.CryptBlocks(decrypted, decryptBytes)
  81. decrypted = pkcs5UnPadding(decrypted)
  82. return string(decrypted), nil
  83. }