rsa_crypto.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package utils
  3. import (
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/sha1"
  7. "crypto/x509"
  8. "encoding/base64"
  9. "fmt"
  10. )
  11. // EncryptOAEPWithPublicKey 使用 OAEP padding方式用公钥进行加密
  12. func EncryptOAEPWithPublicKey(message string, publicKey *rsa.PublicKey) (ciphertext string, err error) {
  13. if publicKey == nil {
  14. return "", fmt.Errorf("you should input *rsa.PublicKey")
  15. }
  16. ciphertextByte, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, publicKey, []byte(message), nil)
  17. if err != nil {
  18. return "", fmt.Errorf("encrypt message with public key err:%s", err.Error())
  19. }
  20. ciphertext = base64.StdEncoding.EncodeToString(ciphertextByte)
  21. return ciphertext, nil
  22. }
  23. // EncryptOAEPWithCertificate 先解析出证书中的公钥,然后使用 OAEP padding方式公钥进行加密
  24. func EncryptOAEPWithCertificate(message string, certificate *x509.Certificate) (ciphertext string, err error) {
  25. if certificate == nil {
  26. return "", fmt.Errorf("you should input *x509.Certificate")
  27. }
  28. publicKey, ok := certificate.PublicKey.(*rsa.PublicKey)
  29. if !ok {
  30. return "", fmt.Errorf("certificate is invalid")
  31. }
  32. return EncryptOAEPWithPublicKey(message, publicKey)
  33. }
  34. // EncryptPKCS1v15WithPublicKey 使用PKCS1 padding方式用公钥进行加密
  35. func EncryptPKCS1v15WithPublicKey(message string, publicKey *rsa.PublicKey) (ciphertext string, err error) {
  36. if publicKey == nil {
  37. return "", fmt.Errorf("you should input *rsa.PublicKey")
  38. }
  39. ciphertextByte, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(message))
  40. if err != nil {
  41. return "", fmt.Errorf("encrypt message with public key err:%s", err.Error())
  42. }
  43. ciphertext = base64.StdEncoding.EncodeToString(ciphertextByte)
  44. return ciphertext, nil
  45. }
  46. // EncryptPKCS1v15WithCertificate 先解析出证书中的公钥,然后使用PKCS1 padding方式用公钥进行加密
  47. func EncryptPKCS1v15WithCertificate(message string, certificate *x509.Certificate) (ciphertext string, err error) {
  48. if certificate == nil {
  49. return "", fmt.Errorf("you should input *x509.Certificate")
  50. }
  51. publicKey, ok := certificate.PublicKey.(*rsa.PublicKey)
  52. if !ok {
  53. return "", fmt.Errorf("certificate is invalid")
  54. }
  55. return EncryptPKCS1v15WithPublicKey(message, publicKey)
  56. }
  57. // DecryptOAEP 使用私钥进行解密
  58. func DecryptOAEP(ciphertext string, privateKey *rsa.PrivateKey) (message string, err error) {
  59. if privateKey == nil {
  60. return "", fmt.Errorf("you should input *rsa.PrivateKey")
  61. }
  62. decodedCiphertext, err := base64.StdEncoding.DecodeString(ciphertext)
  63. if err != nil {
  64. return "", fmt.Errorf("base64 decode failed, error=%s", err.Error())
  65. }
  66. messageBytes, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privateKey, decodedCiphertext, nil)
  67. if err != nil {
  68. return "", fmt.Errorf("decrypt ciphertext with private key err:%s", err)
  69. }
  70. return string(messageBytes), nil
  71. }
  72. // DecryptPKCS1v15 使用私钥对PKCS1 padding方式加密的字符串进行解密
  73. func DecryptPKCS1v15(ciphertext string, privateKey *rsa.PrivateKey) (message string, err error) {
  74. if privateKey == nil {
  75. return "", fmt.Errorf("you should input *rsa.PrivateKey")
  76. }
  77. decodedCiphertext, err := base64.StdEncoding.DecodeString(ciphertext)
  78. if err != nil {
  79. return "", fmt.Errorf("base64 decode failed, error=%s", err.Error())
  80. }
  81. messageBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, decodedCiphertext)
  82. if err != nil {
  83. return "", fmt.Errorf("decrypt ciphertext with private key err:%s", err)
  84. }
  85. return string(messageBytes), nil
  86. }