aes.go 2.8 KB

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