python中混淆文件名

发布于 2025-01-18 09:18:09 字数 876 浏览 2 评论 0原文

我有一个处理一些敏感文件的应用程序。当应用程序运行时,它会解密每个文件大约一秒钟,然后在完成后重新加密。

除了加密内容之外,我想隐藏文件名本身。我已经成功地在我的 Mac 上使用简单的凯撒密码风格的移位函数可靠地完成了此操作,如下所示:

混淆函数(将字符移位 5 个位置):

def obs_filename(text, places=5):
    text = text.replace(' ', '')
    return ''.join(chr(ord(char) + places) for char in text)

用法用法:

  for file in files:
      file_name = os.path.basename(file)
      new_file_name=deobs_filename(file_name)
      os.rename(folder+"/"+file, folder+"/"+new_file_name)

要反混淆该函数,只需反转该函数即可移位另一个位置方式。

    def deobs_filename(text, places=5):
    text = text.replace(' ', '')
    return ''.join(chr(ord(char) - places) for char in text)

问题是,这似乎会在 Windows 中导致错误(我怀疑某种形式的允许文件名字符错误,尽管该函数只是失败并且 cmd 行中没有显示错误)。我怀疑 Windows 中允许的字符有错误的原因是某些文件被重命名,然后中途失败。

希望有人有更好的方法来混淆文件名,或者可能编辑函数以仅允许字母字符而不是所有字符?

I have an app that handles some sensitive files. When the app runs, it decrypts each file for about a second then re-encrypts it once its done with it.

I would like to hide the filename itself, on top of encrypting the content. I have managed to do this reliably on my mac using a simple caesar-cipher style shifting function, as follows:

Obfuscation function (shifts characters over by 5 places):

def obs_filename(text, places=5):
    text = text.replace(' ', '')
    return ''.join(chr(ord(char) + places) for char in text)

Usage usage:

  for file in files:
      file_name = os.path.basename(file)
      new_file_name=deobs_filename(file_name)
      os.rename(folder+"/"+file, folder+"/"+new_file_name)

To de-obfuscate the function is just reversed to shift the other way.

    def deobs_filename(text, places=5):
    text = text.replace(' ', '')
    return ''.join(chr(ord(char) - places) for char in text)

The problem is that this seems to be causing errors in windows (I suspect some form of allowed filename character error, although the function just fails and no error is shown in the cmd line). The reason I suspect an error in the allowed characters in windows is that some of the files are getting renamed, then it fails partway through.

Hoping someone has a better way of obfuscating the filename, or possibly editing the function to only allow alphabet characters instead of all characters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2025-01-25 09:18:10

我最终暂时这样做是为了解决 Windows 问题,只是将文件名转换为十六进制,这样它们就没有任何意义了。这不是保护它们的好方法,甚至可能比凯撒密码更糟糕,但至少它有效。如果有人有文件名,仍在寻找更好的选择来隐藏文件名。

Obsfuscate:

    def obs_filename(fname):
    string = fname.encode('utf-8').hex()
    return string

反混淆:

    def deobs_filename(fname):
    string=codecs.decode(fname, "hex")  
    new_file_name=(str(string,'utf-8'))

这适用于 Macos 和 Windows,其目的是让您第一眼不知道您在看什么。

I ended up doing this for the time being to get around the windows issue, is just turning the filenames to hex so they don't mean anything. Not a great way to secure them, and probably even worse than the cesar cipher, but at least it works. Still looking for a better option to hide the filenames if someone has one.

Obsfuscate:

    def obs_filename(fname):
    string = fname.encode('utf-8').hex()
    return string

Deobfuscate:

    def deobs_filename(fname):
    string=codecs.decode(fname, "hex")  
    new_file_name=(str(string,'utf-8'))

This works on both macos and windows, and serves the purpose of not knowing what you're looking at at first glance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文