des3.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/cipher"
  5. "crypto/des"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "errors"
  9. "strings"
  10. )
  11. func DesBase64Encrypt(origData []byte, desKey string) []byte {
  12. result, err := TripleDesEncrypt(origData, []byte(desKey))
  13. if err != nil {
  14. panic(err)
  15. }
  16. return []byte(base64.StdEncoding.EncodeToString(result))
  17. }
  18. func DesBase64Decrypt(crypted []byte, desKey string) []byte {
  19. result, _ := base64.StdEncoding.DecodeString(string(crypted))
  20. remain := len(result) % 8
  21. if remain > 0 {
  22. mod := 8 - remain
  23. for i := 0; i < mod; i++ {
  24. result = append(result, 0)
  25. }
  26. }
  27. origData, err := TripleDesDecrypt(result, []byte(desKey))
  28. if err != nil {
  29. panic(err)
  30. }
  31. return origData
  32. }
  33. func TripleDesEncrypt(origData, key []byte) ([]byte, error) {
  34. block, err := des.NewTripleDESCipher(key)
  35. if err != nil {
  36. return nil, err
  37. }
  38. origData = PKCS5Padding(origData, block.BlockSize())
  39. blockMode := cipher.NewCBCEncrypter(block, key[:8])
  40. crypted := make([]byte, len(origData))
  41. blockMode.CryptBlocks(crypted, origData)
  42. return crypted, nil
  43. }
  44. func TripleDesDecrypt(crypted, key []byte) ([]byte, error) {
  45. block, err := des.NewTripleDESCipher(key)
  46. if err != nil {
  47. return nil, err
  48. }
  49. blockMode := cipher.NewCBCDecrypter(block, key[:8])
  50. origData := make([]byte, len(crypted))
  51. blockMode.CryptBlocks(origData, crypted)
  52. origData = PKCS5UnPadding(origData)
  53. return origData, nil
  54. }
  55. func ZeroPadding(ciphertext []byte, blockSize int) []byte {
  56. padding := blockSize - len(ciphertext)%blockSize
  57. padtext := bytes.Repeat([]byte{0}, padding)
  58. return append(ciphertext, padtext...)
  59. }
  60. func ZeroUnPadding(origData []byte) []byte {
  61. length := len(origData)
  62. unpadding := int(origData[length-1])
  63. return origData[:(length - unpadding)]
  64. }
  65. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  66. padding := blockSize - len(ciphertext)%blockSize
  67. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  68. return append(ciphertext, padtext...)
  69. }
  70. func PKCS5UnPadding(origData []byte) []byte {
  71. length := len(origData)
  72. unpadding := int(origData[length-1])
  73. return origData[:(length - unpadding)]
  74. }
  75. func DesEncrypt(content string, key string) string {
  76. contents := []byte(content)
  77. keys := []byte(key)
  78. block, err := des.NewCipher(keys)
  79. if err != nil {
  80. return ""
  81. }
  82. contents = PKCS5Padding(contents, block.BlockSize())
  83. blockMode := cipher.NewCBCEncrypter(block, keys)
  84. crypted := make([]byte, len(contents))
  85. blockMode.CryptBlocks(crypted, contents)
  86. return byteToHexString(crypted)
  87. }
  88. func byteToHexString(bytes []byte) string {
  89. str := ""
  90. for i := 0; i < len(bytes); i++ {
  91. sTemp := hex.EncodeToString([]byte{bytes[i]})
  92. if len(sTemp) < 2 {
  93. str += string(0)
  94. }
  95. str += strings.ToUpper(sTemp)
  96. }
  97. return str
  98. }
  99. func DesDecrypt(content string, key string) string {
  100. contentBytes, err := hex.DecodeString(content)
  101. if err != nil {
  102. return "字符串转换16进制数组失败" + err.Error()
  103. }
  104. keys := []byte(key)
  105. block, err := des.NewCipher(keys)
  106. if err != nil {
  107. return "解密失败" + err.Error()
  108. }
  109. blockMode := cipher.NewCBCDecrypter(block, keys)
  110. origData := contentBytes
  111. blockMode.CryptBlocks(origData, contentBytes)
  112. origData = ZeroUnPadding(origData)
  113. return string(origData)
  114. }
  115. func EntryptDesECB(data, key []byte) (string, error) {
  116. if len(key) > 8 {
  117. key = key[:8]
  118. }
  119. block, err := des.NewCipher(key)
  120. if err != nil {
  121. return "", errors.New("des.NewCipher " + err.Error())
  122. }
  123. bs := block.BlockSize()
  124. data = PKCS5Padding(data, bs)
  125. if len(data)%bs != 0 {
  126. return "", errors.New("EntryptDesECB Need a multiple of the blocksize")
  127. }
  128. out := make([]byte, len(data))
  129. dst := out
  130. for len(data) > 0 {
  131. block.Encrypt(dst, data[:bs])
  132. data = data[bs:]
  133. dst = dst[bs:]
  134. }
  135. return base64.StdEncoding.EncodeToString(out), nil
  136. }
  137. func DecryptDESECB(d string, key []byte) ([]byte, error) {
  138. data, err := base64.StdEncoding.DecodeString(d)
  139. if err != nil {
  140. return nil, errors.New("decodebase64 " + err.Error())
  141. }
  142. if len(key) > 8 {
  143. key = key[:8]
  144. }
  145. block, err := des.NewCipher(key)
  146. if err != nil {
  147. return nil, errors.New("des.NewCipher " + err.Error())
  148. }
  149. bs := block.BlockSize()
  150. if len(data)%bs != 0 {
  151. return nil, errors.New("DecryptDES crypto/cipher: input not full blocks")
  152. }
  153. out := make([]byte, len(data))
  154. dst := out
  155. for len(data) > 0 {
  156. block.Decrypt(dst, data[:bs])
  157. data = data[bs:]
  158. dst = dst[bs:]
  159. }
  160. out = PKCS5UnPadding(out)
  161. return out, nil
  162. }