Exemple #1
0
def dh_decrypt(priv, ciphertext, aliceVer = None):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided. Optionally verify 
    the message came from Alice using her verification key."""
    
    # ciphertext be (iv, ciphertext, tag, sender_pub, sig)
    # bob decrypting: check sig using alice's pub ver key,
    # then decrypt using shared key derived from priv (bob's private key)

    # check input parameter format
    if (not isinstance(ciphertext, tuple)) or (isinstance(ciphertext, tuple) and len(ciphertext) != 5):
        raise Exception("Expecting tuple (iv, ciphertext, tag, sender public key, signature).")
    iv, encmsg, tag, sender_pub, sig = ciphertext

    # verify signature
    if aliceVer:
        if not sig:
           raise Exception("Signature required before decyption.")
        elif not do_ecdsa_verify(EcGroup(), aliceVer, sig, sha256(encmsg).digest()):
           raise Exception("Signature verification failed.")
    
    # shared key = bob priv x alice's pub point
    shared_key = priv * sender_pub
    # hash
    shared_key = sha256(shared_key.export()).digest()

    # decrypt
    aes = Cipher("aes-256-gcm")
    plaintext = aes.quick_gcm_dec(shared_key, iv, encmsg, tag)

    return plaintext.encode("utf-8")
Exemple #2
0
def decrypt_message(K, iv, ciphertext, tag):
    """ Decrypt a cipher text under a key K 

        In case the decryption fails, throw an exception.
    """
    aes = Cipher("aes-128-gcm")
    plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)

    return plain.encode("utf8")
Exemple #3
0
def decrypt_message(K, iv, ciphertext, tag, key_length=16):
    """ Decrypt a cipher text under a key K 

        In case the decryption fails, throw an exception.
    """
    aes = Cipher("aes-{}-gcm".format(key_length * 8))
    plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)

    return plain.encode("utf8")
Exemple #4
0
def decrypt_message(K, iv, ciphertext, tag):
    """ Decrypt a cipher text under a key K 

        In case the decryption fails, throw an exception.
    """
    ## YOUR CODE HERE
    aes = Cipher("aes-128-gcm")  # Initialize AES-GCM with 128 bit keys
    plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)

    return plain.encode("utf8")
def decrypt_message(K, iv, ciphertext, tag):
    """ Decrypt a cipher text under a key K 

        In case the decryption fails, throw an exception.
    """
    aes = Cipher("aes-128-gcm")
    plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)
    
    
    return plain.encode("utf8")
Exemple #6
0
def dh_decrypt(priv, ciphertext, aliceVer=None):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided. Optionally verify 
    the message came from Alice using her verification key."""

    ## YOUR CODE HERE
    iv, ciphertext, tag, pub_enc = ciphertext
    freshKey = pub_enc.pt_mul(priv).export()[:16]
    aes = Cipher("aes-128-gcm")
    plaintext = aes.quick_gcm_dec(freshKey, iv, ciphertext, tag)
    return plaintext.encode("utf8")
def dh_decrypt(priv, ciphertext, aliceVer=None):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided. Optionally verify 
    the message came from Alice using her verification key."""
    iv, enc_msg, tag, pub_enc = ciphertext
    shared_key = pub_enc.pt_mul(priv).export()
    hashed_shared_key = sha256(shared_key).digest()
    aes = Cipher("aes-128-gcm")
    plaintext = aes.quick_gcm_dec(hashed_shared_key[:16], iv, enc_msg,
                                  tag).encode("utf-8")
    return plaintext
def decrypt_message(K, iv, ciphertext, tag):
    """ Decrypt a cipher text under a key K 
        In case the decryption fails, throw an exception.
    """
    ## YOUR CODE HERE
    try:
        aes = Cipher("aes-128-gcm")
        plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)
    except:
        raise Exception("decryption failed")

    return plain.encode("utf8")
Exemple #9
0
def decrypt_message(K, iv, ciphertext, tag):
    """ Decrypt a cipher text under a key K 

        In case the decryption fails, throw an exception.
    """
    ## YOUR CODE HERE

    # we declare the aes cipher
    aes = Cipher("aes-128-gcm")
    # we decode the ciphertext
    plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)
    # we return the unencrypted message
    return plain.encode("utf8")
Exemple #10
0
def decrypt_message(K, iv, ciphertext, tag):
    """ Decrypt a cipher text under a key K

        In case the decryption fails, throw an exception.
    """
    ## YOUR CODE HERE tests
    aes = Cipher("aes-128-gcm")
    try:
        m = aes.quick_gcm_dec(K, iv, ciphertext,tag)
        m = m.decode("utf8")
    except:
        raise Exception('decryption failed')

    return m
Exemple #11
0
def decrypt_message(K, iv, ciphertext, tag, key_length=128):
    """ Decrypt a cipher text under a key K 

        In case the decryption fails, throw an exception.
    """

    if key_length not in [128, 192, 256]:
        raise Exception("Invalid key length")

    aes = Cipher("aes-%s-gcm" % str(key_length))

    plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)

    return plain.encode("utf8")
Exemple #12
0
def decrypt_message(K, iv, ciphertext, tag):
    """ Decrypt a cipher text under a key K 

        In case the decryption fails, throw an exception.
    """
    plain = ''
    aes = Cipher("aes-128-gcm")
    try:    
        plain = aes.quick_gcm_dec(K, iv, ciphertext,tag)
        plain = plain.decode("utf8")
    except:
        raise Exception('decryption failed')

    return plain
Exemple #13
0
def dh_decrypt(priv, iv, ciphertext, tag, pub_A, aliceVer = None):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided. Optionally verify 
    the message came from Alice using her verification key."""

    # Calculate 
    shared_K, _ = pub_A.pt_mul(priv).get_affine()
    shared_K = sha256(shared_K.repr()).hexdigest()
    shared_K = shared_K[:32]

    aes = Cipher("aes-256-gcm")
    try:
        plaintext = aes.quick_gcm_dec(shared_K, iv, ciphertext, tag)
    except:
        raise Exception("decryption failed")

    return plaintext.decode("utf8")
Exemple #14
0
def dh_decrypt(priv_receiver, ciphertext, iv, tag, pub_sender, signature = None):
    """ Decrypt a received message encrypted using your public key,
    of which the private key is provided. Optionally verify
    the message came from Alice using her verification key."""

    shared_key = dh_get_shared_key(pub_sender, priv_receiver)

    aes = Cipher("aes-256-gcm")
    plaintext = aes.quick_gcm_dec(shared_key, iv, ciphertext, tag)

    message = plaintext.decode("utf8")

    G = EcGroup()
    if signature:
        verify = ecdsa_verify(EcGroup(), pub_sender, message, signature)
        if not verify:
            raise Exception("Could not verify message")

    return message
def dh_decrypt(priv, ciphertext):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided"""
    Group1,private, public = dh_get_key()#generate new DH pair for Bob
    iv=ciphertext[0]
    cipher=ciphertext[1]
    tag=ciphertext[2]
    pubA=ciphertext[3]
    
    #Bob derives shared secret key by multiplying his public key with Alice's private key
    shared2 = pubA.pt_mul(priv)#qA * dB
    print "key from dec is", shared2

    hashedKey=sha256(shared2.export()).digest()
    
    aes = Cipher("aes-128-gcm")
    plain = aes.quick_gcm_dec(hashedKey[:16], iv, cipher, tag)#where to get IV and tag from ???
    
    return plain.encode("utf8")
Exemple #16
0
def dh_decrypt(priv, ciphertext):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided"""
    Group1, private, public = dh_get_key()  #generate new DH pair for Bob
    iv = ciphertext[0]
    cipher = ciphertext[1]
    tag = ciphertext[2]
    pubA = ciphertext[3]

    #Bob derives shared secret key by multiplying his public key with Alice's private key
    shared2 = pubA.pt_mul(priv)  #qA * dB
    print "key from dec is", shared2

    hashedKey = sha256(shared2.export()).digest()

    aes = Cipher("aes-128-gcm")
    plain = aes.quick_gcm_dec(hashedKey[:16], iv, cipher,
                              tag)  #where to get IV and tag from ???

    return plain.encode("utf8")
Exemple #17
0
def dh_decrypt(priv, ciphertext, aliceVer=None):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided. Optionally verify 
    the message came from Alice using her verification key."""

    ## YOUR CODE HERE
    # The ciphertext is 4 things combined
    alice_pub, iv, encrypted_text, tag = ciphertext
    bob_priv = priv

    assert isinstance(bob_priv, Bn)

    # derive the shared key the same way as before
    shared_key_point = bob_priv * alice_pub  # Now bob_priv is a scalar
    shared_key_point_binary = shared_key_point.export()
    shared_key = sha256(shared_key_point_binary).digest()

    aes = Cipher("aes-256-gcm")
    plain = aes.quick_gcm_dec(shared_key, iv, encrypted_text, tag)

    return plain.encode("utf8")
Exemple #18
0
def dh_decrypt(priv, ciphertext, aliceVer = None):
    """ Decrypt a received message encrypted using your public key, 
    of which the private key is provided. Optionally verify 
    the message came from Alice using her verification key."""
    
    ## YOUR CODE HERE
    pub = ciphertext[0]
    iv = ciphertext[1]
    tag = ciphertext[3]

    # We want d_B Q_A = d_A d_B G, to use as shared key
    secret = pub.pt_mul(priv)
    # x, y = secret.get_affine()
    # shared = x.binary()[:24]
    shared = sha256(secret.export()).digest()

    # Perform decryption with shared key
    aes = Cipher("aes-256-gcm")
    
    try:
 	plain = aes.quick_gcm_dec(shared, iv, ciphertext[2], tag)
    except:
	raise RuntimeError("decryption failed")
    return plain.encode("utf8")