rsa_utils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, encrypted string) ([]byte, error) {
  22. encKey, _ := base64.StdEncoding.DecodeString(encrypted)
  23. blockSize := privateKey.Size()
  24. maxChunkSize := blockSize
  25. var plaintext []byte
  26. for len(encKey) > 0 {
  27. chunkSize := maxChunkSize
  28. if len(encKey) < chunkSize {
  29. chunkSize = len(encKey)
  30. }
  31. chunk := encKey[:chunkSize]
  32. encKey = encKey[chunkSize:]
  33. decryptedChunk, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, chunk)
  34. if err != nil {
  35. return nil, err
  36. }
  37. plaintext = append(plaintext, decryptedChunk...)
  38. }
  39. //hash, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encKey)
  40. //if err != nil {
  41. // return nil, err
  42. //}
  43. return plaintext, nil
  44. }
  45. // ParsePrivateKeyFromPEM 解析RSA公钥
  46. func ParsePrivateKeyFromPEM() (privateKey *rsa.PrivateKey, err error) {
  47. pemBlock, err := os.ReadFile("./conf/rsa_private_key.pem")
  48. block, _ := pem.Decode(pemBlock)
  49. if block == nil {
  50. logger.Error("私钥解析失败")
  51. return nil, errors.New("私钥解析失败")
  52. }
  53. privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return
  58. }
  59. func ParsePrivateKey(configPath string) (privateKey *rsa.PrivateKey, err error) {
  60. pemBlock, err := os.ReadFile(configPath)
  61. block, _ := pem.Decode(pemBlock)
  62. if block == nil {
  63. logger.Error("私钥解析失败")
  64. return nil, errors.New("私钥解析失败")
  65. }
  66. privateInfo, err := x509.ParsePKCS8PrivateKey(block.Bytes)
  67. if err != nil {
  68. return nil, err
  69. }
  70. privateKey = privateInfo.(*rsa.PrivateKey)
  71. return
  72. }
  73. func ParsePublicKey(configPath string) (publicKey *rsa.PublicKey, err error) {
  74. pemBlock, err := os.ReadFile(configPath)
  75. block, _ := pem.Decode(pemBlock)
  76. if block == nil {
  77. logger.Error("公钥解析失败")
  78. return nil, errors.New("公钥解析失败")
  79. }
  80. key, err := x509.ParsePKIXPublicKey(block.Bytes)
  81. if err != nil {
  82. return nil, err
  83. }
  84. publicKey = key.(*rsa.PublicKey)
  85. return
  86. }
  87. // ParsePublicKeyFromPEM 解析RSA公钥
  88. func ParsePublicKeyFromPEM() (publicKey *rsa.PublicKey, err error) {
  89. pemBlock, err := os.ReadFile("./conf/rsa_public_key.pem")
  90. block, _ := pem.Decode(pemBlock)
  91. if block == nil {
  92. logger.Error("公钥解析失败")
  93. return nil, errors.New("公钥解析失败")
  94. }
  95. key, err := x509.ParsePKIXPublicKey(block.Bytes)
  96. if err != nil {
  97. return nil, err
  98. }
  99. publicKey = key.(*rsa.PublicKey)
  100. return
  101. }