示例#1
0
def main():
  #Welcome
  print("Welcome to CipherText!")
  sender = str(input("Who is sending the message? "))
  receiver = str(input("Who is receiving the message? "))

  #Key Generation to obtain derived key from X25519 + HKDF
  derived_key = key_gen.handshake(sender)

  #Message Encryption using derived_key as the key to AES GCM
  message = str(input("Please type the message you'd like to send: "))
  message = (bytes(message, encoding='utf8') if not 
  isinstance(message, bytes) else message)
  
  #This is just information associated to the user, authenticated with the key but not encrypted.
  associated_data = b"This is the user_id, unique to the user from the company server"
  # hashing does not work:
  # associated_data = hash(derived_key, associated_data)

  aesgcm_packet, aesgcm_nonce, ciphertext = aesgcm.encrypt(derived_key, message, associated_data)
  print(sender, "sent", ciphertext)
  
  #for demo purposes:
  #twilio_sms.send(ciphertext)
  #plaintext = twilio_sms.receive(ciphertext)
  #twilio_sms.send(plaintext)


  #check hash
  # associated_data = associated_data.finalize()
  plaintext = aesgcm.decrypt(aesgcm_packet, aesgcm_nonce, ciphertext, associated_data)
  print(receiver, "got", plaintext, "associated_data:", associated_data)
示例#2
0
def main():
  #Welcome
  print("Welcome to CipherText!")
  sender = str(input("Who is sending the message? "))
  receiver = str(input("Who is receiving the message? "))

  #Key Generation to obtain derived key from X25519 + HKDF
  derived_key = key_gen.handshake(sender)

  #Message Encryption using derived_key as the key to AES GCM
  message = str(input("Please type the message you'd like to send: "))
  message = (bytes(message, encoding='utf8') if not 
  isinstance(message, bytes) else message)
  
  #This is information associated to the user, authenticated with the key but not encrypted.
  #According to AESGCM, this associated_data must not be encrypted. 
  associated_data = b"This could be the user_id, unique to the user from the company server"

  #aesgcm.encrypt returns ciphertext bytes with the 16 byte tag appended
  aesgcm_packet, aesgcm_nonce, ciphertext = aesgcm.encrypt(derived_key, message, associated_data)
  print(sender, "sent", ciphertext)

  #aesgcm.decrypt takes the encrypted data with the 16 bit tag appended, and returns the original plaintext.
  plaintext = aesgcm.decrypt(aesgcm_packet, aesgcm_nonce, ciphertext, associated_data)
  print(receiver, "got", plaintext, "associated_data:", associated_data)
示例#3
0
def aes_decrypt(encrypted_txt):
    encoded_key = get_key_from_local_state()
    encrypted_key = base64.b64decode(encoded_key.encode())
    encrypted_key = encrypted_key[5:]
    key = dpapi_decrypt(encrypted_key)
    nonce = encrypted_txt[3:15]
    cipher = aesgcm.get_cipher(key)
    return aesgcm.decrypt(cipher, encrypted_txt[15:], nonce)
示例#4
0
def aes_decrypt(encrypted_txt):
    encoded_key = get_key_from_local_state()
    encrypted_key = base64.b64decode(encoded_key.encode())
    #remove prefix 'DPAPI'
    encrypted_key = encrypted_key[5:]
    key = dpapi_decrypt(encrypted_key)
    #get nonce. ignore prefix 'v10', length is 12 bytes.
    nonce = encrypted_txt[3:15]
    cipher = aesgcm.get_cipher(key)
    return aesgcm.decrypt(cipher, encrypted_txt[15:], nonce)