Esempio n. 1
0
def recv_filelist():
    clientServer.settimeout(2)
    try:
        # 接收key
        key_encrypted = int(clientServer.recv(BUFFSIZE).decode())
        iv_encrypted = int(clientServer.recv(BUFFSIZE).decode())
        fileslist_encrypted = clientServer.recv(BUFFSIZE).decode()
        fileslist_digest = clientServer.recv(32).decode()
        fileslist_decrypted = transfer_decrypt(fileslist_encrypted,
                                               key_encrypted, iv_encrypted)
        print(
            "\n*********************************************************************************************************"
        )
        print("The list of files:")
        if fileslist_digest != md5(fileslist_decrypted):
            print("[Error]: The package you received may be broken\n")
        else:
            fileslist = fileslist_decrypted.split("*")
            for file in fileslist:
                print(file)
            print(
                "*********************************************************************************************************"
            )
    except ConnectionResetError:
        print("[Error]: Address is wrong.")
    except TimeoutError:
        print("[Error]: Timeout.")
    except:
        print("[Error]: Something is wrong.")
    return 0
Esempio n. 2
0
def file_encrypt(filepath):
    """
    对文件进行DES-CBC加密,并用RSA加密key和iv
    :param filepath: 原文件路径
    :return: 加密文件路径, 原文件md5值, 加密后的key, 加密后的iv
    """
    # 随机生成key和iv
    key, iv = get_des_param()
    # 打开原文件
    origin_file = open(filepath, 'rb')
    origin_filedata = origin_file.read().decode('latin')
    # 计算原文件的消息摘要
    origin_digest = md5(origin_filedata)
    # 加密后文件的地址(统一加上.encrypted后缀)
    encrypted_filepath = filepath + ".encrypted"
    # 写入加密文件
    encrypted_file = open(encrypted_filepath, 'wb')
    encrypted_file.write(
        DES.des_cbc_encrypt(origin_filedata, key, iv).encode('latin'))
    # 关闭文件
    origin_file.close()
    encrypted_file.close()
    # 用client的公钥对key和iv进行加密
    key_encrypted = RSA.Encrypt(libnum.s2n(key), server_e, server_n)
    iv_encrypted = RSA.Encrypt(libnum.s2n(iv), server_e, server_n)
    return encrypted_filepath, origin_digest, key_encrypted, iv_encrypted
def send_filelist(path):
    fileslist = get_filelist(path)
    print(
        "*********************************************************************************************************"
    )
    print("List file:")
    for i in fileslist:
        print(i)
    fileslist_str = '*'.join(fileslist)
    # 进行md5消息摘要
    fileslist_digest = md5(fileslist_str)
    # 对发送的要发送的内容(文件列表)进行des-cbc加密
    key_encrypted, iv_encrypted, allfiles_encrypted = transfer_encrypt(
        fileslist_str)
    # 发送RSA加密后的key
    mainSocket.send(str(key_encrypted).encode())
    time.sleep(0.01)
    # 发送RSA加密后的iv
    mainSocket.send(str(iv_encrypted).encode())
    time.sleep(0.01)
    # 发送des-cbc加密后的内容
    mainSocket.send(allfiles_encrypted.encode())
    time.sleep(0.01)
    # 发送消息摘要
    mainSocket.send(fileslist_digest.encode())
    time.sleep(0.01)
    print("The list of optional files was successfully sent")
    print(
        "*********************************************************************************************************"
    )
    return 0
Esempio n. 4
0
def get_file_md5(filepath):
    """
    对文件内容进行md5产生消息摘要来确保文件传输过程中的完整性
    :param filepath: 文件路径
    :return: 文件摘要
    """
    with open(filepath, 'rb') as fs:
        digest = md5(fs.read().decode('latin'))
    return digest
def sign(m, d, n):
    """
    对消息m进行签名
    :param m: 消息m
    :param d: 签名用的私钥
    :param n: 签名用的公钥
    :return: 消息m的签名
    """
    d = int(d)
    n = int(n)
    # 先用md5产生消息的摘要
    digest = md5(m)
    # 转为10进制
    digest = libnum.s2n(digest)
    # 对摘要进行签名
    s = pow(digest, d, n)
    return hex(s)[2:]
def check(m, s, e, n):
    """
    对签名进行验证
    :param m: 消息
    :param s: 消息的的签名
    :param e: 公钥e
    :param n: 公钥n
    :return: 是否通过验证
    """
    s = int(s, 16)
    e = int(e)
    n = int(n)
    # 先用md5产生消息的摘要
    digest = md5(m)
    # 转为10进制
    digest = libnum.s2n(digest)
    # 用公钥对进行验证
    temp = pow(s, e, n)
    if digest == temp:
        return True
    else:
        return False