在 Python 中混淆字符串字符串、Python

2023-09-06 23:13:47 作者:巷里无风尓发动,

我有一个必须传递给方法的密码字符串.一切正常,但我不习惯以明文形式存储密码.有没有办法混淆字符串或真正加密它?我知道混淆可以被逆向工程,但我认为我至少应该尝试掩盖密码.至少它不会被索引程序看到,或者让杂散的眼睛快速查看我的代码.

I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the password a bit. At the very least it wont be visible to a indexing program, or a stray eye giving a quick look at my code.

我知道 pyobfuscate 但我不希望整个程序被混淆,只有一个字符串,可能是定义变量的整行本身.

I am aware of pyobfuscate but I don't want the whole program obfuscated, just one string and possibly the whole line itself where the variable is defined.

目标平台是 GNU Linux Generic(如果有区别的话)

Target platform is GNU Linux Generic (If that makes a difference)

推荐答案

如果您只是想防止随意查看密码,您可能需要考虑将密码编码/解码到/从 base64.它至少不安全,但密码不会被人类/机器人随便读取.

If you just want to prevent casually glancing at a password, you may want to consider encoding/decoding the password to/from base64. It's not secure in the least, but the password won't be casually human/robot readable.

import base64
# Encode password (must be bytes type)
encoded_pw = base64.b64encode(raw_pw)

# Decode password (must be bytes type)
decoded_pw = base64.b64decode(encoded_pw)