Exemple #1
0
 def decrypt_aes_gcm(self, msg, secret):
     aes = Cipher.aes_128_gcm()
     g_x, iv, ciphertext, tag = msg
     d_key = self.G.expon(g_x, secret).export()
     d_key = sha256(d_key).digest()[:16]
     msg = aes.quick_gcm_dec(d_key, iv, ciphertext, tag)
     return msg
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.
    """
    ## YOUR CODE HERE
    aes = Cipher.aes_128_gcm()
    plain = aes.quick_gcm_dec(K, iv, ciphertext, tag)

    return plain.encode("utf8")
Exemple #3
0
 def encrypt_aes_gcm(self, msg, public_key, session):
     aes = Cipher.aes_128_gcm()
     iv = urandom(16)
     _, aes_private_key = self.keyGenerate(session)
     x = aes_private_key[1]
     g_x = aes_private_key[2]
     e_key = self.G.expon(public_key, x).export()
     e_key = sha256(e_key).digest()[:16]
     ciphertext, tag = aes.quick_gcm_enc(e_key, iv, msg)
     return (g_x, iv, ciphertext, tag)
Exemple #4
0
def encrypt_message(K, message):
    """ Encrypt a message under a key K """

    plaintext = message.encode("utf8")

    ## YOUR CODE HERE
    aes = Cipher.aes_128_gcm()
    iv = urandom(16)
    ciphertext, tag = aes.quick_gcm_enc(K, iv, plaintext)

    return (iv, ciphertext, tag)
Exemple #5
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()  #Intialise Advanced encryption standart
    plain = aes.quick_gcm_dec(
        K, iv, ciphertext, tag
    )  # produce the plaintext using decryption function given using the arguments of the function

    return plain.encode("utf8")
Exemple #6
0
def encrypt_message(K, message):
    """ Encrypt a message under a key K """

    plaintext = message.encode("utf8")
    aes = Cipher.aes_128_gcm()  ## intialise aes gcm cipher
    iv = urandom(16)  ## Generate random IV of length 16

    ciphertext, tag = aes.quick_gcm_enc(
        K, iv, plaintext
    )  ## produce cipher and tag using the encryption function provided
    ## YOUR CODE HERE

    return (iv, ciphertext, tag)
Exemple #7
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()
    decrypter = aes.dec(K, iv)
    plain = decrypter.update(ciphertext)
    decrypter.set_tag(tag)
    decrypter.finalize()

    return plain.encode("utf8")
Exemple #8
0
def encrypt_message(K, message):
    """ Encrypt a message under a key K """

    plaintext = message.encode("utf8")

    ## YOUR CODE HERE
    key_length = len(K)
    iv = urandom(key_length)

    aes = Cipher.aes_128_gcm()
    encrypter = aes.enc(K, iv)
    ciphertext = encrypter.update(plaintext)
    encrypter.finalize()
    tag = encrypter.get_tag(key_length)

    return (iv, ciphertext, tag)
def test_fails():
    G1, private1, public1 = dh_get_key()
    msg = u"Test" * 1000
    ciphertext=dh_encrypt(public1,msg)
    iv=ciphertext[0]#get IV from dh_encrypt()
    tag=ciphertext[2]#tag
    pubA=ciphertext[3]#Alice's public key
    
    #derive shared secret by doing qA * dB 
    shared=pubA.pt_mul(private1)
    hashedKey=sha256(shared.export()).digest()
    print "shared in fail is", shared
    
    mess=msg.encode("utf8")
    aes = Cipher.aes_128_gcm()          # Initialize AES cipher
    enc = aes.enc(hashedKey[:16], iv)           # Get an encryption CipherOperation
    ciphertext2 = enc.update(mess)  # Include some plaintext
    nothing = enc.finalize()            # Finalize
    tag2 = enc.get_tag(16)               # Get the AES-GCM tag
    
    if tag==tag2:#only attempt to decrypt if tag is valid !
        assert dh_decrypt(private1,ciphertext)==mess
    else: 
        assert False 
Exemple #10
0
def test_fails():
    G1, private1, public1 = dh_get_key()
    msg = u"Test" * 1000
    ciphertext = dh_encrypt(public1, msg)
    iv = ciphertext[0]  #get IV from dh_encrypt()
    tag = ciphertext[2]  #tag
    pubA = ciphertext[3]  #Alice's public key

    #derive shared secret by doing qA * dB
    shared = pubA.pt_mul(private1)
    hashedKey = sha256(shared.export()).digest()
    print "shared in fail is", shared

    mess = msg.encode("utf8")
    aes = Cipher.aes_128_gcm()  # Initialize AES cipher
    enc = aes.enc(hashedKey[:16], iv)  # Get an encryption CipherOperation
    ciphertext2 = enc.update(mess)  # Include some plaintext
    nothing = enc.finalize()  # Finalize
    tag2 = enc.get_tag(16)  # Get the AES-GCM tag

    if tag == tag2:  #only attempt to decrypt if tag is valid !
        assert dh_decrypt(private1, ciphertext) == mess
    else:
        assert False