1
0

aesecb.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package codec
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. "errors"
  8. "github.com/wuntsong-org/go-zero-plus/core/logx"
  9. )
  10. // ErrPaddingSize indicates bad padding size.
  11. var ErrPaddingSize = errors.New("padding size error")
  12. type ecb struct {
  13. b cipher.Block
  14. blockSize int
  15. }
  16. func newECB(b cipher.Block) *ecb {
  17. return &ecb{
  18. b: b,
  19. blockSize: b.BlockSize(),
  20. }
  21. }
  22. type ecbEncrypter ecb
  23. // NewECBEncrypter returns an ECB encrypter.
  24. func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
  25. return (*ecbEncrypter)(newECB(b))
  26. }
  27. // BlockSize returns the mode's block size.
  28. func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
  29. // CryptBlocks encrypts a number of blocks. The length of src must be a multiple of
  30. // the block size. Dst and src must overlap entirely or not at all.
  31. func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
  32. if len(src)%x.blockSize != 0 {
  33. logx.Error("crypto/cipher: input not full blocks")
  34. return
  35. }
  36. if len(dst) < len(src) {
  37. logx.Error("crypto/cipher: output smaller than input")
  38. return
  39. }
  40. for len(src) > 0 {
  41. x.b.Encrypt(dst, src[:x.blockSize])
  42. src = src[x.blockSize:]
  43. dst = dst[x.blockSize:]
  44. }
  45. }
  46. type ecbDecrypter ecb
  47. // NewECBDecrypter returns an ECB decrypter.
  48. func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
  49. return (*ecbDecrypter)(newECB(b))
  50. }
  51. // BlockSize returns the mode's block size.
  52. func (x *ecbDecrypter) BlockSize() int {
  53. return x.blockSize
  54. }
  55. // CryptBlocks decrypts a number of blocks. The length of src must be a multiple of
  56. // the block size. Dst and src must overlap entirely or not at all.
  57. func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
  58. if len(src)%x.blockSize != 0 {
  59. logx.Error("crypto/cipher: input not full blocks")
  60. return
  61. }
  62. if len(dst) < len(src) {
  63. logx.Error("crypto/cipher: output smaller than input")
  64. return
  65. }
  66. for len(src) > 0 {
  67. x.b.Decrypt(dst, src[:x.blockSize])
  68. src = src[x.blockSize:]
  69. dst = dst[x.blockSize:]
  70. }
  71. }
  72. // EcbDecrypt decrypts src with the given key.
  73. func EcbDecrypt(key, src []byte) ([]byte, error) {
  74. block, err := aes.NewCipher(key)
  75. if err != nil {
  76. logx.Errorf("Decrypt key error: % x", key)
  77. return nil, err
  78. }
  79. decrypter := NewECBDecrypter(block)
  80. decrypted := make([]byte, len(src))
  81. decrypter.CryptBlocks(decrypted, src)
  82. return pkcs5Unpadding(decrypted, decrypter.BlockSize())
  83. }
  84. // EcbDecryptBase64 decrypts base64 encoded src with the given base64 encoded key.
  85. // The returned string is also base64 encoded.
  86. func EcbDecryptBase64(key, src string) (string, error) {
  87. keyBytes, err := getKeyBytes(key)
  88. if err != nil {
  89. return "", err
  90. }
  91. encryptedBytes, err := base64.StdEncoding.DecodeString(src)
  92. if err != nil {
  93. return "", err
  94. }
  95. decryptedBytes, err := EcbDecrypt(keyBytes, encryptedBytes)
  96. if err != nil {
  97. return "", err
  98. }
  99. return base64.StdEncoding.EncodeToString(decryptedBytes), nil
  100. }
  101. // EcbEncrypt encrypts src with the given key.
  102. func EcbEncrypt(key, src []byte) ([]byte, error) {
  103. block, err := aes.NewCipher(key)
  104. if err != nil {
  105. logx.Errorf("Encrypt key error: % x", key)
  106. return nil, err
  107. }
  108. padded := pkcs5Padding(src, block.BlockSize())
  109. crypted := make([]byte, len(padded))
  110. encrypter := NewECBEncrypter(block)
  111. encrypter.CryptBlocks(crypted, padded)
  112. return crypted, nil
  113. }
  114. // EcbEncryptBase64 encrypts base64 encoded src with the given base64 encoded key.
  115. // The returned string is also base64 encoded.
  116. func EcbEncryptBase64(key, src string) (string, error) {
  117. keyBytes, err := getKeyBytes(key)
  118. if err != nil {
  119. return "", err
  120. }
  121. srcBytes, err := base64.StdEncoding.DecodeString(src)
  122. if err != nil {
  123. return "", err
  124. }
  125. encryptedBytes, err := EcbEncrypt(keyBytes, srcBytes)
  126. if err != nil {
  127. return "", err
  128. }
  129. return base64.StdEncoding.EncodeToString(encryptedBytes), nil
  130. }
  131. func getKeyBytes(key string) ([]byte, error) {
  132. if len(key) <= 32 {
  133. return []byte(key), nil
  134. }
  135. keyBytes, err := base64.StdEncoding.DecodeString(key)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return keyBytes, nil
  140. }
  141. func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
  142. padding := blockSize - len(ciphertext)%blockSize
  143. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  144. return append(ciphertext, padtext...)
  145. }
  146. func pkcs5Unpadding(src []byte, blockSize int) ([]byte, error) {
  147. length := len(src)
  148. unpadding := int(src[length-1])
  149. if unpadding >= length || unpadding > blockSize {
  150. return nil, ErrPaddingSize
  151. }
  152. return src[:length-unpadding], nil
  153. }