Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obfuscate strings in Python

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.

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.

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

like image 355
Caedis Avatar asked Jun 11 '09 16:06

Caedis


People also ask

Is there a way to obfuscate Python code?

In Python, There are multiple packages available using which you can obfuscate your code base and secure your intellectual property. pyarmor — full obfuscation with hex-encoding; apparently doesn't allow partial obfuscation of variable/function names only.

How do I create an unreadable code in Python?

Make your source code unreadable As a developer, you can use code obfuscation to make the program files ideally unreadable to a human but still executable by a computer, thus preventing undesired eyes from sneaking into your application code.

What is string obfuscation?

String obfuscation is an established technique used by proprietary, closed-source applications to protect intellectual property. Furthermore, it is also frequently used to hide spyware or malware in applications. In both cases, the techniques range from bit-manipulation over XOR operations to AES encryption.


1 Answers

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)
like image 141
John G Avatar answered Sep 18 '22 08:09

John G