Exemplo n.º 1
0
def file_decrypt(encrypted_filepath, key_encrypted, iv_encrypted):
    """
    对文件进行解密
    :param encrypted_file_path: 加密文件的路径
    :param key_encrypted: 加密的密钥
    :param iv_encrypted: 加密的iv
    :return: 解密后文件路径, 解密后文件的md5
    """
    # 先用server的私钥对key和iv进行解密
    key = RSA.Decrypto(key_encrypted, client_d, client_n)
    iv = RSA.Decrypto(iv_encrypted, client_d, client_n)
    # 打开并读取加密文件
    encrypted_file = open(encrypted_filepath, 'rb')
    encrypted_filedata = encrypted_file.read().decode('latin')
    # 去掉.crypted后缀
    origin_filepath = ".".join(encrypted_filepath.split('.')[:-1])
    # 将解密后的内容写入新文件
    origin_file = open(origin_filepath, 'wb')
    origin_file.write(
        DES.des_cbc_decrypt(encrypted_filedata, libnum.n2s(key),
                            libnum.n2s(iv)).encode('latin'))
    # 关闭文件指针
    encrypted_file.close()
    origin_file.close()
    # 计算解密后文件的md5消息摘要
    origin_file_digest = get_file_md5(origin_filepath)
    return origin_filepath, origin_file_digest
Exemplo n.º 2
0
def transfer_decrypt(cipehr, key_encrypted, iv_encrypted):
    """
    对接收到的每一个分组进行解密
    :param cipehr: 收到的加密分组
    :param key_encrypted: 收到的加密过的key
    :param iv_encrypted: 收到的加密过的iv
    :return: 明文
    """
    # 先用client的私钥对key和iv进行解密
    key = RSA.Decrypto(key_encrypted, client_d, client_n)
    iv = RSA.Decrypto(iv_encrypted, client_d, client_n)
    # 用key和iv对收到的密文分组解密
    message = DES.des_cbc_decrypt(cipehr, libnum.n2s(key), libnum.n2s(iv), 'b')
    return message