keys.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package keys
  2. /*
  3. Keys: holding private and public keys
  4. */
  5. import (
  6. "fmt"
  7. "tools/common"
  8. "tools/rsa/integer"
  9. )
  10. type private struct {
  11. p, q, b, d *integer.RSAInt
  12. }
  13. type public struct {
  14. N, E *integer.RSAInt
  15. }
  16. type Keys struct {
  17. *private
  18. *public
  19. base_init float64
  20. base,
  21. exp int64
  22. }
  23. func New(base_init float64, base, exp int64) (*Keys) {
  24. k := new(Keys)
  25. k.private = new(private)
  26. k.public = new(public)
  27. k.base_init, k.base, k.exp = base_init, base, exp
  28. return k
  29. }
  30. func (k *Keys) Private() *private {
  31. return k.private
  32. }
  33. func (k *Keys) Public() *public {
  34. return k.public
  35. }
  36. func (k *Keys) GenPrimes() {
  37. for k.Private().init(k); !k.Public().init(k); k.Private().init(k) {}
  38. k.Private().d.ModMulInv(k.Public().E, k.Private().b)
  39. }
  40. func (k *Keys) Crack() {
  41. _, k.Private().p, k.Private().q = k.Public().N.PollardPm1(2)
  42. fmt.Printf("p:%s\tq:%s\n", k.Private().p, k.Private().q)
  43. k.Private().b = k.Private().p.Copy().Mul(k.Private().q).EulerTot(k.Private().p, k.Private().q)
  44. k.Private().d.ModMulInv(k.Public().E, k.Public().N)
  45. }
  46. func (p *private) init(k *Keys) {
  47. p.p = integer.New(
  48. int64(k.base_init * float64(k.base),
  49. )).Mul(
  50. integer.New(k.base).Pow64(
  51. (k.exp / 2) - int64(common.IntLen(k.base)),
  52. ),
  53. )
  54. p.q = p.p.Copy()
  55. p.genPQ()
  56. p.b = p.p.Copy().Mul(p.q).EulerTot(p.p, p.q)
  57. p.d = integer.New(0)
  58. }
  59. func (p *private) genPQ() {
  60. p.p.NextRandPrime()
  61. p.q.NextRandPrime()
  62. if p.p.Eq(p.q) {
  63. p.genPQ()
  64. }
  65. }
  66. func (p *public) init(k *Keys) bool {
  67. p.N = k.Private().p.Copy().Mul(k.Private().q)
  68. p.E = k.Private().b.Copy().RelativePrime()
  69. return p.testN(k) && p.E != nil
  70. }
  71. func (p *public) testN(k *Keys) bool {
  72. s := fmt.Sprintf("%d", int64(k.base_init * float64(k.base)))
  73. for i := int(0); i < int(k.exp) - common.IntLen(k.base); i++ {
  74. s += "0"
  75. }
  76. if t, e := integer.New(0).SetString(s); e {
  77. return p.N.Gt(t)
  78. }
  79. return false
  80. }
  81. func (k *Keys) SetKeys(d, e, n string) bool {
  82. k.Private().d = integer.New(0)
  83. k.Public().E = integer.New(0)
  84. k.Public().N = integer.New(0)
  85. _, d_success := k.Private().d.SetString(d)
  86. _, e_success := k.Public().E.SetString(e)
  87. _, n_success := k.Public().N.SetString(n)
  88. return d_success && e_success && n_success
  89. }
  90. func (p *private) D() *integer.RSAInt {
  91. return p.d
  92. }
  93. func (k *Keys) StrKeys() string {
  94. return fmt.Sprintf(
  95. "PRIVATE:\n\tp:%s\n\tq:%s\n\tb:%s\n\td:%s\nPUBLIC:\n\tn:%s\n\te:%s",
  96. k.Private().p,
  97. k.Private().q,
  98. k.Private().b,
  99. k.Private().d,
  100. k.Public().N,
  101. k.Public().E,
  102. )
  103. }