def test_BBS(key_len=128, q=None, p=None, bitlen=1024): start_time = time.time() bbs = BlumBlumShub(q=q, p=p, bitlen=bitlen) pub1 = bbs.generate(seed=42, key_len=key_len) bbs2 = BlumBlumShub(q=q, p=p, bitlen=bitlen) pub2 = bbs.generate(seed=42, key_len=key_len) assert pub1 == pub2 assert len(pub1) == key_len total_time = (time.time() - start_time) print(f'##########\nPassed BBS test in {total_time}s\n##########')
def main(): print(cyan("Prime Number Generator")) bbs = BlumBlumShub( 47549311472920909912209319415632546568615171647449776708088515456271882256688655472429960001356598002721056949199289005875090341339535679228762000284695578992694028821429479898585483427123161057642719497767775759219743666776381110838744061389431879168072462318464481040242866241897410149885018525271140808743387765598861025306828071770446075734192300013226299729071396927791922757802315939093736083879788104144364331864519291777448965862396859815581731257814820213290000280532074935599904323257225411772963246498810492981761448004460009228598633053, 107397595365822977137336244061581024502786353619930520168857195796487927688075184909580315280089385066594856534824324028916873227593184312306380432882318358133946408364432997211883642081142345263913093524717335572775257117166625838352501167314403619745327796928224603118553911877190432477897849958446446909190088634315248524470293658599768274374072505636243343338931450592489529079411012054264332025498428060511919416556838891807530769004624034148341042211078576228613399736180978723912602319129778598464870910571473064783677343206518408119156722081, 505681814266168811) lcg = LinearCongruentialGenerator( # a, x0, c, m 25214903917, 505681814266168811, 11, pow(2,60) # 60 -> 256 bits; 300 -> 512 bits; 780 -> 1024 bits; 1720 -> 2048 bits; 3650 -> 4096 bits ) start_time = time.time() print("RUNNING") for i in range(0,350): # num = bbs.next() num = lcg.next() # print(f"\n{num} ({sys.getsizeof(num)*8} bits) (iteration {i+1})") # if fermat(num): if miller_rabin(num): print(f"\nPRIME: {num} ({sys.getsizeof(num)*8} bits) (iteration {i+1}) (time {(time.time() - start_time)*1000} ms)") print("FINISHED")
def new_chat(): """ 1. Clear current session 2. Initialize a new Diffie-Hellman object and generate your public key 3. Post your public key together with prime and generator so receiving end can set up correct 4. Get public key from other end 5. Compute session key (based on received public key) 6. Further improve session key by using Blum Blum Shub generator """ clear_session() dh = DiffieHellman() public_key = dh.get_public_key() # Send to other part params = { 'generator': dh.get_generator(), 'prime': dh.get_prime(), 'key': public_key } url = session['endpoint'] + '/getpub' res = requests.post(url, json=params).json() received_public_key = res['key'] session['key'] = dh.get_session_key(public_key=received_public_key) #CSPRNG bbs = BlumBlumShub(q=Q, p=P) secret_key = bbs.generate(seed=session['key'], key_len=128) session['secret_key'] = secret_key session['active_chat'] = True return redirect(url_for('index'))
def test_protocol(): start_time = time.time() Alice = DiffieHellman() q = Alice.get_generator() p = Alice.get_prime() Bob = DiffieHellman(generator=q, prime=p) # Compute respective public keys PU_A = Alice.get_public_key() PU_B = Bob.get_public_key() # Compute shared session key K_A = Alice.get_session_key(PU_B) K_B = Bob.get_session_key(PU_A) assert K_A == K_B #CSPRNG bbs = BlumBlumShub(q=383, p=503) Alice_secret_key = bbs.generate(seed=K_A, key_len=128) Bob_secret_key = bbs.generate(seed=K_B, key_len=128) assert Alice_secret_key == Bob_secret_key assert len(Alice_secret_key) == 128 # From Alice to Bob message = "Hi Bob, this is a message that should be encrypted properly" enc_message = encrypt(message, Alice_secret_key) dec_message = decrypt(enc_message, Bob_secret_key) assert message in dec_message # From Bob to Alice message = "Hi Alice, this is also a message that should be encrypted properly" enc_message = encrypt(message, Bob_secret_key) dec_message = decrypt(enc_message, Alice_secret_key) assert message in dec_message total_time = (time.time() - start_time) print(f'##########\nPassed protocol test in {total_time}s\n##########')
def get_pub(): """ POST: Calculate new session key GET: Return public key """ print('METHOD: ', request.method) if request.method == 'POST': # Calculate new session key clear_session() data = request.get_json(force=True) #Use same generator and prime as the user who initiated this session dh = DiffieHellman(generator=data['generator'], prime=data['prime']) public_key = dh.get_public_key() # Send to other part session['key'] = dh.get_session_key(data["key"]) #CSPRNG bbs = BlumBlumShub(q=Q, p=P) secret_key = bbs.generate(seed=session['key'], key_len=128) session['secret_key'] = secret_key session['active_chat'] = True return jsonify({'key': public_key}) else: return session['key']
print(f'\nYou have succesfully set up a shared key for Alice and Bob:\nAlice: {K_A}\nBob: {K_B}') print("\nYou will now further improve the shared key by using Blum Blum Shub") q = int(input("Define q for BBS (press 0 to use default) ")) p = int(input("Define p for BBS (press 0 to use default) ")) bitlen = int(input("Define bitlen for BBS (press 0 to use default(1024)) ")) key_len = int(input("Define key length to be used for AES (press 0 to use default (128)) ")) if q == 0: q = None if p == 0: p = None if bitlen == 0: bitlen = 1024 if key_len == 0: key_len = 128 #CSPRNG bbs = BlumBlumShub(q=q, p=p, bitlen=bitlen) Alice_secret_key = bbs.generate(seed=K_A, key_len=key_len) Bob_secret_key = bbs.generate(seed=K_B, key_len=key_len) key_len = len(Bob_secret_key) print(f'\nYou have succesfully set up a more secure shared key of length {key_len} for Alice and Bob:\nAlice: {Alice_secret_key}\nBob: {Bob_secret_key}') print("\nTest the protocol by writing a message") message = input("\n(Alice) Write your message to Bob here: ") enc_message = encrypt(message, Alice_secret_key) dec_message = decrypt(enc_message, Bob_secret_key) print(f'\nMessage sent over the network: {enc_message}') print(f'\nBob decrypting the message by his generated key: {dec_message}') message = input("\n(Bob) Write your message to Alice here: ") enc_message = encrypt(message, Bob_secret_key) dec_message = decrypt(enc_message, Alice_secret_key) print(f'\nMessage sent over the network: {enc_message}')