crypt-rsa.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package util
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "crypto/x509"
  7. )
  8. // KeyRSA func
  9. func KeyRSA() *rsa.PrivateKey {
  10. var privateKey *rsa.PrivateKey
  11. var err error
  12. if privateKey, err = rsa.GenerateKey(rand.Reader, 2048); err != nil {
  13. panic(err)
  14. }
  15. return privateKey
  16. }
  17. // MarshalPublicKey func
  18. func MarshalPublicKey(key *rsa.PublicKey) []byte {
  19. if data, err := x509.MarshalPKIXPublicKey(key); err != nil {
  20. panic(err)
  21. } else {
  22. return data
  23. }
  24. }
  25. // UnmarshalPublicKey func
  26. func UnmarshalPublicKey(key []byte) *rsa.PublicKey {
  27. var kk interface{}
  28. var err error
  29. if kk, err = x509.ParsePKIXPublicKey(key); err != nil {
  30. panic(err)
  31. }
  32. return kk.(*rsa.PublicKey)
  33. }
  34. // MarshalPrivateKey func
  35. func MarshalPrivateKey(key *rsa.PrivateKey) []byte {
  36. return x509.MarshalPKCS1PrivateKey(key)
  37. }
  38. // UnmarshalPrivateKey func
  39. func UnmarshalPrivateKey(key []byte) *rsa.PrivateKey {
  40. var kk *rsa.PrivateKey
  41. var err error
  42. if kk, err = x509.ParsePKCS1PrivateKey(key); err != nil {
  43. panic(err)
  44. }
  45. return kk
  46. }
  47. // EncryptRSA func
  48. func EncryptRSA(key *rsa.PublicKey, data []byte) []byte {
  49. var err error
  50. klen := key.N.BitLen()/8 - 11
  51. if len(data) <= klen {
  52. var bb []byte
  53. if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data); err != nil {
  54. panic(err)
  55. }
  56. return bb
  57. }
  58. var buf bytes.Buffer
  59. var bb []byte
  60. for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
  61. if r <= klen {
  62. if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
  63. panic(err)
  64. }
  65. buf.Write(bb)
  66. w = r
  67. } else {
  68. if bb, err = rsa.EncryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
  69. panic(err)
  70. }
  71. buf.Write(bb)
  72. w = klen
  73. }
  74. }
  75. return buf.Bytes()
  76. }
  77. // DecryptRSA func
  78. func DecryptRSA(key *rsa.PrivateKey, data []byte) []byte {
  79. var err error
  80. klen := key.N.BitLen() / 8
  81. if len(data) <= klen {
  82. var bb []byte
  83. if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data); err != nil {
  84. panic(err)
  85. }
  86. return bb
  87. }
  88. var buf bytes.Buffer
  89. var bb []byte
  90. for i, w, r := 0, 0, len(data); r > 0; i, r = i+w, r-w {
  91. if r <= klen {
  92. if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:]); err != nil {
  93. panic(err)
  94. }
  95. buf.Write(bb)
  96. w = r
  97. } else {
  98. if bb, err = rsa.DecryptPKCS1v15(rand.Reader, key, data[i:i+klen]); err != nil {
  99. panic(err)
  100. }
  101. buf.Write(bb)
  102. w = klen
  103. }
  104. }
  105. return buf.Bytes()
  106. }