des.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package des
  2. import (
  3. "bytes"
  4. "crypto/cipher"
  5. "crypto/des"
  6. )
  7. const (
  8. Zero string = "Zero"
  9. PKCS5 string = "PKCS5"
  10. PKCS7 string = "PKCS7"
  11. )
  12. /*
  13. *plantText: plant text
  14. *key: size 8
  15. *padding: one of Zero,PKCS5,PKCS7,default PKCS5
  16. */
  17. func CBCEncrypt(plantText, key []byte, padding string) ([]byte, error) {
  18. block, err := des.NewCipher(key)
  19. if err != nil {
  20. return nil, err
  21. }
  22. switch padding {
  23. case Zero:
  24. plantText = ZeroPadding(plantText, block.BlockSize())
  25. case PKCS7:
  26. plantText = PKCS7Padding(plantText, block.BlockSize())
  27. default:
  28. plantText = PKCS5Padding(plantText, block.BlockSize())
  29. }
  30. blockModel := cipher.NewCBCEncrypter(block, key)
  31. ciphertext := make([]byte, len(plantText))
  32. blockModel.CryptBlocks(ciphertext, plantText)
  33. return ciphertext, nil
  34. }
  35. /*
  36. *cipherText: cipher text
  37. *key: size 8
  38. *unpadding: one of Zero,PKCS5,PKCS7,default PKCS5
  39. */
  40. func CBCDecrypt(ciphertext, key []byte, unpadding string) ([]byte, error) {
  41. keyBytes := []byte(key)
  42. block, err := des.NewCipher(keyBytes)
  43. if err != nil {
  44. return nil, err
  45. }
  46. blockModel := cipher.NewCBCDecrypter(block, keyBytes)
  47. plantText := make([]byte, len(ciphertext))
  48. blockModel.CryptBlocks(plantText, ciphertext)
  49. switch unpadding {
  50. case Zero:
  51. plantText = ZeroUnPadding(plantText)
  52. case PKCS7:
  53. plantText = PKCS7UnPadding(plantText)
  54. default:
  55. plantText = PKCS5UnPadding(plantText)
  56. }
  57. return plantText, nil
  58. }
  59. func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
  60. padding := blockSize - len(ciphertext)%blockSize
  61. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  62. return append(ciphertext, padtext...)
  63. }
  64. func PKCS7UnPadding(plantText []byte) []byte {
  65. length := len(plantText)
  66. unpadding := int(plantText[length-1])
  67. return plantText[:(length - unpadding)]
  68. }
  69. func ZeroPadding(ciphertext []byte, blockSize int) []byte {
  70. padding := blockSize - len(ciphertext)%blockSize
  71. padtext := bytes.Repeat([]byte{0}, padding)
  72. return append(ciphertext, padtext...)
  73. }
  74. func ZeroUnPadding(origData []byte) []byte {
  75. length := len(origData)
  76. unpadding := int(origData[length-1])
  77. return origData[:(length - unpadding)]
  78. }
  79. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  80. padding := blockSize - len(ciphertext)%blockSize
  81. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  82. return append(ciphertext, padtext...)
  83. }
  84. func PKCS5UnPadding(origData []byte) []byte {
  85. length := len(origData)
  86. unpadding := int(origData[length-1])
  87. return origData[:(length - unpadding)]
  88. }