簡體   English   中英

如何使用python在linux中創建受密碼保護的文件夾/文件

[英]how to create a password protected folder/file in linux using python

我想在文件夾n中創建一個受密碼保護的文件(具有某些擴展名),當在該文件夾中創建一個新文件時,它應該被自動加密n使用python在Linux中使用python保護密碼我已經嘗試了pycrypto我可以加密,但是我無法密碼保護文件夾,所以請PLz幫助我,這是完整的程序

def discover():
#file_path = ''
#print file_path
    for root, dirs, files in os.walk(r'/root/Desktop/rakesh/rakeshp/dlp'):
       for file_name in files:
           file_path = os.path.join(root, file_name)
#print file_path        
file_list = []          
if file_path.endswith('.py'):
file_list.append(file_path)
dict = {}
status=""
for p in file_list:
    if not os.access(p, os.F_OK):
    dict[p]=status=status+"NOEXISTS"
    if(os.access(p,os.R_OK)):
    dict[p]=status=status+"READ," 
    if(os.access(p, os.W_OK)):
    dict[p]=status=status+"WRITE," 
    if (os.access(p, os.X_OK)):
    dict[p]=status=status+"EXECUTE"  
    elif os.access(p, os.F_OK) and not (os.access(p,os.R_OK)) and not (os.access(p, os.W_OK)) and not   (os.access(p, os.X_OK)):
    dict[p]=status=status="NOACCESS"
    status="" # Set blank before we enter the loop again            
    os.chmod(file_path,0444)                    
    for size in file_list:              
    size = os.path.getsize(file_path)       
    #print size                 
    #print (dict)
    in_filename = file_path
    #print in_filename
    if not out_filename:
    out_filename = in_filename + '.enc'
    iv = 16 * '\x00'
    #iv = bytes([random.randint(0, 0xFF) for i in range(16)])
    #print iv
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    with open(in_filename, 'rb') as infile:
        with open(out_filename, 'wb') as outfile:
        outfile.write(struct.pack('<Q', size))
        outfile.write(iv)
        while True:
            chunk = infile.read(chunksize)
            if len(chunk) == 0:
            #print (chunk)
            break
            elif len(chunk) % 16 != 0:
            chunk += ' ' * (16 - len(chunk) % 16)
            #print (chunk)
            outfile.write(encryptor.encrypt(chunk))

if __name__ == "__main__":
    discover()

恕我直言,這取決於密碼保護的含義。 如果要基於密碼限制對用戶的訪問,那么您要做的就是創建用戶並在文件上執行chown 然后,用戶必須登錄系統才能訪問它。

否則,您可以根據此問題嘗試一些操作,並根據密碼的哈希值對文件進行加密。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM