- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的任务是用密码保护一个 Java 应用程序,而对真正的安全性几乎不关心。因此,将用户名/密码对存储在文本文件中然后对其进行加密似乎是明智的。对于加密,使用 XOR 密码似乎是合适的,因为它们既简单又快速(记住——它只是要阻止临时用户,而不是防弹)。
我编写了所有合适的 Java,然后意识到我需要一种方法来加密配置文件。我写了一个额外的方法,但使用超过一两次就很笨拙(而且似乎只对某些输入有效),所以我决定最好用 Python 写一些东西,以便在 REPL 中使用。
这是我最终得到的:
from itertools import izip, cycle
KEY = "stackoverflow"
def encrypt(text):
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(text,cycle(KEY)))
def decrypt(text):
return encrypt(text)
def export(users, file):
with open(file, "w") as f:
for user, password in users.items():
f.write(encrypt('"%s" "%s"'%(user, password)) + "\n")
def import_data(file):
with open(file) as f:
return [decrypt(i) for i in f.readlines()]
从表面上看,它有效:
>>> x = encrypt("Hello world!")
>>> x
';\x11\r\x0f\x04O\x01\n\x00\n\x08N'
>>> decrypt(x)
'Hello world!'
但随后事情开始分崩离析:
>>> export({"foo" : "bar", "baz" : "quux", "spam" : "eggs"}, "users.dat")
>>> import_data("users.dat")
['"foo" "bar"e', '"baz" "quux"}', '"spam" "eggs"y']
这是 vim 读取它的方式 -
然后:
>>> export({"what" : "not", "this" : "that", "admin_istrator" : "quux"}, "users2.dat")
>>> import_data("users2.dat")
['"thi', "k97$ma{~'l", '"what" "not"}', '"admin_istrator" "quux', '7~']
Vim :
我突然想到,我可能对字符的加密形式作为换行符有疑问,但据我所知,这并不能解释第一个示例或所有中的古怪行为第二个古怪的行为。
关于换行符,我的 B 计划是加密整个文件——换行符和所有文件——然后将其备份、解密、拆分为“\n”,然后继续我的基于行的解析。
提前致谢。
更新:这是我对 B 计划的实现(前两段所述)。
def import2(file):
with open(file) as f:
return decrypt(f.read())
然后:
>>> export({"foo" : "bar", "this" : "that", "admin_istrator" : "letmein"}, "users2.dat")
>>> import2("users2.dat")
'"this" "that"y%smg&91uux!}"admin_istrator" "letmein"y'
更新二:二进制。
[代码同上,只是所有的open
都是open(file, "rb")
或者open(file, "wb")
。]
>>> export({"foo" : "bar", "this" : "that", "admin_istrator" : "letmein"}, "users2.dat")
>>> import2("users2.dat")
'"this" "that"y%smg&91uux!}"admin_istrator" "letmein"y'
>>> import_data("users2.dat")
['"t', "k97$ma{~'", '"foo" "bar"', '"admin_istrator" "letmein"']
最终更新:Base 64,其他恶作剧。
def import2(file):
with open(file, "rb") as f:
return filter(str.strip, [decrypt(i) for i in f.readlines()])
其中 encrypt
和 decrypt
encode
in/decode
base 64。
最佳答案
您正在尝试将二进制文件存储在文本模式文件中。使用 open(file, "wb")
用于写作和open(file, "rb")
用于读取以二进制模式打开文件并修复问题。
在文本模式下每"\r"
, "\n"
和 "\r\n"
序列被视为换行符,因此它们被转换为您本地操作系统的行结束约定("\r\n"
适用于 Windows,"\n"
适用于 Unix,"\r"
适用于旧 Mac)。如果您从文本文件中读取它们,您将始终得到 "\n"
相反,如果你写它们,我不记得实际的行为,但你肯定也会弄得一团糟,而不是你的数据:)
使用 XOR 加密,您很可能会遇到这种情况:)
如果您被迫不使用二进制文件,请尝试使用 base64 编码(例如 "some\0te\n\nxt with bi\x01naries".encode('base64')
)。解码使用 .decode
(谢谢,Captain Obvious!)。
关于python - XOR 加密在*大部分*时间都有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6259194/
XOR 的各个部分如何命名? A xor B = C A和B叫什么 对于分部,它是: A / B = C A = 除数,B = 被除数,C = 商 对于和(和 XOR 与和一样对称)它是 A + B
我在算法方面遇到了问题。 我有一个用于 IO 的字节,可以使用称为 XorAndXor 的方法设置其中的某些位。该算法的工作原理如下: newValue = (((currentValue XOR x
我很惊讶地看到一个 BPMN 图,其中一个异或决策(“XOR-Split”)用相同的网关符号“关闭”。 我真的想知道证明这种方法合理的原因是什么。在我看来,这是多余的。 事实似乎是,XOR-Join
我在查看 XOR 链接列表的实现时多次遇到这段代码,但似乎没有一个人正确解释了这一行(或者也许我错过了一些东西) - struct node* XOR (struct node *a, struct
这应该是一个简单的问题。我是 Coq 的新手。 我想在 Coq 中定义exclusive or in Coq(据我所知,这不是预定义的)。重要的部分是允许多个命题(例如 Xor A B C D)。 我
我试图记住数学是如何计算出来的,以计算循环冗余检查中 XOR 算法的剩余部分,以验证网络消息的剩余位。 我不应该扔掉那本教科书。 这在代码中很容易完成,但是如何手工完成呢? 我知道它看起来有点像标准除
给定一个数字 N 和一个整数数组(所有整数都小于 2^15)。 (A 是数组 100000 的大小) 从数组中找到 N 和整数的最大 XOR 值。 Q是查询次数(50000)和开始,停止是数组中的范围
这更像是一个有趣的问题。我正在研究 SC61860 CPU,它是 8 位 CPU,用于 1987 年的 Sharp PC-1360 掌上电脑(也用于 PC-1401 和 1403)。它的指令集实际上并
我正在尝试在 c 中进行某种异或文件加密,并在 javascript 中进行解密(使用 this 作为基础,现在我遇到了以下问题: 例如我想在 C 中执行 73^122,结果是 57,但在 javas
我的任务是计算数组中字节的异或和: X = char1 XOR char2 XOR char3 ... charN; 我正在尝试将其并行化,改为对 __m128 进行异或运算。这应该提供加速因子 4。
我有一系列错误或 View ( Seq[Xor[Error,View]] ) 我想将其映射到第一个错误(如果有)或 View 序列的异或 ( Xor[Error, Seq[View]] ) 或者可能只
这是我想做的一个例子:假设我有 5 个类,我想表达这样的约束,即我们可以将类“B”或/和“C”的实例链接到“A”,如果是这样,我们就不能拥有其他任何东西,如果我们不这样做没有这些类的任何实例,我们只能
因此我们可以确定异或距离度量是一个真实的度量(它是对称的,满足三角不等式等) 在阅读 Kademlia 及其 k-buckets 之前,我在想每个节点都会简单地找到自己的 id 并存储其最近的 k 个
该函数用于计算一个32位整数的异或 int xor32int(int x, int y) { int res = 0; // Initialize result // Assuming
我是加密新手,我正在尝试解释以下代码。即, 是什么?什么意思? 我有一个 secret_key key 。我也有一个 unique_id。我使用下面的代码创建垫。 pad = hmac.new(sec
我正在尝试将一些 javascript 代码复制到 python 中,由于某种原因,javascript 中的 XOR 运算符 (^) 给我的值与 python 中的 XOR 运算符 (^) 不同。我
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
这个问题在这里已经有了答案: Does Typescript support mutually exclusive types? (7 个答案) 关闭 3 年前。 我怎么说我希望一个接口(inter
我有一些未知的 C++ 代码是在发布版本中编译的,因此对其进行了优化。我正在努力解决的问题是: xor al, al add esp, 8 cmp byte ptr [ebp+
我得到了以下卡诺图,但我仍然无法从每个表中计算 XOR 的表达式。 Table 1 -------
我是一名优秀的程序员,十分优秀!