Skip to content

Commit 17c5bff

Browse files
authored
Merge pull request #109 from jatinluthra14/sbyte_xor
Single Byte Xor Bruteforcer
2 parents c71ff99 + 3d0c822 commit 17c5bff

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Cryptography/single_byte_xor.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
def score(text):
4+
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.'\n"
5+
s = 0
6+
for i in text:
7+
if i in charset or i == ' ' or i == '\'':
8+
s+=1
9+
return s
10+
11+
def xor(s1, s2):
12+
res = ""
13+
for i in range(0, len(s1)):
14+
res += chr(ord(s1[i]) ^ ord(s2[i%len(s2)]))
15+
return res
16+
17+
def main():
18+
best = ""
19+
b = 0
20+
for i in range(1, 256):
21+
c = xor(sys.argv[1].decode('hex'), chr(i))
22+
print(c,i)
23+
if score(c) > b:
24+
b = score(c)
25+
best = c
26+
print("Plaintext: {}".format(best))

0 commit comments

Comments
 (0)