简体   繁体   中英

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.

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.

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?

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. 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.

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.

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 ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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