rsa_utils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package auth
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "encoding/base64"
  7. "encoding/pem"
  8. "errors"
  9. logger "eta/eta_mini_ht_api/common/component/log"
  10. "os"
  11. )
  12. // EncryptWithRSA 使用 RSA 公钥加密数据
  13. func EncryptWithRSA(publicKey *rsa.PublicKey, data []byte) ([]byte, error) {
  14. encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, data)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return encrypted, nil
  19. }
  20. // DecryptWithRSA 使用 RSA 私钥解密数据
  21. func DecryptWithRSA(privateKey *rsa.PrivateKey, encryptedText string) (plaintext []byte, err error) {
  22. encryptBytes, _ := base64.StdEncoding.DecodeString(encryptedText)
  23. maxChunkSize := privateKey.Size()
  24. for len(encryptBytes) > 0 {
  25. chunkSize := maxChunkSize
  26. if len(encryptBytes) < chunkSize {
  27. chunkSize = len(encryptBytes)
  28. }
  29. encryptedChunk := encryptBytes[:chunkSize]
  30. encryptBytes = encryptBytes[chunkSize:]
  31. var decryptedChunk []byte
  32. decryptedChunk, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, encryptedChunk)
  33. if err != nil {
  34. logger.Error("RSA解密失败", err)
  35. return
  36. }
  37. plaintext = append(plaintext, decryptedChunk...)
  38. }
  39. return
  40. }
  41. // ParsePrivateKeyFromPEM 解析RSA公钥
  42. func ParsePrivateKeyFromPEM() (privateKey *rsa.PrivateKey, err error) {
  43. pemBlock, err := os.ReadFile("./conf/rsa_private_key.pem")
  44. block, _ := pem.Decode(pemBlock)
  45. if block == nil {
  46. logger.Error("私钥解析失败")
  47. return nil, errors.New("私钥解析失败")
  48. }
  49. privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return
  54. }
  55. func ParsePrivateKey(configPath string) (privateKey *rsa.PrivateKey, err error) {
  56. pemBlock, err := os.ReadFile(configPath)
  57. block, _ := pem.Decode(pemBlock)
  58. if block == nil {
  59. logger.Error("私钥解析失败")
  60. return nil, errors.New("私钥解析失败")
  61. }
  62. privateInfo, err := x509.ParsePKCS8PrivateKey(block.Bytes)
  63. if err != nil {
  64. return nil, err
  65. }
  66. privateKey = privateInfo.(*rsa.PrivateKey)
  67. return
  68. }
  69. func ParsePublicKey(configPath string) (publicKey *rsa.PublicKey, err error) {
  70. pemBlock, err := os.ReadFile(configPath)
  71. block, _ := pem.Decode(pemBlock)
  72. if block == nil {
  73. logger.Error("公钥解析失败")
  74. return nil, errors.New("公钥解析失败")
  75. }
  76. key, err := x509.ParsePKIXPublicKey(block.Bytes)
  77. if err != nil {
  78. return nil, err
  79. }
  80. publicKey = key.(*rsa.PublicKey)
  81. return
  82. }
  83. // ParsePublicKeyFromPEM 解析RSA公钥
  84. func ParsePublicKeyFromPEM() (publicKey *rsa.PublicKey, err error) {
  85. pemBlock, err := os.ReadFile("./conf/rsa_public_key.pem")
  86. block, _ := pem.Decode(pemBlock)
  87. if block == nil {
  88. logger.Error("公钥解析失败")
  89. return nil, errors.New("公钥解析失败")
  90. }
  91. key, err := x509.ParsePKIXPublicKey(block.Bytes)
  92. if err != nil {
  93. return nil, err
  94. }
  95. publicKey = key.(*rsa.PublicKey)
  96. return
  97. }