des3.go 4.6 KB

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