Skip to content

Instantly share code, notes, and snippets.

@0xdeadbeer
Last active March 12, 2023 13:53
Show Gist options
  • Save 0xdeadbeer/290a77b4312209344b9dc6a68d9857f1 to your computer and use it in GitHub Desktop.
Save 0xdeadbeer/290a77b4312209344b9dc6a68d9857f1 to your computer and use it in GitHub Desktop.
Wiener's attack on RSA - Python3
#!/usr/bin/python3
import contfrac
import math
"""
Generously coded by me and my friend in the middle of a chill Sunday
Resources:
- https://crypto.stanford.edu/~dabo/pubs/papers/RSA-survey.pdf
- https://en.wikipedia.org/wiki/Wiener%27s_attack
- https://en.wikipedia.org/wiki/RSA_(cryptosystem)
"""
def main():
e = 42667
N = 64741
k, d = zip(*contfrac.convergents((e,N)))
for i in range(len(k)):
try:
phi = (e * d[i] - 1) / k[i]
if (not phi.is_integer()):
continue
a = 1
b = phi-N-1
c = N
D = (b**2) - (4*a*c)
if (D < 0):
continue
if (not math.sqrt(D).is_integer()):
continue
p = int((-b+math.sqrt(D)) / (2 * a))
q = int((-b-math.sqrt(D)) / (2 * a))
print(f"Outfucked the system with p: {p} and q: {q}")
except Exception as exception:
# print("Exception on this phi: " + str(exception))
pass
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment