ecb.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package utils
  2. import (
  3. "bytes"
  4. "errors"
  5. "crypto/aes"
  6. "crypto/cipher"
  7. "crypto/rand"
  8. "encoding/base64"
  9. "io"
  10. )
  11. // 签名字符串生成
  12. // CBC 模式
  13. // 加密
  14. //
  15. // 使用PKCS7进行填充,IOS也是7
  16. func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
  17. padding := blockSize - len(ciphertext)%blockSize // 需要填充的数目
  18. // 只要少于256就能放到一个byte中,默认的blockSize=16(即采用16*8=128, AES-128长的密钥)
  19. // 最少填充1个byte,如果原文刚好是blocksize的整数倍,则再填充一个blocksize
  20. padtext := bytes.Repeat([]byte{byte(padding)}, padding) // 生成填充的文本
  21. return append(ciphertext, padtext...)
  22. }
  23. func PKCS7UnPadding(origData []byte) []byte {
  24. length := len(origData)
  25. unpadding := int(origData[length-1])
  26. return origData[:(length - unpadding)]
  27. }
  28. // aes加密,填充秘钥key的16位,24,32分别对应AES-128, AES-192, or AES-256.
  29. func AesCBCEncrypt(rawData, key []byte) ([]byte, error) {
  30. block, err := aes.NewCipher(key)
  31. if err != nil {
  32. return nil, err
  33. }
  34. // 填充原文
  35. blockSize := block.BlockSize()
  36. rawData = PKCS7Padding(rawData, blockSize)
  37. // 初始向量IV必须是唯一,但不需要保密
  38. cipherText := make([]byte, blockSize+len(rawData))
  39. // block大小 16
  40. //iv := cipherText[:blockSize]
  41. iv := key[:16]
  42. if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  43. return nil, err
  44. }
  45. // block大小和初始向量大小一定要一致
  46. mode := cipher.NewCBCEncrypter(block, iv)
  47. mode.CryptBlocks(cipherText[blockSize:], rawData)
  48. return cipherText, nil
  49. }
  50. // 解密
  51. func AesCBCDncrypt(encryptData, key []byte) ([]byte, error) {
  52. block, err := aes.NewCipher(key)
  53. if err != nil {
  54. return nil, err
  55. }
  56. blockSize := block.BlockSize()
  57. if len(encryptData) < blockSize {
  58. return nil, errors.New("ciphertext too short")
  59. }
  60. //iv := encryptData[:blockSize]
  61. iv := key[:16]
  62. encryptData = encryptData[blockSize:]
  63. if len(encryptData)%blockSize != 0 {
  64. return nil, errors.New("ciphertext is not a multiple of the block size")
  65. }
  66. mode := cipher.NewCBCDecrypter(block, iv)
  67. mode.CryptBlocks(encryptData, encryptData)
  68. // 解填充
  69. encryptData = PKCS7UnPadding(encryptData)
  70. return encryptData, nil
  71. }
  72. func ProvincialEncrypt(rawData, key []byte) (string, error) {
  73. data, err := AesCBCEncrypt(rawData, key)
  74. if err != nil {
  75. return "", err
  76. }
  77. return base64.StdEncoding.EncodeToString(data), nil
  78. }
  79. func ProvincialDecrypt(rawData string, key []byte) (string, error) {
  80. data, err := base64.StdEncoding.DecodeString(rawData)
  81. if err != nil {
  82. return "", err
  83. }
  84. dnData, err := AesCBCDncrypt(data, key)
  85. if err != nil {
  86. return "", err
  87. }
  88. return string(dnData), nil
  89. }