password):cipher=AES.new(password.encode(),AES.MODE_EAX)withopen(file_path,'rb')asfile:file_data=file.read()encrypted_data,tag=cipher.encrypt_and_digest(file_data)withopen(file_path+'.enc','wb')asencrypted_file:[encrypted_file.write(x)forxin(cipher.nonce,tag,encrypted_data)]defdecrypt...
AI检测代码解析 importosfromcryptography.fernetimportFernetdefgenerate_key():returnFernet.generate_key()defencrypt_file(file_path,key):withopen(file_path,'rb')asfile:original_data=file.read()fernet=Fernet(key)encrypted_data=fernet.encrypt(original_data)withopen(file_path,'wb')asfile:file.write(en...
encrypted_file = PdfFileWriter() original = PdfFileReader("PDF文件所在的路径") number_of_pages = original.numPages for i in range(number_of_pages): page = original.getPage(i) encrypted_file.addPage(page) password = "密码" encrypted_file.encrypt(password) with open("新生成的PDF的路径", "...
1.ECB模式加密,代码如下: from Crypto.Cipher import AES password = b'1234567812345678' text = b'abcdefghijklmnop' aes = AES.new(password, AES.MODE_ECB) en_text = aes.encrypt(text) print("密文:",en_text) den_text = aes.decrypt(en_text) print("明文:",den_text) password:密钥,b表示转换...
(public_key_file,"rb")askey_file:public_key=rsa.PublicKey.load_pkcs1(key_file.read())# 加密文件内容encrypted_content=rsa.encrypt(file_content,public_key)# 将加密后的内容写入文件withopen(file_path,"wb")asfile:file.write(encrypted_content)defdecrypt_file(file_path,private_key_file,password)...
encrypt(file.read(), recipients, output=output_file_path) if status.ok: print("File encrypted successfully.") else: print("Encryption failed:", status.status) # 解密文件 def decrypt_gpg_file(input_file_path, output_file_path): with open(input_file_path, "rb") as encrypted_file: data ...
) encrypted_data = cipher_suite.encrypt(data) with open(file_path + '.encrypted', 'wb') as file: file.write(encrypted_data) # 加密前的明文文件路径 file_path = 'path/to/your/file.txt' # 密码,可以是任意字符串,但必须保密 password = b'your_password' encrypt_file(file_path, password)...
writer.encrypt(password) # 将加密后的 PDF 写入新的文件 with open(output_pdf_path, 'wb') as output_file: writer.write(output_file) print(f"PDF 文件已加密并保存为 {output_pdf_path}") 3、运行脚本: 在命令行中运行脚本: python set_pdf_password.py ...
importPyPDF2defencrypt_pdf(input_pdf, output_pdf, password):#打开PDF文件with open(input_pdf,'rb') as file:#创建PDF写入对象pdf_writer =PyPDF2.PdfWriter()#创建PDF阅读器对象pdf_reader =PyPDF2.PdfReader(file)#遍历每一页并添加到写入对象中forpage_numinrange(len(pdf_reader.pages)): ...
defsave_credentials(website_name, password): encrypted_password = encrypt_password(password) withopen('credentials.csv','a', newline='')ascsvfile: writer = csv.writer(csvfile) writer.writerow([website_name, encrypted_password.decode()])# Ensur...