Asymetric and symmetric
in transit and at rest
In [1]:
100 % 9
Out[1]:
In [2]:
(5**3) % 17
Out[2]:
In [8]:
import random
random.randint(0, 100)
Out[8]:
In [9]:
import os
def rand_int(nbytes):
return int.from_bytes(os.urandom(nbytes), byteorder='little')
rand_int(50)
Out[9]:
In [14]:
def rand_less_than(upper_bound):
while True:
r = rand_int(((upper_bound).bit_length() + 7) // 8)
if r < upper_bound:
return r
def is_prime(p):
for _ in range(5): # do this 5 times
a = rand_less_than(p) # picks a number smaller than
if not pow(a, p - 1, p) == 1: # a**(p - 1) % p == 1
return False
return True
In [17]:
is_prime(233)
Out[17]:
In [18]:
# alice and bob are choose a big prime p, and a base g
# alice choose a secret called 'a'
# bob chooose a secret called 'b'
# alices sends g**a % p = A
# bob sends g**b % p = B
# alice calculates B**a
# bob calculates A**b
In [19]:
# A = g**a %p, b
# A**b -> (g**a)**b % p
In [21]:
def choose_parameters():
while True:
modulus = rand_int(1024 // 8)
if is_prime(modulus):
break
base = rand_less_than(modulus)
return base, modulus
In [23]:
g, p = choose_parameters()
In [25]:
class Peep:
def __init__(self, base, modulus): # g, p
self.base = base
self.modulus = modulus
self.private = rand_less_than(modulus)
self.public = pow(base, self.private, modulus)
def send(self):
return self.public
def receive(self, B):
self.shared_secret = pow(B, self.private, self.modulus)
In [28]:
alice = Peep(g, p)
bob = Peep(g, p)
A = alice.send()
B = bob.send()
A, B
Out[28]:
In [29]:
bob.receive(A)
alice.receive(B)
In [30]:
alice.shared_secret, bob.shared_secret
Out[30]:
In [ ]:
http://pastebin.com/7QARWsF0
In [31]:
from hashlib import sha256
In [35]:
hasher = sha256()
In [38]:
hasher.update('bad stuff'.encode('ascii'))
In [39]:
hasher.update('cuss words'.encode('ascii'))
In [40]:
hasher.digest()
Out[40]:
In [41]:
(2**8)**20
Out[41]:
In [43]:
import math
math.log10(1461501637330902918203684832716283019655932542976)
Out[43]: