Beispiel #1
0
print(len(value))
print(len(set(value)))
print(Counter(value))


def overlapping_substrings(string, size):
    """ Yields all substrings of a given length from a string """
    for i in range(len(string) + 1 - size):
        v = string[i:i + size]
        if 'k' not in v:
            yield v


print(Counter(overlapping_substrings(value, 2)))
print(Counter(overlapping_substrings(value, 3)))

translation = {
    'k': ' ',
    'e': 'T',
    'O': 'H',
    'V': 'E',
}

print(value)

result = decode(value, translation)
print(result)

print(Counter(result.split()))

print(result[:10])
Beispiel #2
0
from hacker.decoder import decode, int2

value = '0111 0011 1001 0011 1001 0001'

result = decode(value.split(), int2, str)
print(result)
Beispiel #3
0
from hacker.bytestreams import bytes_from_binary
from hacker.decoder import decode
from hacker.settings import inputfile
from hacker.text import remove_punctuation

filename = inputfile('crypto', 'didactic_text_4', 'text.txt')
with open(filename, 'r') as file:
    value = file.read()

value = remove_punctuation(value).lower()

translation = {
    'he': 0,
    'him': 0,
    'his': 0,
    'himself': 0,
    'she': 1,
    'her': 1,
    'hers': 1,
    'herself': 1,
}

binary = decode(filter(lambda w: w in translation, value.split()), translation,
                str)

result = decode(bytes_from_binary(binary), chr)
print(result)
Beispiel #4
0
from hacker.bytestreams import substrings
from hacker.decoder import decode

value = 'fmpmfpmpp mmmpppfmmfppmpppff fmpppf pmfmffmpfmpp, fmpmfpmpp fmfpppmfffpmmpppfffmmmpp, mmmpppmpm mppfpmmpppffffmfmpmfpmffpppmfm mfffmm mpfppfpfffmpffmfmpfppppf'  # noqa

# To make decoding easier, we also make the puntuation take 3 characters
temp = decode(value, {' ': '   ', ',': ',,,'})

translation = {
    '   ': ' ',
    ',,,': ',',
    'fmp': 'T',
    'mfp': 'H',
    'mpp': 'E',
    'ppf': 'O',
    'mmm': 'a',
    'ppp': 'N',
    'fmm': 'S',
    'fpp': 'W',
    'pff': 'R',
    'mpm': 'D',
    'fmf': 'U',
    'pmf': 'L',
    'mff': 'I',
    'mpf': 'F',
    'fpm': 'V',
    'ffm': 'Y',
    'mfm': 'G',
}

result = decode(substrings(temp, 3), translation)
Beispiel #5
0
from hacker.decoder import decode
from hacker.images import pixels
from hacker.settings import inputfile

filename = inputfile('crypto', 'didactic_green', 'greenline.png')

result = decode(pixels(filename), lambda x: x[1], chr)
print(result)
Beispiel #6
0
from hacker.decoder import decode

value = '+90+509+20+355+977+46+685+503+7+98+421+43+45+380+377'

# Source: https://en.wikipedia.org/wiki/List_of_country_calling_codes
translation = {
    '90': 'Turkey',
    '509': 'Haiti',
    '20': 'Egypt',
    '355': 'Albania',
    '977': 'Nepal',
    '46': 'Sweden',
    '685': 'Western Samoa',
    '503': 'El Salvador',
    '7': 'Republic of Kazakhstan',
    '98': 'Iran',
    '421': 'Slovakia',
    '43': 'Austria',
    '45': 'Denmark',
    '380': 'Ukraine',
    '377': 'Monaco',
}

result = decode(value[1:].split('+'), translation, lambda s: s[0])
print(result)
counter = Counter(overlapping_substrings(value, 2))
inputs = {
    character:
    tuple([counter[character + character2] for character2 in ALPHABET])
    for character in ALPHABET
}

# It's no guarantee, but 100 iterations seems to give the same answer consistently
clusters = cluster(inputs, amount_of_clusters=4, iterations=100)
result = sorted(
    [sorted(character_list) for character_list in clusters.values()])

expected_result = [['0', '3'], ['1', '6', 'a', 'B', 'c', 'D', 'E'],
                   ['2', '4', '5', '7', '8'], ['9', 'F']]
assert result == expected_result  # Making sure we're not outputting a wrong result if we get unlucky with clustering

counter = Counter(value)
result = sorted(result,
                key=lambda character_list: -sum(
                    [counter[character] for character in character_list]))

translation = {}
for index, character_list in enumerate(result):
    translation.update({
        character: PLAINTEXT_DECREASING[index]
        for character in character_list
    })

result = decode('0123456789ABCDEF', translation)
print(result)
Beispiel #8
0
from hacker.decoder import decode, toint2

value = [199, 77, 202]

result = int(decode(value, toint2), 2)
print(result)
Beispiel #9
0
from hacker.decoder import decode, toint16
from hacker.images import pixels
from hacker.settings import inputfile

filename = inputfile('crypto', 'didactic_red', 'redline.png')

result = decode(pixels(filename), lambda x: x[0], toint16)
print(result)
from hacker.decoder import decode
from hacker.settings import inputfile

filename = inputfile('crypto', 'didactic_vampire_text', 'text.txt')
with open(filename, 'r') as f:
    value = f.read()

result = decode(filter(str.isupper, value))
print(result)
Beispiel #11
0
from hacker.ciphers import rot
from hacker.decoder import decode

value = 'Guvf zrffntr vf rapelcgrq va ebg 13. Lbhe nafjre vf svfupnxr.'

result = decode(value, lambda x: rot(x, 13))
print(result)
Beispiel #12
0
from hacker.bytestreams import bytes_from_binary
from hacker.decoder import decode

value = 'abbbabaaabbabaaaabbaababaabaaaaaabbaaaababbabbbaabbbaabbabbbabbbabbaabababbbaabaaabaaaaaabbabaababbbaabbaabaaaaaabbaaaababbaabaaabbbabababbabbababbaaabaabbbaabaabbaaaababbbabaaabbaabab'  # noqa

translation = {'a': '0', 'b': '1'}
value = decode(value, translation)

result = decode(bytes_from_binary(value), chr)
print(result)
Beispiel #13
0
from hacker.decoder import decode, int16

value = '5a 63 08 47 16 08 07 35 10 4b 6e 0a 59 13 44 10'

# periodical table, atomic weights to abbreviations of elements
# Source: https://nl.wikipedia.org/wiki/Periodiek_systeem
translation = {
    90: 'Th',
    99: 'Es',
    8: 'O',
    71: 'Lu',
    22: 'Ti',
    7: 'N',
    53: 'I',
    16: 'S',
    75: 'Re',
    110: 'Ds',
    10: 'Ne',
    89: 'Ac',
    19: 'K',
    68: 'Er',
}

result = decode(value.split(), int16, translation)
print(result)
Beispiel #14
0
    'c':
    'i',  # Look at the keys starting from here (will be made clear in the rest of the code)
    'O': 'j',
    'V': 'k',
    'E': 'l',
    'R': 'm',
    'I': 'n',
    'N': 'o',
    'G': 'p',  # Until here
    'Z': 'q',
    'M': 'r',
    'J': 's',
    'P': 't',
    'U': 'u',
    'B': 'v',
    'K': 'w',
    'a': 'x',
    'T': 'y',
    'D': 'z',
}

result = decode(value, translation)
print(result)
# The result making an effort to contain every letter is a hint to the answer lying somewhere in the key

sorted_by_key = sorted(list(translation.items()), key=lambda x: x[0])
print(decode(sorted_by_key[7:13], lambda t: t[1]))

sorted_by_value = sorted(list(translation.items()), key=lambda x: x[1])
print(decode(sorted_by_value[8:16], lambda t: t[0]))
Beispiel #15
0
from hacker.decoder import decode, int2

value = '1100111 0001110 1001111 1110111 1011011 1001111 0000000 0001111 0100111 1100111 1001111 0000000 0100000 1011011 1001110 0110000 1001111 0010101 0001111 0110000 1000111 0110000 1001110 0000010'  # noqa

result = decode(value.split(), int2, chr)
print(result)

result = decode(value.split(), int2, str, lambda x: x + ' ')
print(result)
Beispiel #16
0
from hacker.ciphers import rot
from hacker.decoder import decode

value = 'cqrb lryqna rb fjh, fjh qjamna cqjw axc cqracnnw. qnan, hxd wnena twxf qxf oja cx bqroc! xq kh cqn fjh, cqn jwbfna rb mnjmvjwblqnbc.'  # noqa

# Simply try all the keys
for key in range(26):
    result = decode(value, lambda x: rot(x, key))

    if 'the' in result:
        break

print(result)
Beispiel #17
0
from hacker.bytestreams import substrings
from hacker.decoder import decode

value = 'TACTGTGTGCTCATTCGGTTATCGACCCTTTCCACTTAATCAATCCCCTCTCTTCTCTTGACT'

# DNA to RNA translation
dna_to_rna = {'t': 'a', 'a': 'U', 'c': 'G', 'g': 'c'}
value = decode(value.lower(), dna_to_rna)

# RNA codon to protein translation
# Source: https://en.wikipedia.org/wiki/Genetic_code
translation = {
    'UUU': 'F',
    'UCU': 'S',
    'UAU': 'Y',
    'UGU': 'c',
    'UUC': 'F',
    'UCC': 'S',
    'UAC': 'Y',
    'UGC': 'c',
    'UUA': 'L',
    'UCA': 'S',
    'UAA': '.',
    'UGA': '.',
    'UUG': 'L',
    'UCG': 'S',
    'UAG': '.',
    'UGG': 'W',
    'CUU': 'L',
    'CCU': 'P',
    'CAU': 'H',
from hacker.bytestreams import ungroup, bytes_from_binary
from hacker.decoder import transform, decode
from hacker.images import pixels
from hacker.settings import inputfile, outputfile

filename = inputfile('crypto', 'lucy_in_the_sky_with_briolettes',
                     'briolette.png')
outputfilename = outputfile('crypto', 'lucy_in_the_sky_with_briolettes',
                            'briolette.txt')

temp = decode(ungroup(transform(pixels(filename), lambda p: p[:3])),
              lambda v: v % 2, str)
print(temp)
print(len(temp) % 8)

print(min(bytes_from_binary(temp, 5)))
print(max(bytes_from_binary(temp, 5)))

with open(outputfilename, 'wb') as output:
    output.write(bytes(bytes_from_binary(temp)))

result = decode(bytes_from_binary(temp, 10), lambda s: s % 128, chr)
print('START')
i = 0
for c in result:
    if 32 <= ord(c) <= 126:
        print(c, end='')
    else:
        print(f'<{ord(c):02x}>', end='')
    i += 1
    if i % 24 == 0:
from hacker.bytestreams import bytes_from_hex
from hacker.decoder import decode

value = '948881859781c4979186898d90c4c68c85878f85808b8b808881c6c4828b96c4908c8d97c4878c858888818a8381'

for key in range(0xff):
    result = decode(bytes_from_hex(value), lambda b: b ^ key, chr)
    if result.isprintable() and 'this' in result:
        print(result)
Beispiel #20
0
from hacker.decoder import decode
from hacker.settings import inputfile

value = 'Thy raiment waxed not old upon thee, neither did thy foot swell, these forty years. And it shall be, when the officers have made an end of speaking unto the people that they shall make captains of the armies to lead the people. And it shall be, if thou have no delight in her, then thou shalt let her go whither she will; but thou shalt not sell her at all for money, thou shalt not make merchandise of her, because thou hast humbled her. Look down from thy holy habitation, from heaven, and bless thy people Israel, and the land which thou hast given us, as thou swarest unto our fathers, a land that floweth with milk and honey. Now therefore write ye this song for you, and teach it the children of Israel: put it in their mouths, that this song may be a witness for me against the children of Israel. But Jeshurun waxed fat, and kicked: thou art waxen fat, thou art grown thick, thou art covered with fatness; then he forsook God which made him, and lightly esteemed the Rock of his salvation. And I commanded you at that time all the things which ye should do. When a man hath taken a new wife, he shall not go out to war, neither shall he be charged with any business: but he shall be free at home one year, and shall cheer up his wife which he hath taken. When a man hath taken a wife, and married her, and it come to pass that she find no favour in his eyes, because he hath found some uncleanness in her: then let him write her a bill of divorcement, and give it in her hand, and send her out of his house. They shall call the people unto the mountain; there they shall offer sacrifices of righteousness: for they shall suck of the abundance of the seas, and of treasures hid in the sand. Cursed shalt thou be when thou comest in, and cursed shalt thou be when thou goest out.'  # noqa

filename = inputfile('crypto', 'didactic_text_2', 'bible.txt')


def find_bible_verse(verse):
    """
    Find the specific bible verse in a file containing the King James edition of the bible (Source: gutenberg.org)
    """
    with open(filename, 'r') as file:
        for line in file.readlines():
            if verse[:
                     50] in line:  # The text file has extra newlines, so only check the start for matching
                return line.split()[0]


def index_to_letter(index):
    """ Convert a 1-index to a letter in the alphabet: 1 -> a,... """
    return chr(ord('a') + index - 1)


result = decode(value.split('. '), find_bible_verse,
                lambda s: int(s.split(':')[1]), index_to_letter)
print(result)
Beispiel #21
0
from collections import Counter

from hacker.bytestreams import bytes_from_binary, skip
from hacker.decoder import decode
from hacker.images import pixels, size
from hacker.settings import inputfile
from hacker.util import x_max, x_min

filename = inputfile('crypto', 'blizzard', 'blizzard.png')

width, height = size(filename)
print(width, height)  # 5 * 83, 7 * 47

temp = decode(pixels(filename), lambda p: '1' if p[0] else '0')
print(temp)

print(Counter(temp))

#result = decode(skip(bytes_from_binary(temp, 7), 47), chr)
#print(result)

from PIL import Image

from hacker.decoder import decode, toint2

im = Image.open(filename)
pix = im.load()
width, height = im.size


# IDEA try other "shapes", skipping X bits, etc.
    'Utah': 'UT',
    'Vermont': 'VT',
    'Virginia': 'VA',
    'Washington': 'WA',
    'West Virginia': 'WV',
    'Wisconsin': 'WI',
    'Wyoming': 'WY',
    # Districts, territories and possessions
    'American Samoa': 'AS',
    'District of Columbia': 'DC',
    'Federated States of Micronesia': 'FM',
    'Guam': 'GU',
    'Marshall Islands': 'MH',
    'Northern Mariana Islands': 'MP',
    'Palau': 'PW',
    'Puerto Rico': 'PR',
    'Virgin Islands': 'VI',
}
states = {value: key for key, value in states.items()}


def decode_substring(s):
    """
    Decode a substring of length 3
    The first 2 characters indicate a state, the third which letter in the state name to return
    """
    return states.get(s[:2])[ord(s[2]) - ord('a')]

result = decode(substrings(value, 3), decode_substring)
print(result)
Beispiel #23
0
from hacker.bytestreams import bytes_from_binary
from hacker.decoder import decode
from hacker.settings import inputfile

filename = inputfile('crypto', 'didactic_text_3', 'text.txt')
with open(filename, 'r') as f:
    binary = decode(f.readlines(), lambda line: line[-3:] == '  \n', lambda b: '1' if b else '0')

result = decode(bytes_from_binary(binary), chr)
print(result)
Beispiel #24
0
from hacker.bytestreams import substrings
from hacker.codes import BRAILLE
from hacker.decoder import decode

value = [
    ' . .  .     .  ..  .  . .  .      .  .     . .  .  .. .. .  ..  .  . .  .  .. .. .  ',
    '.. ..  .        . .  ..  . ..    .  .     .   .     .  .  . .  .  .  .   .  .     . ',
    '.              .  .   .    .        .     .  .  .. .     .     .     .     .        ',
]

result = decode(
    zip(substrings(value[0], 3), substrings(value[1], 3),
        substrings(value[2], 3)), lambda t: t[0][0:2] + t[1][0:2] + t[2][0:2],
    BRAILLE)
print(result)
Beispiel #25
0
from PIL import Image

from hacker.decoder import decode, toint16
from hacker.settings import inputfile

filename = inputfile('crypto', 'didactic_rgb', 'didactrgb.png')

rgb = Image.open(filename).load()[0, 0]

result = int(decode(rgb, toint16), 16)
print(result)
Beispiel #26
0
from hacker.decoder import decode

value = 'in cryptography, a substitution cipher is a method of encryption by which units of plainteXt are suBstituted with ciphertext according to a regular system; the "units" may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth. the receiver deciphers the text by performinG an inverse substitution. substitution ciphers can be compared with tRansposition ciphers. in a transposition cipher, the units of the plaintext are rearranged in a different and usually quite complex order, but the units themselves are left unchanged. by contrast, in a substitution cipher, the units of the plaintext are retained in the same sequence in the ciphertext, but the units themselves are altered. there are a number of different types of substitution cipher. if the cipher operates on single letters, it is termed a simple substitution cipher; a cipher that operates on larger groups of letters is termed polygraphic. a monoalphabetic cipher uses fixed substitution over the entire message, Whereas a polyalphabetic cipher uses a number of substItutions at different times in the message such as with homophones, where a unit from the plaintext is mapped to one of several possibilities in the Ciphertext. substitution over a sinGle letter simple substitution can be Demonstrated by writing out the alphabet in some order to represent the substitution. this is termed a substitution alphabet. the cipher alphabet may be shifted or reversed (creating the caesar and atbash ciphers, respectively) or scrambled in a more complex fashion, in which case it is called a mixed alphabet or deranged alphabet. traditionally, mixed alphabets are created by first writing out a keyword, removing repeated letters in it, then writing all the remaining letters in the alphabet. a disadvantage of this method of derangement is that the last letters of the alphabet (which are mostly low freQuency) tend to stay at the end. a stronger way of constructIng a mixed alphabet is to perform a columnar transposition on the ordinary alphabet using the keyword, but this is not often done. although the number of possible keys is very large (26! = 288.4, or about 88 bits), this Cipher is not very strong, beinG easily broken. provided the message is of reasonable length (see below), the cryptanalyst can deduce the probable meaning of the most common symbols by analyzing the frequency distRibution of the cipherteXt frequency analysis. this allows formation of partial words, which can be tentatively filled in, progressively expanding the (partial) solution (see frequency analysis for a demonstration of this). in some cases, underlying words can also Be determined from the pattern of their letters; for example, attract, osseous, and words with those two as the root are the only common enGlish words with the pattern abbcaDb. many people soLve such ciphers for recReation, as With cryptogram puzzles in the newspaper. accordinG to the unicity distance of english, 27.'  # noqa

translation = {
    'X': 't',
    'B': 'h',
    'G': 'e',
    'R': 'a',
    'W': 'n',
    'I': 's',
    'c': 'w',
    'D': 'r',
    'Q': 'i',
    'L': 'v',
}

result = decode(filter(str.isupper, value), translation)
print(result)
Beispiel #27
0
from hacker.decoder import decode, int2

value = '01110101 01110011 01100101 00100000 01110111 01100101 01100100 01101110 01100101 01110011 01100100 01100001 01111001 00100000 01100110 01101111 01110010 00100000 01110100 01101000 01100101 00100000 01100001 01101110 01110011 01110111 01100101 01110010'  # noqa

result = decode(value.split(), int2, chr)
print(result)
Beispiel #28
0
from hacker.decoder import decode, int16

value = '79 65 73 79 65 73 79 65 73 79 65 73 79 65 73 79 0d 0a 65 73 79 65 73 79 65 73 79 65 73 79 65 73 79 65 0d 0a 73 79 45 73 79 65 53 79 65 73 59 45 53 79 65 73 0d 0a 79 65 53 59 65 73 59 65 73 59 65 73 79 45 73 79 0d 0a 65 73 59 45 73 79 45 73 79 45 73 79 65 53 79 65 0d 0a 73 79 45 73 59 65 53 79 65 53 79 65 73 59 65 73 0d 0a 79 65 53 79 45 73 59 65 73 59 65 73 79 45 73 79 0d 0a 65 73 59 65 73 59 45 73 79 45 73 79 65 53 79 65 0d 0a 73 79 45 73 79 45 53 79 65 53 79 65 73 59 65 73 0d 0a 79 65 53 79 65 73 59 65 73 79 45 53 59 65 73 79 0d 0a 65 73 79 65 73 79 65 73 79 65 73 79 65 73 79 65 0d 0a 73 79 65 73 79 65 73 79 65 73 79 65 73 79 65 73 0d 0a'  # noqa


def translate(c):
    """ Turn upper case characters into a visible character, lower case into invisible. Maintain newlines """
    if c == '\n':
        return c
    return 'X' if c.isupper() else ' '


result = decode(value.split(), int16, chr, translate)

print(result)
Beispiel #29
0
from hacker.codes import MORSE
from hacker.decoder import decode

value = '- .... . .- -. ... .-- . .-. .. ... .... --- .- .-. ... .'

result = decode(value.split(), MORSE)
print(result)