encryptUtils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "errors"
  7. )
  8. // AESSecretKey AES密钥
  9. const AESSecretKey = "0d299ce2d4105282f7471074cb0f9f9d"
  10. // AESEncrypt AES加密
  11. func AESEncrypt(plaintext, key []byte) ([]byte, error) {
  12. block, err := aes.NewCipher(key)
  13. if err != nil {
  14. return nil, errors.New("错误的数据或密钥")
  15. }
  16. ecb := NewECBEncrypter(block)
  17. content := plaintext[:]
  18. content = PKCS5Padding(content, block.BlockSize())
  19. crypted := make([]byte, len(content))
  20. ecb.CryptBlocks(crypted, content)
  21. return crypted, nil
  22. }
  23. // AESDecrypt AES
  24. func AESDecrypt(crypted, key []byte) ([]byte, error) {
  25. block, err := aes.NewCipher(key)
  26. if err != nil {
  27. return nil, errors.New("错误的密钥")
  28. }
  29. blockMode := NewECBDecrypter(block)
  30. if len(crypted)%blockMode.BlockSize() != 0 {
  31. return nil, errors.New("错误的数据")
  32. }
  33. origData := make([]byte, len(crypted))
  34. blockMode.CryptBlocks(origData, crypted)
  35. origData = PKCS5UnPadding(origData)
  36. return origData, nil
  37. }
  38. // PKCS5Padding ECB PKCS5Padding
  39. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  40. padding := blockSize - len(ciphertext)%blockSize
  41. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  42. return append(ciphertext, padtext...)
  43. }
  44. // PKCS5UnPadding ECB PKCS5Unpadding
  45. func PKCS5UnPadding(origData []byte) []byte {
  46. length := len(origData)
  47. // 去掉最后一个字节 unpadding 次
  48. unpadding := int(origData[length-1])
  49. // 防止数据越界崩溃
  50. if length-unpadding >= len(origData) {
  51. return origData
  52. }
  53. return origData[:(length - unpadding)]
  54. }
  55. type ecb struct {
  56. b cipher.Block
  57. blockSize int
  58. }
  59. func newECB(b cipher.Block) *ecb {
  60. return &ecb{
  61. b: b,
  62. blockSize: b.BlockSize(),
  63. }
  64. }
  65. type ecbEncrypter ecb
  66. // NewECBEncrypter returns a BlockMode which encrypts in electronic code book
  67. // mode, using the given Block.
  68. func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
  69. return (*ecbEncrypter)(newECB(b))
  70. }
  71. func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
  72. func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
  73. if len(src)%x.blockSize != 0 {
  74. panic("crypto/cipher: input not full blocks")
  75. }
  76. if len(dst) < len(src) {
  77. panic("crypto/cipher: output smaller than input")
  78. }
  79. for len(src) > 0 {
  80. x.b.Encrypt(dst, src[:x.blockSize])
  81. src = src[x.blockSize:]
  82. dst = dst[x.blockSize:]
  83. }
  84. }
  85. type ecbDecrypter ecb
  86. // NewECBDecrypter returns a BlockMode which decrypts in electronic code book
  87. // mode, using the given Block.
  88. func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
  89. return (*ecbDecrypter)(newECB(b))
  90. }
  91. func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
  92. func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
  93. if len(src)%x.blockSize != 0 {
  94. panic("crypto/cipher: input not full blocks")
  95. }
  96. if len(dst) < len(src) {
  97. panic("crypto/cipher: output smaller than input")
  98. }
  99. for len(src) > 0 {
  100. x.b.Decrypt(dst, src[:x.blockSize])
  101. src = src[x.blockSize:]
  102. dst = dst[x.blockSize:]
  103. }
  104. }