繁体   English   中英

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

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

所以,我正在尝试在 Python 中制作一个个人任务脚本,它将访问项目的难度并创建一个文件,该文件将是项目文件。

所以,假设我在 /home/xyz/Desktop/Projects 有一个文件夹,它会生成一个名为 tempPrime.py 的文件,我想让程序在 10 天后无法访问,并在给定时间段后将其解锁。

我如何使用 python 来做到这一点,如果不可能,Linux 中是否有任何命令会在锁定后 20 天后自动锁定文件并解锁?

一个例子:假设我想制作一个打印素数的 python 脚本,而我对素数一无所知,所以在开始项目之前,我运行了这个脚本,它会询问我们决定时间限制的详细信息。 假设时间限制是 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()

现在,在此之后,我有固定的时间来完成,否则我将在 20 天内无法访问它。

您需要管理员权限才能使用户真正无法访问文件。

如果没有管理员权限,您可以使用chmod拒绝访问文件,但用户可以恢复它(使用chmod )。

作为替代方案,您的程序可以加密文件。 这使得读取文件变得困难,但并非不可能,因为您的程序必须保留加密密钥。

暂无
暂无

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

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