<commit_before>#!/usr/bin/python2.7

# http://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
# http://de.wikipedia.org/wiki/RSA-Kryptosystem
# http://www2.informatik.uni-hamburg.de/wsv/teaching/sonstiges/EwA-Folien/Sohst-Folien.pdf

import hashlib, base64
import logging
import binascii
from math import floor
from my_xtea import encrypt_cbc, decrypt_cbc

seperator = "-" * 80

def gen_dhke_key(base, exp, div):
	return base ** exp % div

def euclid(a, b):
	if b == 0:
		return a
	else:
		return euclid(b, a % b)

def extended_euclid(a, b):
	'''
	Taken from http://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
	'''
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = extended_euclid(b % a, a)
	return (g, x - (b // a) * y, y) # // Floor Division

def mod_inv(a, m):
	'''
	Returns modular inverse if exisits else None 
	Taken from http://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
	'''
	gcd, x, y = extended_euclid(a, m)
	if gcd != 1:
		return None
	else:
		return x % m

def rsa_cipher(msg, key, n):
	return msg ** key % n

def a1():
	# Aufgabe 1
	print seperator
	print "Aufgabe 1"
	print seperator
	p = 467
	g = 2
	rand_set = [(2, 5), (400, 134), (228, 57)]
	print "p = {}, g = {}".format(p, g)
	print seperator
	for a, b in rand_set:
		print "a = {}, b = {}".format(a, b)
		print "Public key A = {}".format(gen_dhke_key(g, a, p))
		print "Public key B = {}".format(gen_dhke_key(g, b, p))
		print "Shared key = {}".format(a, b, gen_dhke_key(g, a * b, p))

def a2():
	# Aufgabe 2
	print seperator
	print "Aufgabe 2"
	print seperator
	p = 5
	q = 11
	e = 3
	msg = 9
	n = p * q
	phi_n = (p - 1) * (q - 1)

	# Calculate private exponent
	d = mod_inv(e, phi_n)
	print "Public key = ({}, {})".format(e, n)
	print "Private key = ({}, {})".format(d, n)

	encrypted_msg = rsa_cipher(msg, e, n)
	print "Plain message = {}".format(msg)
	print "Encrypted message = {}".format(encrypted_msg)
	print "Decrypted message = {}".format(rsa_cipher(encrypted_msg, d, n))

def a3():
	# Aufgabe 3
	print seperator
	print "Aufgabe 3"
	print seperator
	p = 41<commit_msg>Shared keys<commit_after>#!/usr/bin/python2.7

# http://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
# http://de.wikipedia.org/wiki/RSA-Kryptosystem
# http://www2.informatik.uni-hamburg.de/wsv/teaching/sonstiges/EwA-Folien/Sohst-Folien.pdf

import hashlib, base64
import logging
import binascii
from math import floor
from my_xtea import encrypt_cbc, decrypt_cbc

seperator = "-" * 80

def gen_dhke_key(base, exp, div):
	return base ** exp % div

def euclid(a, b):
	if b == 0:
		return a
	else:
		return euclid(b, a % b)

def extended_euclid(a, b):
	'''
	Taken from http://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
	'''
	if a == 0:
		return (b, 0, 1)
	else:
		g, y, x = extended_euclid(b % a, a)
	return (g, x - (b // a) * y, y) # // Floor Division

def mod_inv(a, m):
	'''
	Returns modular inverse if exisits else None 
	Taken from http://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
	'''
	gcd, x, y = extended_euclid(a, m)
	if gcd != 1:
		return None
	else:
		return x % m

def rsa_cipher(msg, key, n):
	return msg ** key % n

def a1():
	# Aufgabe 1
	print seperator
	print "Aufgabe 1"
	print seperator
	p = 467
	g = 2
	rand_set = [(2, 5), (400, 134), (228, 57)]
	print "p = {}, g = {}".format(p, g)
	print seperator
	for a, b in rand_set:
		print "a = {}, b = {}".format(a, b)
		public_key_A = gen_dhke_key(g, a, p)
		print "A = {}".format(public_key_A)
		public_key_B = gen_dhke_key(g, b, p)
		print "B = {}".format(public_key_B)
		
		shared_key_B = gen_dhke_key(public_key_A, b, p)
		shared_key_A = gen_dhke_key(public_key_B, a, p)
		shared_key = gen_dhke_key(g, a * b, p)
		
		assert(shared_key == shared_key_A == shared_key_B)
		
		print "Shared key K_B = {}".format(shared_key_B)
		print "Shared key K_A = {}".format(shared_key_A)
		print "Shared key K = {}".format(shared_key)

def a2():
	# Aufgabe 2
	print seperator
	print "Aufgabe 2"
	print seperator
	p = 5
	q = 11
	e = 3
	msg = 9
	n = p * q
	phi_n = (p - 1) * (q - 1)

	# Calculate private exponent
	d = mod_inv(e, phi_n)
	print "Public key = ({}, {})".format(e, n)
	print "Private key = ({}, {})".format(d, n)

	encrypted_msg = rsa_cipher(msg, e, n)
	print "Plain message = {}".format(msg)
	print "Encrypted message = {}".format(encrypted_msg)
	print "Decrypted message = {}".format(rsa_cipher(encrypted_msg, d, n))

def a3():
	# Aufgabe 3
	print seperator
	print "Aufgabe 3"
	print seperator
	p = 41
{'fraction_non_alphanumeric': 0.15175513698630136, 'fraction_numerical': 0.02011986301369863, 'mean_word_length': 3.797741273100616, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5339492', 'n_tokens_mistral': 2122, 'n_tokens_neox': 1966, 'n_words': 508}