Python 3.x 混淆要保存在文本文件中的字符串
我试图混淆字符串,以便用户不容易读取。但是,模糊化字符串的形式应该是可以存储在文本文件中的字符串,而不是以字节形式存储在二进制文件中。我尝试了一些方法,但没有成功:Python 3.x 混淆要保存在文本文件中的字符串,python-3.x,encode,Python 3.x,Encode,我试图混淆字符串,以便用户不容易读取。但是,模糊化字符串的形式应该是可以存储在文本文件中的字符串,而不是以字节形式存储在二进制文件中。我尝试了一些方法,但没有成功: def myencode(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
def myencode(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))
上述fn的输出需要存储在二进制文件中。将..\u b64encode(字节(enc))
更改为..\u b64encode(enc)
不起作用
如何实现可存储在文本文件中的类似结果
编辑: 相应的解码fn如下所示:
def mydecode(enc_str, key):
dec = []
enc_str = base64.urlsafe_b64decode(enc_str)
for i in range(len(enc_str)):
key_c = key[i % len(key)]
dec_c = chr((256 + enc_str[i] - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
或者,请参见此处以获取完整示例:
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*
解码fn是否也需要更改?我已经在上面的问题的编辑中添加了decode fn。我不能100%确定要做什么,但可以将字符串编码为字节,即:bytes_out=some_string。encode('utf-8')这个
bytes_out
是否可以保存为文本(而不是二进制)文件中的字符串并作为字符串再次读取?我甚至可以这样做:,尽管此文件中没有密码密钥。有关易于使用的加密/解密序列,请参阅
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*