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.

40 lines
824 B

  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/cipher"
  5. "crypto/des"
  6. )
  7. func padding(src []byte, blocksize int) []byte {
  8. n := len(src)
  9. padnum := blocksize - n%blocksize
  10. pad := bytes.Repeat([]byte{byte(padnum)}, padnum)
  11. dst := append(src, pad...)
  12. return dst
  13. }
  14. func unpadding(src []byte) []byte {
  15. n := len(src)
  16. unpadnum := int(src[n-1])
  17. dst := src[:n-unpadnum]
  18. return dst
  19. }
  20. func EncryptDES(src []byte) []byte {
  21. key := []byte("qimiao66")
  22. block, _ := des.NewCipher(key)
  23. src = padding(src, block.BlockSize())
  24. blockmode := cipher.NewCBCEncrypter(block, key)
  25. blockmode.CryptBlocks(src, src)
  26. return src
  27. }
  28. func DecryptDES(src []byte) []byte {
  29. key := []byte("qimiao66")
  30. block, _ := des.NewCipher(key)
  31. blockmode := cipher.NewCBCDecrypter(block, key)
  32. blockmode.CryptBlocks(src, src)
  33. src = unpadding(src)
  34. return src
  35. }