aes.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. EncryptCBC(encryptStr string, urlEncode bool) (string, error)
  11. DecryptCBC(decryptStr string, urlEncode bool) (string, error)
  12. EncryptCFB(plain string, urlEncode bool) (string, error)
  13. DecryptCFB(encrypted string, urlEncode bool) (string, error)
  14. }
  15. type aes struct {
  16. key string
  17. iv string
  18. }
  19. func New(key, iv string) Aes {
  20. return &aes{
  21. key: key,
  22. iv: iv,
  23. }
  24. }
  25. func (a *aes) i() {}
  26. func (a *aes) EncryptCBC(encryptStr string, urlEncode bool) (string, error) {
  27. encoder := base64.StdEncoding
  28. if urlEncode {
  29. encoder = base64.URLEncoding
  30. }
  31. encryptBytes := []byte(encryptStr)
  32. block, err := cryptoAes.NewCipher([]byte(a.key))
  33. if err != nil {
  34. return "", err
  35. }
  36. blockSize := block.BlockSize()
  37. encryptBytes = pkcsPadding(encryptBytes, blockSize)
  38. blockMode := cipher.NewCBCEncrypter(block, []byte(a.iv))
  39. encrypted := make([]byte, len(encryptBytes))
  40. blockMode.CryptBlocks(encrypted, encryptBytes)
  41. return encoder.EncodeToString(encrypted), nil
  42. }
  43. func (a *aes) DecryptCBC(decryptStr string, urlEncode bool) (string, error) {
  44. encoder := base64.StdEncoding
  45. if urlEncode {
  46. encoder = base64.URLEncoding
  47. }
  48. decryptBytes, err := encoder.DecodeString(decryptStr)
  49. if err != nil {
  50. return "", err
  51. }
  52. block, err := cryptoAes.NewCipher([]byte(a.key))
  53. if err != nil {
  54. return "", err
  55. }
  56. blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv))
  57. decrypted := make([]byte, len(decryptBytes))
  58. blockMode.CryptBlocks(decrypted, decryptBytes)
  59. decrypted = pkcsUnPadding(decrypted)
  60. return string(decrypted), nil
  61. }
  62. func (a *aes) EncryptCFB(plain string, urlEncode bool) (string, error) {
  63. encoder := base64.StdEncoding
  64. if urlEncode {
  65. encoder = base64.URLEncoding
  66. }
  67. block, err := cryptoAes.NewCipher([]byte(a.key))
  68. if err != nil {
  69. return "", err
  70. }
  71. encrypted := make([]byte, len(plain))
  72. stream := cipher.NewCFBEncrypter(block, []byte(a.iv))
  73. stream.XORKeyStream(encrypted, []byte(plain))
  74. return encoder.EncodeToString(encrypted), nil
  75. }
  76. func (a *aes) DecryptCFB(encrypted string, urlEncode bool) (string, error) {
  77. encoder := base64.StdEncoding
  78. if urlEncode {
  79. encoder = base64.URLEncoding
  80. }
  81. decryptBytes, err := encoder.DecodeString(encrypted)
  82. if err != nil {
  83. return "", err
  84. }
  85. block, err := cryptoAes.NewCipher([]byte(a.key))
  86. if err != nil {
  87. return "", err
  88. }
  89. plain := make([]byte, len(decryptBytes))
  90. stream := cipher.NewCFBDecrypter(block, []byte(a.iv))
  91. stream.XORKeyStream(plain, decryptBytes)
  92. return string(plain), nil
  93. }