Created
March 31, 2014 07:25
-
-
Save inaz2/9887030 to your computer and use it in GitHub Desktop.
utilities for XOR/ROT substritution cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python | |
Python 2.7.3 (default, Feb 27 2014, 20:00:17) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> from xortools import * | |
>>> plaintext = 'This program cannot be run in DOS mode.' | |
>>> ciphertext = xorencode(plaintext, 'AX') | |
>>> ciphertext | |
'\x150(+a(37&* 5a; 6/75x#=a*46a1/x\x05\x17\x12x,7%=o' | |
>>> find_xorkey(ciphertext, 'program', keylen=2) | |
'AX': 'This program cannot be run in DOS mode.' | |
>>> xordecode(ciphertext, 'AX') | |
'This program cannot be run in DOS mode.' | |
>>> ciphertext = rotencode(plaintext, 8) | |
>>> ciphertext | |
'Bpqa xzwoziu kivvwb jm zcv qv LWA uwlm.' | |
>>> find_rotkey(ciphertext) | |
0: 'Bpqa xzwoziu kivvwb jm zcv qv LWA uwlm.' | |
1: 'Aopz wyvnyht jhuuva il ybu pu KVZ tvkl.' | |
2: 'Znoy vxumxgs igttuz hk xat ot JUY sujk.' | |
3: 'Ymnx uwtlwfr hfssty gj wzs ns ITX rtij.' | |
4: 'Xlmw tvskveq gerrsx fi vyr mr HSW qshi.' | |
5: 'Wklv surjudp fdqqrw eh uxq lq GRV prgh.' | |
6: 'Vjku rtqitco ecppqv dg twp kp FQU oqfg.' | |
7: 'Uijt qsphsbn dboopu cf svo jo EPT npef.' | |
8: 'This program cannot be run in DOS mode.' | |
9: 'Sghr oqnfqzl bzmmns ad qtm hm CNR lncd.' | |
10: 'Rfgq npmepyk ayllmr zc psl gl BMQ kmbc.' | |
11: 'Qefp moldoxj zxkklq yb ork fk ALP jlab.' | |
12: 'Pdeo lnkcnwi ywjjkp xa nqj ej ZKO ikza.' | |
13: 'Ocdn kmjbmvh xviijo wz mpi di YJN hjyz.' | |
14: 'Nbcm jlialug wuhhin vy loh ch XIM gixy.' | |
15: 'Mabl ikhzktf vtgghm ux kng bg WHL fhwx.' | |
16: 'Lzak hjgyjse usffgl tw jmf af VGK egvw.' | |
17: 'Kyzj gifxird treefk sv ile ze UFJ dfuv.' | |
18: 'Jxyi fhewhqc sqddej ru hkd yd TEI cetu.' | |
19: 'Iwxh egdvgpb rpccdi qt gjc xc SDH bdst.' | |
20: 'Hvwg dfcufoa qobbch ps fib wb RCG acrs.' | |
21: 'Guvf cebtenz pnaabg or eha va QBF zbqr.' | |
22: 'Ftue bdasdmy omzzaf nq dgz uz PAE yapq.' | |
23: 'Estd aczrclx nlyyze mp cfy ty OZD xzop.' | |
24: 'Drsc zbyqbkw mkxxyd lo bex sx NYC wyno.' | |
25: 'Cqrb yaxpajv ljwwxc kn adw rw MXB vxmn.' | |
>>> rotdecode(ciphertext, 8) | |
'This program cannot be run in DOS mode.' | |
>>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import cycle, product | |
def xorencode(s, key='\xFF'): | |
return ''.join(chr(ord(x)^ord(y)) for x, y in zip(s, cycle(key))) | |
xordecode = xorencode | |
def find_xorkey(s, word, keylen=1): | |
for tup in product(xrange(0x100), repeat=keylen): | |
key = ''.join(chr(x) for x in tup) | |
s2 = xordecode(s, key) | |
if word in s2: | |
print "%r: %r" % (key, s2) | |
def rotencode(s, n=13): | |
a = [] | |
for c in s: | |
x = ord(c) - 0x41 | |
if 0 <= x < 0x1B: | |
a.append(chr(0x41 + ((x+n) % 26))) | |
continue | |
x -= 0x20 | |
if 0 <= x < 0x1B: | |
a.append(chr(0x61 + ((x+n) % 26))) | |
continue | |
a.append(c) | |
return ''.join(a) | |
def rotdecode(s, n=13): | |
return rotencode(s, 26-n) | |
def find_rotkey(s): | |
for i in xrange(26): | |
x = rotdecode(s, i) | |
print "%d: %r" % (i, x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment