Exemple #1
0
def conk_p():
    M_1 = 'execute'
    M_2 = 'terminated'
    key = getkey()  #获得对称密钥
    c_1 = encrypt(M_1, key)  #对不同的消息进行加密
    c_2 = encrypt(M_2, key)
    # 存储此次运行的不同集合的消息密文,模拟用户提前通过信道接收
    f = open(r"cipher.txt", "w", encoding='utf-8')
    f.write(c_1 + '\n' + c_2)
    f.close()
    hc_1 = hashlib.sha256(c_1.encode()).hexdigest()  # h(c_1)
    hc_2 = hashlib.sha256(c_2.encode()).hexdigest()  # h(c_2)
    hk = hashlib.sha256(key.encode()).hexdigest()

    G_1 = ['Bob']  # 集合G1
    G_2 = ['Cindy']  # 集合G2
    l_m_1 = {
        'O': 'Alice',
        'G_i': G_1,
        'h(c_i)': hc_1,
        'h(k)': hk,
    }
    l_m_2 = {
        'O': 'Alice',
        'G_i': G_2,
        'h(c_i)': hc_2,
        'h(k)': hk,
    }
    print(l_m_1)
    print(l_m_2)
    # print(type(l_m_1)) 字典格式,不能有集合,不然无法json序列化
    # print(type(l_m_2))
    l_1 = json.dumps(l_m_1, sort_keys=True).encode('utf-8')
    l1 = hashlib.sha256(l_1).hexdigest()
    l_2 = json.dumps(l_m_2, sort_keys=True).encode('utf-8')
    l2 = hashlib.sha256(l_2).hexdigest()
    L_m = [l1, l2]
    #L_m_h = hashlib.sha256(str(L_m).encode('utf-8')).hexdigest()
    group_result = group_encryption()
    #构造密钥公布证据
    con_kp = {
        'identifying': "f_con",
        'O': "Alice",
        'R_m': "{Bob,Cindy}",
        'L\'': L_m,
        'h(k)': hk,
        # 't': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
        't': time.time(),
        'E_R\'(k)': group_result
    }
    return con_kp
Exemple #2
0
def main():

    #usage check
    if (len(sys.argv) != 4) or (not sys.argv[3] in ['-e', '-d']):
        print(
            "Usage: encryptFile.py inFileName outFileName [options]\n\nOptions:\n-e\tencrypt\n-d\tdecrypt"
        )
        return

    #read file contents, erroring out if file does not exist
    try:
        with open(sys.argv[1], 'r') as f:
            data = f.read()
    except:
        print("Error: input file '{0}' not found".format(sys.argv[1]),
              file=sys.stderr)
        return

    #pass data into encryption/decryption method
    bitArray = tobits(data) if sys.argv[3] == '-e' else [
        int(i) for i in list(data)
    ]
    result = encrypt(bitArray,
                     defaultKey) if sys.argv[3] == '-e' else frombits(
                         decrypt(bitArray, defaultKey))

    #write result to output file
    with open(sys.argv[2], 'w') as f:
        for i in result:
            f.write(str(i))
Exemple #3
0
def breakDES(tfirst, tlast):
    global P
    global C
    start = datetime.datetime.now()
    for k in range(tfirst, tlast):

        # find encryptedP
        bin_k = to_binary(k)
        encryptedP = encrypt(P, bin_k)

        if encryptedP == C:
            end = datetime.datetime.now()
            print("*^* " + str(bin_k) + " *^*")

            pfile = open("found_key.txt", 'w')
            pfile.write("Thread Victorious ... " + str(tfirst) + " - " +
                        str(tlast))
            pfile.write("\n")
            pfile.write("thread started  : " + str(start))
            pfile.write("\n")
            pfile.write("thread finished : " + str(end))
            pfile.write("\n")
            pfile.write("Key Found: " + str(bin_k))
            pfile.close()

            # stop all threads & machines
            break

    print("Thread Complete Over: " + str(tfirst) + " to " + str(tlast) +
          " ** ")
Exemple #4
0
def break_des(tfirst, tlast):
    """
    Try keys within the specified range. If a key works,
    print it out to a file with the length of time it took.
    Meant to be used as the target of threads.
    :param tfirst: starting key
    :param tlast: ending key
    """
    global P
    global C
    global terminate
    start = datetime.datetime.now()

    log_name = str(tfirst) + "_log.txt"

    logfile = open(log_name, 'a')
    logfile.write("Thread " + str(tfirst) + " - " + str(tlast))
    logfile.write("\n")
    logfile.close()

    # Check all keys k in given range for the thread
    for k in range(tfirst, tlast):

        # Find encrypted_p
        bin_k = to_binary(k)
        encrypted_p = encrypt(P, bin_k)

        # If encryption with key k equals ciphertext, print to file and terminate
        if encrypted_p == C:
            end = datetime.datetime.now()
            print("*^* " + str(bin_k) + " *^*")

            pfile = open("found_key.txt", 'w')
            pfile.write("Thread Victorious ... " + str(tfirst) + " - " +
                        str(tlast))
            pfile.write("\n")
            pfile.write("thread started  : " + str(start))
            pfile.write("\n")
            pfile.write("thread finished : " + str(end))
            pfile.write("\n")
            pfile.write("Key Found: " + str(bin_k))
            pfile.close()

            terminate = True
            Client('150.243.146.253',
                   10001).main()  # Change to dedserver IP address and port
        # Trigger if another thread finds the key
        elif terminate:
            print("Thread terminated before Key was found")
            break
        # about every hour, add a checkpoint in case of a crash
        elif (k % 1000000 == 0):
            logfile = open(log_name, 'a')
            logfile.write("Checkpoint " + str(k))
            logfile.write("\n")
            logfile.close()

    print("Thread Complete Over: " + str(tfirst) + " to " + str(tlast) +
          " ** ")
Exemple #5
0
	def check(self, key, plaintext, cipher):
		self.assertEqual(cipher, encrypt(plaintext, key))
		self.assertEqual(plaintext, decrypt(cipher, key))
Exemple #6
0
import sys,socket
from DES import encrypt, tobits, defaultKey

#boilerplate TCP code from https://wiki.python.org/moin/TcpCommunication
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 64000

fileName = input("Enter name of file to send to server: ")
#read file contents, erroring out if file does not exist
try:
    with open(fileName, 'r') as f:
        data = f.read()

except:
    print("Error: input file '{0}' not found".format(fileName),file=sys.stderr)
    sys.exit()
    
#establish socket connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

#encrypt data from file
bitArray = tobits(data)
result = encrypt(bitArray,defaultKey)
resultStr = ''.join(str(i) for i in result)
    
#send encrypted data to server
s.send(resultStr.encode())
s.close()