1.攻防世界初级区最后一题

已知椭圆曲线加密Ep(a,b)参数为
p = 15424654874903
a = 16546484
b = 4548674875
G(6478678675,5636379357093)
私钥为
k = 546768
求公钥K(x,y)

2.根据圆锥曲线的加密原理,写出解密脚本如下:

  1. import collections
  2. import random
  3. EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h')
  4. curve = EllipticCurve(
  5. 'secp256k1',
  6. # Field characteristic.
  7. p=int(input('p=')),
  8. # Curve coefficients.
  9. a=int(input('a=')),
  10. b=int(input('b=')),
  11. # Base point.
  12. g=(int(input('Gx=')),
  13. int(input('Gy='))),
  14. # Subgroup order.
  15. n=int(input('k=')),
  16. # Subgroup cofactor.
  17. h=1,
  18. )
  19. # Modular arithmetic ##########################################################
  20. def inverse_mod(k, p):
  21. """Returns the inverse of k modulo p.
  22. This function returns the only integer x such that (x * k) % p == 1.
  23. k must be non-zero and p must be a prime.
  24. """
  25. if k == 0:
  26. raise ZeroDivisionError('division by zero')
  27. if k < 0:
  28. # k ** -1 = p - (-k) ** -1 (mod p)
  29. return p - inverse_mod(-k, p)
  30. # Extended Euclidean algorithm.
  31. s, old_s = 0, 1
  32. t, old_t = 1, 0
  33. r, old_r = p, k
  34. while r != 0:
  35. quotient = old_r // r
  36. old_r, r = r, old_r - quotient * r
  37. old_s, s = s, old_s - quotient * s
  38. old_t, t = t, old_t - quotient * t
  39. gcd, x, y = old_r, old_s, old_t
  40. assert gcd == 1
  41. assert (k * x) % p == 1
  42. return x % p
  43. # Functions that work on curve points #########################################
  44. def is_on_curve(point):
  45. """Returns True if the given point lies on the elliptic curve."""
  46. if point is None:
  47. # None represents the point at infinity.
  48. return True
  49. x, y = point
  50. return (y * y - x * x * x - curve.a * x - curve.b) % curve.p == 0
  51. def point_neg(point):
  52. """Returns -point."""
  53. assert is_on_curve(point)
  54. if point is None:
  55. # -0 = 0
  56. return None
  57. x, y = point
  58. result = (x, -y % curve.p)
  59. assert is_on_curve(result)
  60. return result
  61. def point_add(point1, point2):
  62. """Returns the result of point1 + point2 according to the group law."""
  63. assert is_on_curve(point1)
  64. assert is_on_curve(point2)
  65. if point1 is None:
  66. # 0 + point2 = point2
  67. return point2
  68. if point2 is None:
  69. # point1 + 0 = point1
  70. return point1
  71. x1, y1 = point1
  72. x2, y2 = point2
  73. if x1 == x2 and y1 != y2:
  74. # point1 + (-point1) = 0
  75. return None
  76. if x1 == x2:
  77. # This is the case point1 == point2.
  78. m = (3 * x1 * x1 + curve.a) * inverse_mod(2 * y1, curve.p)
  79. else:
  80. # This is the case point1 != point2.
  81. m = (y1 - y2) * inverse_mod(x1 - x2, curve.p)
  82. x3 = m * m - x1 - x2
  83. y3 = y1 + m * (x3 - x1)
  84. result = (x3 % curve.p,
  85. -y3 % curve.p)
  86. assert is_on_curve(result)
  87. return result
  88. def scalar_mult(k, point):
  89. """Returns k * point computed using the double and point_add algorithm."""
  90. assert is_on_curve(point)
  91. if k < 0:
  92. # k * point = -k * (-point)
  93. return scalar_mult(-k, point_neg(point))
  94. result = None
  95. addend = point
  96. while k:
  97. if k & 1:
  98. # Add.
  99. result = point_add(result, addend)
  100. # Double.
  101. addend = point_add(addend, addend)
  102. k >>= 1
  103. assert is_on_curve(result)
  104. return result
  105. # Keypair generation and ECDHE ################################################
  106. def make_keypair():
  107. """Generates a random private-public key pair."""
  108. private_key = curve.n
  109. public_key = scalar_mult(private_key, curve.g)
  110. return private_key, public_key
  111. private_key, public_key = make_keypair()
  112. print("private key:", hex(private_key))
  113. print("public key: (0x{:x}, 0x{:x})".format(*public_key))

根据题目提示flag为公钥相加的值,提交的时候把末尾的**L**去掉
image.png