智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 line
3.2 KiB

  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "fmt"
  7. )
  8. func AesEncrypt(rawData, key []byte) ([]byte, error) {
  9. block, err := aes.NewCipher(key)
  10. if err != nil {
  11. return nil, err
  12. }
  13. blockSize := block.BlockSize()
  14. rawData = PKCS5Padding(rawData, blockSize)
  15. // rawData = ZeroPadding(rawData, block.BlockSize())
  16. blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
  17. encrypted := make([]byte, len(rawData))
  18. // 根据CryptBlocks方法的说明,如下方式初始化encrypted也可以
  19. // encrypted := rawData
  20. blockMode.CryptBlocks(encrypted, rawData)
  21. return encrypted, nil
  22. }
  23. func AesDecrypt(encrypted, key []byte) ([]byte, error) {
  24. block, err := aes.NewCipher(key)
  25. if err != nil {
  26. return nil, err
  27. }
  28. blockSize := block.BlockSize()
  29. blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
  30. rawData := make([]byte, len(encrypted))
  31. // rawData := encrypted
  32. blockMode.CryptBlocks(rawData, encrypted)
  33. rawData = PKCS5UnPadding(rawData)
  34. // rawData = ZeroUnPadding(rawData)
  35. return rawData, nil
  36. }
  37. func ZeroPadding(cipherText []byte, blockSize int) []byte {
  38. padding := blockSize - len(cipherText)%blockSize
  39. padText := bytes.Repeat([]byte{0}, padding)
  40. return append(cipherText, padText...)
  41. }
  42. func ZeroUnPadding(rawData []byte) []byte {
  43. length := len(rawData)
  44. unPadding := int(rawData[length-1])
  45. return rawData[:(length - unPadding)]
  46. }
  47. func PKCS5Padding(cipherText []byte, blockSize int) []byte {
  48. padding := blockSize - len(cipherText)%blockSize
  49. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  50. return append(cipherText, padText...)
  51. }
  52. func PKCS5UnPadding(rawData []byte) []byte {
  53. length := len(rawData)
  54. // 去掉最后一个字节 unPadding 次
  55. unPadding := int(rawData[length-1])
  56. return rawData[:(length - unPadding)]
  57. }
  58. // 填充0
  59. func zeroFill(key *string) {
  60. l := len(*key)
  61. if l != 16 && l != 24 && l != 32 {
  62. if l < 16 {
  63. *key = *key + fmt.Sprintf("%0*d", 16-l, 0)
  64. } else if l < 24 {
  65. *key = *key + fmt.Sprintf("%0*d", 24-l, 0)
  66. } else if l < 32 {
  67. *key = *key + fmt.Sprintf("%0*d", 32-l, 0)
  68. } else {
  69. *key = string([]byte(*key)[:32])
  70. }
  71. }
  72. }
  73. type AesCrypt struct {
  74. Key []byte
  75. Iv []byte
  76. }
  77. func (a *AesCrypt) Encrypt(data []byte) ([]byte, error) {
  78. aesBlockEncrypt, err := aes.NewCipher(a.Key)
  79. if err != nil {
  80. println(err.Error())
  81. return nil, err
  82. }
  83. content := pKCS5Padding(data, aesBlockEncrypt.BlockSize())
  84. cipherBytes := make([]byte, len(content))
  85. aesEncrypt := cipher.NewCBCEncrypter(aesBlockEncrypt, a.Iv)
  86. aesEncrypt.CryptBlocks(cipherBytes, content)
  87. return cipherBytes, nil
  88. }
  89. func (a *AesCrypt) Decrypt(src []byte) (data []byte, err error) {
  90. decrypted := make([]byte, len(src))
  91. var aesBlockDecrypt cipher.Block
  92. aesBlockDecrypt, err = aes.NewCipher(a.Key)
  93. if err != nil {
  94. println(err.Error())
  95. return nil, err
  96. }
  97. aesDecrypt := cipher.NewCBCDecrypter(aesBlockDecrypt, a.Iv)
  98. aesDecrypt.CryptBlocks(decrypted, src)
  99. return pKCS5Trimming(decrypted), nil
  100. }
  101. func pKCS5Padding(cipherText []byte, blockSize int) []byte {
  102. padding := blockSize - len(cipherText)%blockSize
  103. padText := bytes.Repeat([]byte{byte(padding)}, padding)
  104. return append(cipherText, padText...)
  105. }
  106. func pKCS5Trimming(encrypt []byte) []byte {
  107. padding := encrypt[len(encrypt)-1]
  108. return encrypt[:len(encrypt)-int(padding)]
  109. }