Пример #1
0
def encryptor(entire_msg, key):
    K1, K2 = toy_des.key_getter(key)
    # see, I'm going to operate under the assumption that entire_msg is already in bits
    # so I don't need to do text_to_bits
    # turning bits to bits because it'll convert easier lollllllll
    bits_of_bits = toy_des.text_to_bits(entire_msg)
    block_list = []
    block = ""
    for i in range(0, len(bits_of_bits), 8):
        for j in range(i, i + 8):
            if j == len(bits_of_bits):
                break
            block += bits_of_bits[j]
        block_list.append(block)
        block = ""

    encrypt_list = []
    encrypt_string = ""
    for i in range(len(block_list)):
        encrypt_list.append(toy_des.encryptor(block_list[i], K1, K2))

    for i in range(len(encrypt_list)):
        encrypt_string += toy_des.text_from_bits(encrypt_list[i])

    return encrypt_string
Пример #2
0
def decryptor(entire_msg, key):
    K1, K2 = toy_des.key_getter(key)
    decrypt_list = []
    encrypt_bits = toy_des.text_to_bits(entire_msg)

    block = ""
    for i in range(0, len(encrypt_bits), 8):
        for j in range(i, i + 8):
            block += encrypt_bits[j]
        decrypt_list.append(block)
        block = ""

    decrypt_bits = []
    for i in range(len(decrypt_list)):
        decrypt_bits.append(toy_des.decryptor(decrypt_list[i], K1, K2))
    block = ""
    for i in range(len(decrypt_bits)):
        block += toy_des.text_from_bits(decrypt_bits[i])

    return block
Пример #3
0
    # encrypt the file to send upon a connection

    # code from toy_des.py
    secret_bin = toy_des.text_to_bits(entire_message)
    block_list = []
    block = ""
    # print(len(secret_bin))
    for i in range(0, len(secret_bin), 8):
        for j in range(i, i + 8):
            if j == len(secret_bin):
                break
            block += secret_bin[j]
        block_list.append(block)
        block = ""

    Key1, Key2 = toy_des.key_getter(key)

    encrypt_list = []
    encrypt_string = ""
    for i in range(len(block_list)):
        encrypt_list.append(toy_des.encryptor(block_list[i], Key1, Key2))

    for i in range(len(encrypt_list)):
        encrypt_string += toy_des.text_from_bits(encrypt_list[i])

    # want to send the encrypted string
    # listen for incoming connections
    sock.listen(1)
    while True:
        try:  # this will just keep sending the encrypted message
            connection, cli_addr = sock.accept()
Пример #4
0
import toy_des
import sys

if __name__ == "__main__":
    # users will connect to the server,
    # get an encrypted message, and decrypts it

    if len(sys.argv) != 3 or len(sys.argv[2]) != 10:
        print("usage: client.py \"<insert filename here>\" <10-bit key here>")

    # the filename is going to be the ouput file

    file = sys.argv[1]
    key = sys.argv[2]

    K1, K2 = toy_des.key_getter(key)

    f = open(file, "w+")

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    serv_addr = ('localhost', 9095)
    sock.connect(serv_addr)

    while True:
        encrypted = sock.recv(1024).decode()

        print("Received the message:", encrypted)

        # this is pretty much the same code from toy_des.
        # it's just the decryption process