如何从记事本中读取加密数据并解密



我正试图从文件中读取文本,并使用Python中的Fernet加密库对其进行解密。所以我正在运行一个For循环,它打印所有的文本,同时我尝试使循环解密。

from cryptography.fernet import Fernet
from inspect import currentframe
key = "kdQGjocgILOLXj6k_mkkOJOxHxXJji7kdRUTtrGZBTo="
f = Fernet(key)

def get_linenumber():
cf = currentframe()
return cf.f_back.f_lineno

def Main():
choice = input("1. Write Raw Data To File For Encryptionn2. Read Data From File For DecryptionnnEnter Option Here: ")
if choice == "1":
print("Init Main Successfull On Line ", get_linenumber())
File = open(r"D:Visual Studio ProjectsPython ProjSecure Note Storage ProjectFile.txt",mode="a")

FileToEncrypt = bytes(input("Input data to encrypt, Note it will be stored as a string, then heavily encrypted with SHA-256.n"),'utf-8')
print("nSuccesfully printed File To Encrypt data on line ",get_linenumber,"nData From FileToEncrypt Var: ",FileToEncrypt)

FileEncrypted = f.encrypt(FileToEncrypt)
print("nnHere is the final product: ",FileEncrypted)
EncryptionDescription = input("What Is The Data Entered. (Explain For Future Reference!!!)nnEnter Here: ")
File.write(f"{EncryptionDescription}n" + str(FileEncrypted))

File.close()

elif choice == "2":
print("nnYou Have Chosen Decryption Method!n")
File = open(r"D:Visual Studio ProjectsPython ProjSecure Note Storage ProjectFile.txt",mode="r")

for line in File:
name = line.strip()
num = File.readline()
num = Fernet.decrypt(f,num)
print (num)






else:
print("Sorry, We Do Not Recognise What You Have Entered. Please look at the options and think...")
exit(0)

###Debug Shit
#print(Fernet.generate_key()) # so i can find one key and keep it static:) 
# print(File.read())
#  print("File Print Successfull On Line ", get_linenumber())
# File.write("nRawrar")



if __name__ == "__main__":
Main()

我试着从文件中打印数据,当它起作用时。我试图将Num变量转换为加密文本的解密版本。当这不起作用时,我把参数搞得有点乱。但我不知道在哪里,也不知道自己在做什么。

这种情况下的第一个问题似乎是您正确地将FileToEncrypt转换为字节进行加密,但在保存时,您只需使用str()将其强制转换为字符串。您要做的是使用.decode()
此外,在将数据写入文件时,您可能还想添加另一个换行符。否则,如果多次附加到同一个文件,您将面临问题File.write(f"{EncryptionDescription}n" + FileEncrypted + "n")您还需要确保在加载此数据时,使用bytes(str, 'utf-8')再次将其转换为字节。

现在您的数据已正确保存到File.txt文件中,您现在可以对数据进行解码了。但是,根据文档,由于您没有在解密函数调用中传递时间戳,您仍然会面临错误。从本质上讲,我找到了两种方法来做到这一点。一种简单的方法是调用具有非常高数字的函数作为ttl参数,如下所示:print(Fernet.decrypt(f,bytes(num, "utf-8"), 1000000))
正确的方法是根据文档使用函数extract_timestamp()。我像encryptionTimeStamp = Fernet.extract_timestamp(f, bytes(num, 'utf-8'))一样成功地实现了它
这样做将获得加密时的unix时间戳。ttl参数是加密后的秒数;当前unix时间戳-加密时的unix时间戳";并将其作为CCD_ 10参数传递。

from cryptography.fernet import Fernet
from inspect import currentframe
import time
key = "kdQGjocgILOLXj6k_mkkOJOxHxXJji7kdRUTtrGZBTo="
f = Fernet(key)

def get_linenumber():
cf = currentframe()
return cf.f_back.f_lineno

def Main():
choice = input("1. Write Raw Data To File For Encryptionn2. Read Data From File For DecryptionnnEnter Option Here: ")
if choice == "1":
print("Init Main Successfull On Line ", get_linenumber())
File = open(r"File.txt",mode="a")

FileToEncrypt = bytes(input("Input data to encrypt, Note it will be stored as a string, then heavily encrypted with SHA-256.n"),'utf-8')
print("nSuccesfully printed File To Encrypt data on line ",get_linenumber,"nData From FileToEncrypt Var: ",FileToEncrypt)

FileEncrypted = f.encrypt(FileToEncrypt)
FileEncrypted = FileEncrypted.decode()
print("nnHere is the final product: ",FileEncrypted)
EncryptionDescription = input("What Is The Data Entered. (Explain For Future Reference!!!)nnEnter Here: ")
File.write(f"{EncryptionDescription}n" + FileEncrypted + "n")

File.close()

elif choice == "2":
print("nnYou Have Chosen Decryption Method!n")
File = open(r"File.txt",mode="r")

for line in File:
name = line.strip()
num = File.readline()
numBytes = bytes(num, 'utf-8')
encryptionTimeStamp = Fernet.extract_timestamp(f, numBytes)
currentTime = int(time.time())
timeElapsed = currentTime - encryptionTimeStamp
print( Fernet.decrypt(f,numBytes, timeElapsed))







else:
print("Sorry, We Do Not Recognise What You Have Entered. Please look at the options and think...")
exit(0)

###Debug Shit
#print(Fernet.generate_key()) # so i can find one key and keep it static:) 
# print(File.read())
#  print("File Print Successfull On Line ", get_linenumber())
# File.write("nRawrar")



if __name__ == "__main__":
Main()

最新更新