Skip to content

Instantly share code, notes, and snippets.

@kiriappeee
Created April 12, 2018 09:38
Show Gist options
  • Save kiriappeee/b782cfbebe42e972f2cc613f0023dabc to your computer and use it in GitHub Desktop.
Save kiriappeee/b782cfbebe42e972f2cc613f0023dabc to your computer and use it in GitHub Desktop.
A Diffie Helman Story. Alice and Bob meet a thief while exchanging keys
from Crypto.Util import number
from datetime import datetime
import time
p=int("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199FFFFFFFFFFFFFFFF", 16)
g=5
smallestRandomNumber=int("1"+"0"*255, 2)
def thief(A,a):
print("Greetings! I AM A THIEF!!! Mc Von Noodle at your service! I caught your numbers being exchanged. Now I shall.... CRACK THEM\n\n")
time.sleep(5)
print("dun dun duuuuuuuuuuun")
time.sleep(5)
print("""
All I need to do is find a number that can calculate what you used to get {} using g^a mod p!! HAHAHA
BEGIN!!!""".format(A))
crackStart=datetime.now()
testNumber=smallestRandomNumber
totalTime=0
while testNumber-smallestRandomNumber<9200:
if pow(g, testNumber, p) == A:
print("FOOLISH MORTALS! I HAVE FOUND THE ANSWER AND IT IS.... {}".format(testNumber))
break
if testNumber-smallestRandomNumber!=0 and (testNumber-smallestRandomNumber)%1000==0:
totalTime+=datetime.now().timestamp()-crackStart.timestamp()
averageTime=totalTime/((testNumber-smallestRandomNumber)//1000)
print("I JUST CHECKED {onethousandth} NUMBERS!!! {onethousandth}!! And it's only taking me an average of {averageTime} seconds!".format(onethousandth=testNumber-smallestRandomNumber, averageTime=averageTime))
crackStart=datetime.now()
testNumber+=1
print("Wait...")
time.sleep(2)
print("This is taking too long isn't it?")
time.sleep(2)
print("STOP LAUGHING!!!")
time.sleep(2)
totalAttemptsRequired=a-smallestRandomNumber
totalThousands=totalAttemptsRequired//1000
totalTimeRequired=int(totalThousands*averageTime/60/60/24/365/1000)
print("What? By your calculations it will take... {} MILLENIA????".format(totalTimeRequired))
time.sleep(5)
print("Aha!\n\n\nI call upon the most powerful bitcoin mining rig ever!!! And I'll use golang! And parallelism")
time.sleep(7)
print("Ha! It now takes only 0.001 seconds to go through a 1000 numbers!")
time.sleep(2)
totalTimeRequired=int(totalThousands*0.2/60/60/24/365/1000)
print("What? It'll still take {} MILLENIA???".format(totalTimeRequired))
time.sleep(5)
print("Thief has exited the building with a \"I'LL BE BAAAAACK!!\"")
print("""
-----------------------------------------------
A Diffie Helman Story
Alice and Bob meet a thief
----------------------------------------------
""")
time.sleep(5)
print("Alice and Bob are about to go through a full Diffie Helman Exchange\n\n")
time.sleep(4)
dhExchangeStart=datetime.now()
a=number.getRandomNBitInteger(256)
b=number.getRandomNBitInteger(256)
A=pow(g,a,p)
print("Alice is sending over {}\n\n".format(A))
B=pow(g,b,p)
print("Bob is sending over {}\n\n".format(B))
print("They are mixing their stuff up now\n\n")
sA=pow(B,a,p)
sB=pow(A,b,p)
if sA==sB:
print("Diffie Helman Exchanged! Final key is {}\n\n".format(sA))
print("Time elapsed to exchange: {}\n\n---------------------------------------------".format(datetime.now().timestamp()-dhExchangeStart.timestamp()))
time.sleep(4)
print("But what Alice and Bob did not realise was that a wily thief was capturing the exchange of their numbers. And he's about to spring his trap")
time.sleep(12)
thief(A,a)
else:
print("ahem. That's embarassing. It failed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment