简体   繁体   English

如何使用python在linux中创建受密码保护的文件夹/文件

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

i would like to create a password protected files(with some extentions) in a folder n when exer a new file is created in the folder it should be auto encrypted n password protected in linux using python i have tried with pycrypto i can encrypt but i cant password protect the folder so plz help me with this here is the complete prog 我想在文件夹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()

IMHO it depends what you mean by password protection. 恕我直言,这取决于密码保护的含义。 If you want to restrict access to an user based on a password, then all you need to do is to create the user and perform a chown on the file. 如果要基于密码限制对用户的访问,那么您要做的就是创建用户并在文件上执行chown Then the user has to be logged in on the system to be able to access it. 然后,用户必须登录系统才能访问它。

Otherwise, you could try something based on this question and encrypt the file based on the hash of the password. 否则,您可以根据此问题尝试一些操作,并根据密码的哈希值对文件进行加密。

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

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