rsaEncDec.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package util
  2. import (
  3. log "app.yhyue.com/moapp/jylog"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/x509"
  7. "encoding/base64"
  8. "encoding/pem"
  9. "errors"
  10. "fmt"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/os/gctx"
  13. "io/ioutil"
  14. "math/big"
  15. )
  16. var (
  17. PublicKey *rsa.PublicKey
  18. PrivateKey *rsa.PrivateKey
  19. Displacement int64
  20. )
  21. func init() {
  22. initPrivatePublicKey()
  23. }
  24. func initPrivatePublicKey() {
  25. // 读取私钥文件
  26. privateKeyBytes, err := ioutil.ReadFile("./etc/rsa_private_key.pem")
  27. if err != nil {
  28. log.Println("无法读取私钥文件:", err)
  29. return
  30. }
  31. // 解码私钥
  32. privateKeyBlock, _ := pem.Decode(privateKeyBytes)
  33. if privateKeyBlock == nil || privateKeyBlock.Type != "PRIVATE KEY" {
  34. log.Println("私钥文件格式错误")
  35. return
  36. }
  37. // 解析私钥
  38. privateKeys, err := x509.ParsePKCS8PrivateKey(privateKeyBlock.Bytes)
  39. if err != nil {
  40. log.Println("私钥解析失败:", err)
  41. return
  42. }
  43. // 转换为RSA类型的私钥
  44. rsaPrivateKey, ok := privateKeys.(*rsa.PrivateKey)
  45. if !ok {
  46. fmt.Println("无法转换为RSA类型的私钥")
  47. return
  48. }
  49. PrivateKey = rsaPrivateKey
  50. // 读取公钥文件
  51. publicKeyBytes, err := ioutil.ReadFile("./etc/rsa_public_key.pem")
  52. if err != nil {
  53. log.Println("无法读取公钥文件:", err)
  54. return
  55. }
  56. // 解码公钥
  57. publicKeyBlock, _ := pem.Decode(publicKeyBytes)
  58. if publicKeyBlock == nil || publicKeyBlock.Type != "PUBLIC KEY" {
  59. log.Println("公钥文件格式错误", publicKeyBlock.Type)
  60. return
  61. }
  62. // 解析公钥
  63. publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
  64. if err != nil {
  65. log.Println("公钥解析失败:", err.Error())
  66. return
  67. }
  68. // 转换为公钥类型
  69. rsaPublicKey, ok := publicKeyInterface.(*rsa.PublicKey)
  70. if !ok {
  71. log.Println("无法转换为公钥类型")
  72. return
  73. }
  74. PublicKey = rsaPublicKey
  75. Displacement = g.Cfg().MustGet(gctx.New(), "displacement").Int64()
  76. }
  77. // Encryption wei加密
  78. func DisplacementEncryption(content string) (string, error) {
  79. if content == "" {
  80. return "", errors.New("加密内容为空")
  81. }
  82. plaintext := []byte(content)
  83. if Displacement != 0 { // 位移处理的值
  84. shiftValue := big.NewInt(Displacement)
  85. PublicKey.E = int(shiftValue.Int64())
  86. }
  87. // 使用公钥加密数据
  88. ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, PublicKey, plaintext)
  89. if err != nil {
  90. log.Println("加密失败:", err)
  91. return "", err
  92. }
  93. return string(ciphertext), nil
  94. }
  95. func DisplacementDecryption(content string) (string, error) {
  96. if content == "" {
  97. return "", errors.New("解密内容为空")
  98. }
  99. if PrivateKey == nil {
  100. return "", errors.New("无效私钥")
  101. }
  102. if Displacement != 0 {
  103. exponent := big.NewInt(Displacement) // 位移处理的值
  104. PrivateKey.D = new(big.Int).Mul(PrivateKey.D, exponent)
  105. }
  106. ciphertext, err := base64.StdEncoding.DecodeString(content)
  107. if err != nil {
  108. log.Println("密文解码失败:", err)
  109. return "", errors.New("密文解码失败")
  110. }
  111. // 使用私钥解密数据
  112. decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
  113. if err != nil {
  114. log.Println("解密失败:", err)
  115. return "", err
  116. }
  117. return string(decryptedText), nil
  118. }
  119. // Encryption 加密
  120. func Encryption(content string) (string, error) {
  121. if content == "" {
  122. return "", errors.New("加密内容为空")
  123. }
  124. plaintext := []byte(content)
  125. // 使用公钥加密数据
  126. ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, PublicKey, plaintext)
  127. if err != nil {
  128. log.Println("加密失败:", err)
  129. return "", err
  130. }
  131. return string(ciphertext), nil
  132. }
  133. func Decryption(content string) (string, error) {
  134. if content == "" {
  135. return "", errors.New("解密内容为空")
  136. }
  137. if PrivateKey == nil {
  138. return "", errors.New("无效私钥")
  139. }
  140. ciphertext, err := base64.StdEncoding.DecodeString(content)
  141. if err != nil {
  142. log.Println("密文解码失败:", err)
  143. return "", errors.New("密文解码失败")
  144. }
  145. // 使用私钥解密数据
  146. decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
  147. if err != nil {
  148. log.Println("解密失败:", err)
  149. return "", err
  150. }
  151. return string(decryptedText), nil
  152. }