簡體   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