我有一个python脚本正在创建一个ODBC连接.使用连接字符串生成ODBC连接.在此连接字符串中,我必须包含此连接的用户名和密码.
是否有一种简单的方法可以隐藏文件中的密码(只是在我编辑文件时没有人能读取密码)?
Dav*_*ebb 108
Base64编码在标准库中,可以阻止肩膀冲浪:
>>> import base64
>>> print(base64.b64encode("password".encode("utf-8")))
cGFzc3dvcmQ=
>>> print(base64.b64decode("cGFzc3dvcmQ=").decode("utf-8"))
password
Run Code Online (Sandbox Code Playgroud)
Mar*_*ett 50
当您需要为远程登录指定密码时,Douglas F Shearer是Unix中普遍认可的解决方案.
您可以添加--password-from-file选项以指定路径并从文件中读取明文.
然后,该文件可以位于受操作系统保护的用户自己的区域中.它还允许不同的用户自动获取自己的文件.
对于不允许脚本用户知道的密码 - 您可以使用elavated权限运行脚本,并拥有该root/admin用户拥有的密码文件.
小智 45
这是一个简单的方法:
这应该比base64解码更安全 - 尽管它容易受到py_to_pyc反编译器的攻击.
jon*_*erg 27
如果您正在使用Unix系统,请利用标准Python库中的netrc模块.它从单独的文本文件(.netrc)中读取密码,该文件具有此处描述的格式.
这是一个小用法示例:
import netrc
# Define which host in the .netrc file to use
HOST = 'mailcluster.loopia.se'
# Read from the .netrc file in your home directory
secrets = netrc.netrc()
username, account, password = secrets.authenticators( HOST )
print username, password
Run Code Online (Sandbox Code Playgroud)
tdu*_*ehr 19
假设用户在运行时无法提供用户名和密码的最佳解决方案,可能是一个单独的源文件,仅包含导入主代码的用户名和密码的变量初始化.凭据更改时,只需要编辑此文件.否则,如果您只担心具有平均记忆的肩膀冲浪者,基本64编码可能是最简单的解决方案.ROT13太容易手动解码,不区分大小写并且在加密状态下保留太多含义.在python脚本之外编码您的密码和用户ID.他是否在运行时进行脚本解码以供使用.
为自动化任务提供脚本凭据始终是一个冒险的提议.您的脚本应该有自己的凭据,并且它使用的帐户应该没有其他所需的访问权限.至少密码应该很长而且是随机的.
tzo*_*zot 15
base64是满足您简单需求的方法.无需导入任何内容:
>>> 'your string'.encode('base64')
'eW91ciBzdHJpbmc=\n'
>>> _.decode('base64')
'your string'
Run Code Online (Sandbox Code Playgroud)
for python3 obfuscation using base64
is done differently:
import base64
base64.b64encode(b'PasswordStringAsStreamOfBytes')
Run Code Online (Sandbox Code Playgroud)
which results in
b'UGFzc3dvcmRTdHJpbmdBc1N0cmVhbU9mQnl0ZXM='
Run Code Online (Sandbox Code Playgroud)
note the informal string representation, the actual string is in quotes
and decoding back to the original string
base64.b64decode(b'UGFzc3dvcmRTdHJpbmdBc1N0cmVhbU9mQnl0ZXM=')
b'PasswordStringAsStreamOfBytes'
Run Code Online (Sandbox Code Playgroud)
to use this result where string objects are required the bytes object can be translated
repr = base64.b64decode(b'UGFzc3dvcmRTdHJpbmdBc1N0cmVhbU9mQnl0ZXM=')
secret = repr.decode('utf-8')
print(secret)
Run Code Online (Sandbox Code Playgroud)
for more information on how python3 handles bytes (and strings accordingly) please see the official documentation.
小智 5
我这样做的方法如下:
在 python 外壳中:
>>> from cryptography.fernet import Fernet
>>> key = Fernet.generate_key()
>>> print(key)
b'B8XBLJDiroM3N2nCBuUlzPL06AmfV4XkPJ5OKsPZbC4='
>>> cipher = Fernet(key)
>>> password = "thepassword".encode('utf-8')
>>> token = cipher.encrypt(password)
>>> print(token)
b'gAAAAABe_TUP82q1zMR9SZw1LpawRLHjgNLdUOmW31RApwASzeo4qWSZ52ZBYpSrb1kUeXNFoX0tyhe7kWuudNs2Iy7vUwaY7Q=='
Run Code Online (Sandbox Code Playgroud)
然后,使用以下代码创建一个模块:
from cryptography.fernet import Fernet
# you store the key and the token
key = b'B8XBLJDiroM3N2nCBuUlzPL06AmfV4XkPJ5OKsPZbC4='
token = b'gAAAAABe_TUP82q1zMR9SZw1LpawRLHjgNLdUOmW31RApwASzeo4qWSZ52ZBYpSrb1kUeXNFoX0tyhe7kWuudNs2Iy7vUwaY7Q=='
# create a cipher and decrypt when you need your password
cipher = Fernet(key)
mypassword = cipher.decrypt(token).decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
完成此操作后,您可以直接导入 mypassword,也可以根据需要导入令牌和密码以进行解密。
显然,这种方法有一些缺点。如果某人同时拥有令牌和密钥(就像他们拥有脚本一样),他们就可以轻松解密。但是它确实会混淆,如果您编译代码(使用 Nuitka 之类的东西),至少您的密码不会在十六进制编辑器中显示为纯文本。
归档时间: |
|
查看次数: |
182157 次 |
最近记录: |