encryptarticle.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package encrypt
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "net/url"
  5. "strings"
  6. )
  7. // 正文
  8. var SE = &SimpleEncrypt{Key: "topnet2015topnet2015"}
  9. var SE2 = &SimpleEncrypt{Key: "2017jianyu"}
  10. var SE3 = &SimpleEncrypt{Key: "entservice"}
  11. // 百度
  12. var BSE = &SimpleEncrypt{Key: "HNtopnet2017jy"}
  13. var BSE2 = &SimpleEncrypt{Key: "TOP2017jianyu"}
  14. // 订阅推送邮件
  15. var ESE = &SimpleEncrypt{Key: "PEjy2018topnet"}
  16. var ESE2 = &SimpleEncrypt{Key: "TopnetJy2018Pe"}
  17. // 首页
  18. var ISE = &SimpleEncrypt{Key: "Indexjy2021topnet"}
  19. var ISE2 = &SimpleEncrypt{Key: "TopnetJy2021Pe"}
  20. // 通用加密
  21. func CommonEncodeArticle(stype string, keys ...string) (id string) {
  22. switch stype {
  23. case "content", "yyszb":
  24. id = BEncodeArticleId2ByCheck("A", SE, SE2, keys...)
  25. case "bdprivate":
  26. id = BEncodeArticleId2ByCheck("B", BSE, BSE2, keys...)
  27. case "mailprivate":
  28. id = BEncodeArticleId2ByCheck("C", ESE, ESE2, keys...)
  29. case "indexcontent":
  30. id = BEncodeArticleId2ByCheck("D", ISE, ISE2, keys...)
  31. }
  32. return
  33. }
  34. // 通用解密
  35. func CommonDecodeArticle(stype string, id string) (res []string) {
  36. switch stype {
  37. case "content", "bdcontent", "yyszb":
  38. res = BDecodeArticleId2ByCheck(id, SE, SE2)
  39. case "bdprivate":
  40. res = BDecodeArticleId2ByCheck(id, BSE, BSE2)
  41. case "mailprivate":
  42. res = BDecodeArticleId2ByCheck(id, ESE, ESE2)
  43. case "indexcontent":
  44. res = BDecodeArticleId2ByCheck(id, ISE, ISE2)
  45. case "advancedProject":
  46. res = BDecodeArticleId2ByCheck(id, SE, SE2)
  47. case "entservice":
  48. res = []string{SE3.DecodeString(id)}
  49. }
  50. return
  51. }
  52. // 短地址加密,二次加密带校验和
  53. func BEncodeArticleId2ByCheck(h string, s1, s2 *SimpleEncrypt, keys ...string) string {
  54. kstr := strings.Join(keys, ",")
  55. kstr = s1.EncodeStringByCheck(kstr)
  56. return url.QueryEscape(h + common.GetLetterRandom(2) + s2.EncodeStringByCheck(kstr))
  57. }
  58. // 短地址解密,二次解密带校验和
  59. func BDecodeArticleId2ByCheck(id string, s1, s2 *SimpleEncrypt) []string {
  60. if !strings.Contains(id, "+") { //新加密算法解密
  61. id, _ = url.QueryUnescape(id)
  62. }
  63. kstr := s2.DecodeStringByCheck(id[3:])
  64. return strings.Split(s1.DecodeStringByCheck(kstr), ",")
  65. }