def handle_supervisor_purchase_message(supervisor_connection, keypair,
                                       customer_public_key,
                                       supervisor_public_key):
    customer_signature = NetworkUtil.receive_message(supervisor_connection)
    encrypted_purchase_message = NetworkUtil.receive_message(
        supervisor_connection)
    supervisor_signature = NetworkUtil.receive_message(supervisor_connection)

    purchase_message = CryptoUtil.decrypt(encrypted_purchase_message,
                                          keypair).decode()
    print(f"Purchase message is: {purchase_message}")

    CryptoUtil.verify(purchase_message.encode(), customer_public_key,
                      customer_signature)
    CryptoUtil.verify(purchase_message.encode(), supervisor_public_key,
                      supervisor_signature)
    return encrypted_purchase_message, supervisor_signature, purchase_message
def handle_customer_purchase_message_and_confirm(customer_connection, keypair,
                                                 customer_public_key):
    encrypted_purchase_message = NetworkUtil.receive_message(
        customer_connection)
    customer_signature = NetworkUtil.receive_message(customer_connection)

    purchase_message = CryptoUtil.decrypt(encrypted_purchase_message,
                                          keypair).decode()
    print(f"Purchase message is: {purchase_message}")

    CryptoUtil.verify(purchase_message.encode(), customer_public_key,
                      customer_signature)

    timestamp = purchase_message.split("|")[0]
    item = purchase_message.split("|")[1]
    print(f"Customer would like to purchase {item} at {timestamp}.")

    confirmation = input(f"Confirm the above purchase? (y/n) ")
    return confirmation, customer_signature, purchase_message, encrypted_purchase_message
示例#3
0
def main(args):

    if len(args) != 2 or args[1] not in ['decrypt', 'encrypt', 'enc', 'dec']:
        usage()

    op = args[1]

    if op in ['enc', 'encrypt']:
        key = getpass.getpass('Please enter the encryption key: ')
        plaintext = getpass.getpass(
            'Please enter the plaintext you wish to encrypt: ')
        try:
            encrypted_value = CryptoUtil.encrypt(plaintext, key)
            print "Encrypted Value: " + encrypted_value
        except KeyLengthError, ex:
            print ex

    if op in ['dec', 'decrypt']:
        key = getpass.getpass('Please enter the encryption key: ')
        encrypted_value = getpass.getpass(
            'Please enter the encrypted string you wish to decrypt: ')
        try:
            decrypted_value = CryptoUtil.decrypt(encrypted_value, key)
            print "Plaintext Value: " + decrypted_value
        except KeyLengthError, ex:
            print ex


if __name__ == "__main__":
    main(sys.argv)
示例#4
0
    print
    exit()

def main(args):

    if len(args) != 2 or args[1] not in ['decrypt','encrypt','enc','dec']:
        usage()

    op = args[1]

    if op in ['enc', 'encrypt']:
        key = getpass.getpass('Please enter the encryption key: ')
        plaintext = getpass.getpass('Please enter the plaintext you wish to encrypt: ')
        try:
            encrypted_value = CryptoUtil.encrypt(plaintext, key)
            print "Encrypted Value: " + encrypted_value
        except KeyLengthError, ex:
            print ex

    if op in ['dec', 'decrypt']:
        key = getpass.getpass('Please enter the encryption key: ')
        encrypted_value = getpass.getpass('Please enter the encrypted string you wish to decrypt: ')
        try:
            decrypted_value = CryptoUtil.decrypt(encrypted_value, key)
            print "Plaintext Value: " + decrypted_value
        except KeyLengthError, ex:
            print ex

if __name__ == "__main__":
    main(sys.argv)
    return encrypted_purchase_message, signature, confirmation


if __name__ == "__main__":
    # Connect to supervisor
    supervisor_connection = ClientNetworkUtil.connect_to_supervisor()
    # Generate our key pair and get the supervisor's public key
    keypair, supervisor_public_key = ClientNetworkUtil.exchange_public_keys(
        supervisor_connection)

    while True:
        # Ask the user for an item to purchase and get the purchase message
        purchase_message = ask_user_for_purchase_message()
        print(f"Purchase message is: {purchase_message}")

        # Send purchase to supervisor
        (encrypted_purchase_message, signature,
         confirmation) = send_purchase_to_supervisor(supervisor_connection,
                                                     purchase_message, keypair,
                                                     supervisor_public_key)

        confirmation_msg = CryptoUtil.decrypt(confirmation, keypair).decode()
        print(confirmation_msg)

        # Show all the received messages (encrypted)
        presentation = input("Show the all receiving messages? (y/n)")
        if (presentation == 'y'):
            print(f"\nSupervisor's public key: {supervisor_public_key}", )
            print(f"\nEncrypted order Message: {encrypted_purchase_message}")
            print(f"\nConfirmation message from supervisor: {confirmation}")
示例#6
0
import CryptoUtil
import sys
from CryptoUtil import KeyLengthError

__author__ = 'bryce'

def main(args):
    if len(args) != 4 or args[1] not in ['dec', 'enc']:
        print 'Usage: crypt.py enc <plaintext> <passphrase>'
        print '       crypt.py dec <encrypted> <passphrase>'
        return

    op = args[1]
    key = args[3]

    if op == 'enc':
        plain = args[2]
        try:
            print CryptoUtil.encrypt(plain, key)
        except KeyLengthError, ex:
            print ex
    elif op == 'dec':
        encrypted = args[2]
        try:
            print CryptoUtil.decrypt(encrypted, key)
        except KeyLengthError, ex:
            print ex

if __name__ == "__main__":
    main(sys.argv)