'''Diffe-Hell,an key exchage, used for an xored onetime pad.'''
from ElGamalInt import keygen # from HW solution, now uploaded ino code

def dhCommonKey(p, public, private):
    ''' return the shared diffe-Hellman key given the prime modulus,
    other person's public key and your private key, with public, private,
    and the returned shared key as integers in [0, p).
    '''
    #code

def oneTimeDHCrypt(p, public, private, msg):
    ''' Assume p is prime and other parameters are integers in [0, p).
    Generate a Diffe-Hellman key from p, public, and private, and use it
    to return an xor encryption/decryption of msg.  This function works
    for both encryption and decryption, reversing whose public and private
    keys are used, with msg first as plaintext, and second as ciphertext.'''

    #code

def testOneTimeDH(p, g, msg):
    ''' p prime, g generator, msg < p. Test oneTimeDHCrypt.'''
    (myPub, myPriv) = keygen(p, g) # by me,I send myPub to you
    (yourPub, yourPriv) = keygen(p, g) # by you, yourPub sent to me
    cipher = oneTimeDHCrypt(p, yourPub, myPriv, msg) # by me, cipher sent to you
    decrypted = oneTimeDHCrypt(p, myPub, yourPriv, cipher)#by you, get decrypted
    print cipher, myPriv, myPub, yourPriv, yourPub
    print "msg      ", msg
    print "decrypted", decrypted
    if msg != decrypted:  print 'DIFFERENT!'
    print

if __name__ == '__main__':
    testOneTimeDH(29, 3, 20)
    testOneTimeDH(4007, 589, 1234)
