rsa.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package rsa
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "encoding/base64"
  7. "encoding/pem"
  8. )
  9. var _ Public = (*rsaPub)(nil)
  10. var _ Private = (*rsaPri)(nil)
  11. type Public interface {
  12. i()
  13. EncryptURLEncoding(encryptStr string) (string, error)
  14. Encrypt(encryptStr string) (string, error)
  15. }
  16. type Private interface {
  17. i()
  18. Decrypt(decryptStr string) (string, error)
  19. DecryptURLEncoding(decryptStr string) (string, error)
  20. }
  21. type rsaPub struct {
  22. PublicKey string
  23. }
  24. type rsaPri struct {
  25. PrivateKey string
  26. }
  27. func NewPublic(publicKey string) Public {
  28. return &rsaPub{
  29. PublicKey: publicKey,
  30. }
  31. }
  32. func NewPrivate(privateKey string) Private {
  33. return &rsaPri{
  34. PrivateKey: privateKey,
  35. }
  36. }
  37. func (pub *rsaPub) i() {}
  38. func (pub *rsaPub) Encrypt(encryptStr string) (string, error) {
  39. // pem 解码
  40. block, _ := pem.Decode([]byte(pub.PublicKey))
  41. // x509 解码
  42. publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
  43. if err != nil {
  44. return "", err
  45. }
  46. // 类型断言
  47. publicKey := publicKeyInterface.(*rsa.PublicKey)
  48. //对明文进行加密
  49. encryptedStr, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(encryptStr))
  50. if err != nil {
  51. return "", err
  52. }
  53. //返回密文
  54. return base64.StdEncoding.EncodeToString(encryptedStr), nil
  55. }
  56. func (pub *rsaPub) EncryptURLEncoding(encryptStr string) (string, error) {
  57. // pem 解码
  58. block, _ := pem.Decode([]byte(pub.PublicKey))
  59. // x509 解码
  60. publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
  61. if err != nil {
  62. return "", err
  63. }
  64. // 类型断言
  65. publicKey := publicKeyInterface.(*rsa.PublicKey)
  66. //对明文进行加密
  67. encryptedStr, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(encryptStr))
  68. if err != nil {
  69. return "", err
  70. }
  71. //返回密文
  72. return base64.URLEncoding.EncodeToString(encryptedStr), nil
  73. }
  74. func (pri *rsaPri) i() {}
  75. func (pri *rsaPri) Decrypt(decryptStr string) (string, error) {
  76. // pem 解码
  77. block, _ := pem.Decode([]byte(pri.PrivateKey))
  78. // X509 解码
  79. privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  80. if err != nil {
  81. return "", err
  82. }
  83. decryptBytes, err := base64.StdEncoding.DecodeString(decryptStr)
  84. //对密文进行解密
  85. decrypted, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, decryptBytes)
  86. //返回明文
  87. return string(decrypted), nil
  88. }
  89. func (pri *rsaPri) DecryptURLEncoding(decryptStr string) (string, error) {
  90. // pem 解码
  91. block, _ := pem.Decode([]byte(pri.PrivateKey))
  92. // X509 解码
  93. privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  94. if err != nil {
  95. return "", err
  96. }
  97. decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr)
  98. //对密文进行解密
  99. decrypted, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, decryptBytes)
  100. //返回明文
  101. return string(decrypted), nil
  102. }