Obfuscate strings in Python
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)
Obfuscating a string that can afterwards be used in a web address
I ended up solving the issue with itsdangerous, which is a dependency of Flask so I have it on my server anyway.
As the example here shows:
>>> from itsdangerous import URLSafeSerializer
>>> s = URLSafeSerializer('secret-key')
>>> s.dumps([1, 2, 3, 4])
'WzEsMiwzLDRd.wSPHqC0gR7VUqivlSukJ0IeTDgo'
>>> s.loads('WzEsMiwzLDRd.wSPHqC0gR7VUqivlSukJ0IeTDgo')
[1, 2, 3, 4]
It's safe to assume I won't have any surprises as the docstring says:
Works like
:class:Serializer
but dumps and loads into a URL safe string consisting of the upper and lowercase character of the alphabet as well as_
,-
and.
.
Obfuscating a string to be saved in a text file
def myencode_str(ori_str, key):
enc = []
for i in range(len(ori_str)):
key_c = key[i % len(key)]
enc_c = (ord(ori_str[i]) + ord(key_c)) % 256
enc.append(enc_c)
return (base64.urlsafe_b64encode(bytes(enc))).decode("utf-8")
or see here for a full example:
from cryptography.fernet import Fernet
class Encrypt(object):
'''
see https://cryptography.io/en/latest/fernet/
'''
@classmethod
def encrypt(cls, plain_text):
'''
@param enctypted_text: str or bytes
@return cipher_text: str (.decode() converts the byte string to string)
'''
if isinstance(plain_text, str):
plain_text = plain_text.encode()
elif not isinstance(plain_text, bytes):
raise ValueError('Value must be string or bytes')
cipher_suite = Fernet(config.KEY.encode())
cipher_text = cipher_suite.encrypt(plain_text).decode()
return cipher_text
@classmethod
def decrypt(cls, enctypted_text):
'''
@param enctypted_text: str or bytes
@return plain_text: str (.decode() converts the byte string to string)
'''
if isinstance(enctypted_text, str):
enctypted_text = enctypted_text.encode()
elif not isinstance(enctypted_text, bytes):
raise ValueError('Value must be string or bytes')
cipher_suite = Fernet(config.KEY.encode())
plain_text = cipher_suite.decrypt(enctypted_text).decode()
return plain_text
@classmethod
def generateKey(cls):
key = Fernet.generate_key()
return key*
obfuscating filenames in python
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.
Related Topics
List Nearby/Discoverable Bluetooth Devices, Including Already Paired, in Python, on Linux
Pip Error:'Module' Object Has No Attribute 'Cryptography_Has_Ssl_St'
Can You Fool Isatty and Log Stdout and Stderr Separately
/Usr/Bin/Ld: Cannot Find -Lpython2.7
Anaconda: Disable Prompt Change
Find Broken Symlinks with Python
Better Way to Script Usb Device Mount in Linux
Linux/Python: Encoding a Unicode String for Print
Pyserial Works Fine in Python Interpreter, But Not Standalone
Running Python Script as a Systemd Service
/Usr/Bin/Env: Python2: No Such File or Directory
Plotting the Data with Scrollable X (Time/Horizontal) Axis on Linux
Does R Have Function Startswith or Endswith Like Python