Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的列表和x?_Python_Rsa_Pycryptodome - Fatal编程技术网

python中的列表和x?

python中的列表和x?,python,rsa,pycryptodome,Python,Rsa,Pycryptodome,此代码用于使用RSA加密随机输入。我知道这一行将把输出写入一个新文件,但我不能掌握在代码中使用x和list([])的原理 cipher_aes = AES.new(session_key, AES.MODE_EAX) ciphertext, tag = cipher_aes.encrypt_and_digest(data) [ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ] pri

此代码用于使用RSA加密随机输入。我知道这一行将把输出写入一个新文件,但我不能掌握在代码中使用x和list([])的原理

cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
print("Message encrypted \n")
file_out.close()
完整代码如下:

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP

key = RSA.generate(2048)
private_key = key.export_key()
file_out = open("private.pem", "wb")
file_out.write(private_key)
file_out.close()

public_key = key.publickey().export_key()
file_out = open("receiver.pem", "wb")
file_out.write(public_key)
file_out.close()

print("Keys Generated \n")

data = input("Insert message to encrypt: ").encode("utf-8")
file_out = open("encrypted_data.bin", "wb")

recipient_key = RSA.import_key(open("receiver.pem").read())
session_key = get_random_bytes(16)

# Encrypt session key (random) guna public key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
enc_session_key = cipher_rsa.encrypt(session_key)

# Encrypt data guna AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
print("Message encrypted \n")
file_out.close()

感谢您的帮助

该列表是不必要的。他们使用列表理解,但不将列表保存在任何地方。所以它相当于

for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext):
    file_out.write(x)
甚至不需要循环,您可以简单地连接所有变量:

file_out.write(enc_session_key + cipher_aes.nonce + tag + ciphertext)

这个清单是不必要的。他们使用列表理解,但不将列表保存在任何地方。所以它相当于

for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext):
    file_out.write(x)
甚至不需要循环,您可以简单地连接所有变量:

file_out.write(enc_session_key + cipher_aes.nonce + tag + ciphertext)

这是一个好主意。我认为这是一个很奇怪的用法,没有理由在这里使用列表。它被用作
for
循环的快捷方式。它是一个。我认为这是一个很奇怪的用法,没有理由在这里使用列表。它被用作
for
循环的快捷方式。谢谢您,先生!这真的很有用谢谢你,先生!这真的很有帮助