des3.go 4.6 KB

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