简体   繁体   English

如何在一段时间后加密文件,然后在 python 中自动解密?

[英]How to encrypt a file after a certain period of time and then also decrypt it automatically later in python?

So, I am trying to make a personal Tasks script in Python, which will access the difficulty of the project and will create a file, which will be the project file.所以,我正在尝试在 Python 中制作一个个人任务脚本,它将访问项目的难度并创建一个文件,该文件将是项目文件。

So, supposing I have a folder at /home/xyz/Desktop/Projects and it generates a file with name tempPrime.py and I want to make the program inaccessible after say, 10 days and unlock it back after a given time period.所以,假设我在 /home/xyz/Desktop/Projects 有一个文件夹,它会生成一个名为 tempPrime.py 的文件,我想让程序在 10 天后无法访问,并在给定时间段后将其解锁。

How do I do that using python, and if it isn't possible is there any command in Linux that will automatically lock the file and unlock it after say 20 days after the locking?我如何使用 python 来做到这一点,如果不可能,Linux 中是否有任何命令会在锁定后 20 天后自动锁定文件并解锁?

An Example: Suppose I want to make a python script that prints prime numbers, and I know nothing about prime numbers, so before starting the project, I run this script, which will ask me details which we decide the time limit.一个例子:假设我想制作一个打印素数的 python 脚本,而我对素数一无所知,所以在开始项目之前,我运行了这个脚本,它会询问我们决定时间限制的详细信息。 Let suppose the time limit be 4 days, so what I want is to create Prime.py somewhere on my computer which once opened will encrypt itself after 4 days until I changed it's status as done, and if I wasn't able to make it then the file would lock itself and would unlock only after a given time say 20 days after locking.假设时间限制是 4 天,所以我想要的是在我的计算机上的某个地方创建Prime.py ,一旦打开它就会在 4 天后自行加密,直到我将它的状态更改为完成,如果我无法做到然后该文件将自行锁定,并且仅在锁定后的给定时间(例如 20 天)后解锁。

from subprocess import call,os
#Task Manager For Me
def tellTimeLimit():
    uDif=int(input("How difficult is the project?\n Define it on an universal scale of 0-10: "))
    pDif=int(input("How well-versed are you with the topic?\nDefine it on an personal scale of 0-10: "))
    res=True if input("Do you have enough resources available for it? ")=="Yes" else False
    calcTime= uDif*0.55 #For each increase in difficulty, the time alloted will increase by 12 hours i.e. 0.5 days
    calcTime+=int(11-pDif)*0.35 #For each decrease in difficulty the time alloted will increase by 6 hours i.e. 0.25 days
    if res:
        calcTime*=0.9+11-int(input("Rank the resources on their usefulness on a scale of 0-10: "))*0.1 #For each decrease in difficulty the time alloted will increase by 0.1 which then will be added to the calcTime multiplied by 0.8
    else:
        print(calcTime)
        calcTime*=2.25
        print(calcTime)
    return calcTime
def takeData():
    fileDir="/home/xyz/Desktop/Programs/Python/pTasks/"
    pName=input("Enter the name of the project: ")
    if not os.path.exists(fileDir):
        os.mkdir(fileDir)
    fileLoc=os.path.join(fileDir,pName)
    file = open(fileLoc,'w')
    file.write("#This project can only be accessed by you for "+str((tellTimeLimit()))+" Days\n So Hurry up!!!")
takeData()

Now after this, I have a fixed amount of time to complete else I would be unable to access it for twenty days.现在,在此之后,我有固定的时间来完成,否则我将在 20 天内无法访问它。

You need admin privileges to make a file really inaccesible to a user.您需要管理员权限才能使用户真正无法访问文件。

Without the admin privileges you could deny access to a file with chmod , but the user can revert it (with chmod ).如果没有管理员权限,您可以使用chmod拒绝访问文件,但用户可以恢复它(使用chmod )。

As an alternative, your program could encrypt the file.作为替代方案,您的程序可以加密文件。 This make it difficult to read the file, but not impossible, because your program must retain the encryption key.这使得读取文件变得困难,但并非不可能,因为您的程序必须保留加密密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM