cbc.go 785 B

1234567891011121314151617181920212223242526272829303132333435
  1. package utils
  2. import (
  3. "crypto/cipher"
  4. "crypto/des"
  5. )
  6. //des加密
  7. func CBCDesEncrypt(key, iv, plainText []byte) ([]byte, error) {
  8. block, err := des.NewCipher(key)
  9. if err != nil {
  10. return nil, err
  11. }
  12. blockSize := block.BlockSize()
  13. origData := pkcs5Padding(plainText, blockSize)
  14. blockMode := cipher.NewCBCEncrypter(block, iv)
  15. cryted := make([]byte, len(origData))
  16. blockMode.CryptBlocks(cryted, origData)
  17. return cryted, nil
  18. }
  19. //des解密
  20. func CBCDesDecrypt(key, iv, cipherText []byte) ([]byte, error) {
  21. block, err := des.NewCipher(key)
  22. if err != nil {
  23. return nil, err
  24. }
  25. blockMode := cipher.NewCBCDecrypter(block, iv)
  26. origData := make([]byte, len(cipherText))
  27. blockMode.CryptBlocks(origData, cipherText)
  28. origData = pkcs5UnPadding(origData)
  29. return origData, nil
  30. }