首页 > 解决方案 > 如何在一段时间后加密文件,然后在 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 天内无法访问它。

标签: pythonlinux

解决方案


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

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

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


推荐阅读