Example #1
0
def guess_keysize(data, n=3):
    diffs = []
    for i in keysize:
        b1 = data[:i]
        b2 = data[i:i*2]
        b3 = data[i*2:i*3]
        b4 = data[i*3:i*4]
        dist = (hamming(b1, b2) + hamming(b2, b3) + hamming(b3, b4) + hamming(b4, b2)) / 4.
        diff = (dist / float(i), i)
        diffs.append(diff)
    diffs = sorted(diffs)
    print diffs
    return [x[1] for x in diffs[:n]]
Example #2
0
File: bio3.py Project: ilgo/biology
def motif_ham_score(pattern, dnas):
    '''

    '''
    scores = [] 
    k = len(pattern)
    for dna in dnas:
        scores.append(min([util.hamming(pattern, s) for s in util.kmer_gen(dna, k)]))
    return sum(scores)   
Example #3
0
def test1():
	s1 = b"this is a test"
	s2 = b"wokka wokka!!!"
	b1 = util.byte2bit(s1)
	b2 = util.byte2bit(s2)
	print ("-------------------beg test1-------------")
	print (util.hamming(b1, b2))
	print (util.bit2byte(b1).decode())
	print (util.bit2byte(b2).decode())
	print ("-------------------end test1-------------")
Example #4
0
def test1():
    s1 = b"this is a test"
    s2 = b"wokka wokka!!!"
    b1 = util.byte2bit(s1)
    b2 = util.byte2bit(s2)
    print("-------------------beg test1-------------")
    print(util.hamming(b1, b2))
    print(util.bit2byte(b1).decode())
    print(util.bit2byte(b2).decode())
    print("-------------------end test1-------------")
Example #5
0
File: bio1.py Project: ilgo/biology
def approximate_pattern(text, pattern, d):
    '''
    get the starting positions of all patterns with at most d mismatches in the text

    :param str text: the genome to be analyzed
    :param str pattern: the pattern to be detected
    :param int d: mismatches allowed
    :return: a list of starting positions
    :rtype: [`int`, ...]
    '''
    pos = []
    for i, kmer in enumerate(util.kmer_gen(text, len(pattern))):
        if util.hamming(pattern, kmer) <= d:
            pos.append(i)
    return pos
Example #6
0
def guessKeySize(b):
	maxSize = 41 
	keySizeScore = []
	for n in range(2, maxSize):
		score = 0.0
		x = 2*n
		repeat = (len(b)//n)//4

		for i in range(int(repeat)):
			bstr1 = b[(i*x):((i+1)*x)]
			bstr2 = b[((i+2)*x):((i+3)*x)]
			#print n, i, str1, str2
			bit1 = util.byte2bit(bstr1)
			bit2 = util.byte2bit(bstr2)
			score += util.hamming(bit1, bit2)
		keySizeScore.append((n, float(score)/n/repeat))
	return keySizeScore
Example #7
0
def guessKeySize(b):
    maxSize = 41
    keySizeScore = []
    for n in range(2, maxSize):
        score = 0.0
        x = 2 * n
        repeat = (len(b) // n) // 4

        for i in range(int(repeat)):
            bstr1 = b[(i * x):((i + 1) * x)]
            bstr2 = b[((i + 2) * x):((i + 3) * x)]
            #print n, i, str1, str2
            bit1 = util.byte2bit(bstr1)
            bit2 = util.byte2bit(bstr2)
            score += util.hamming(bit1, bit2)
        keySizeScore.append((n, float(score) / n / repeat))
    return keySizeScore
Example #8
0
    def diameter(self, label):
        max_dists = []
        for table in self:

            buckets_containing_label = [
                bucket
                for bucket in self[0].values()
                if any(map(lambda x: x.filter_label(label), bucket.points))
            ]
            max_dist = 0
            for pair in combinations(buckets_containing_label, 2):

                dist = hamming(*map(lambda x: x.address, pair))

                if dist > max_dist:
                    max_dist = dist
            max_dists.append(max_dist)
        return max_dists
Example #9
0
def run(config):
    '''Primary Audiocom functionality.'''

    # Create the preamble to pre-pend to the transmission
    preamble = Preamble(config)

    # Create the sources
    sources = {}
    for i in range(len(config.channels)):
        frequency = config.channels[i]
        source = Source(config, i)
        print("Channel: %d Hz" % frequency)
        print("\n".join(["\t%s" % source]))
        sources[frequency] = source

    # Create a sender for each source, so we can process the bits to
    # get the modulated samples.  We combine all of the modulated
    # samples into a single array by adding them.

    baseband_samples = []
    modulated_samples = []

    for frequency in sources:
        src = sources[frequency]
        sender = Sender(frequency, preamble, config)
        sender.set_source(src)

        modulated_samples = util.add_arrays(sender.modulated_samples(),
                                            modulated_samples)
        baseband_samples = util.add_arrays(sender.bits_to_samples(src.payload),
                                           baseband_samples)
        print("sending %d samples" % len(modulated_samples))

    # Create the channel
    if config.bypass:
        channel = AbstractChannel(config.bypass_noise, config.bypass_h)
    else:
        channel = AudioChannel(config)

    # Transmit and receive data on the channel.  The received samples
    # (samples_rx) have not been processed in any way.
    samples_rx = channel.xmit_and_recv(modulated_samples)
    print('Received', len(samples_rx), 'samples')

    for frequency in config.channels:
        r = Receiver(frequency, preamble, config)
        try:
            # Call the main receiver function.  The returned array of bits
            # EXCLUDES the preamble.
            bits = r.process(samples_rx)

            # Push into a Sink so we can convert back to a useful payload
            # (this step will become more useful when we're transmitting
            # files or images instead of just bit arrays)
            src = sources[frequency]
            sink = Sink(src)
            received_payload = sink.process(bits)
            print("Received %d data bits" % len(received_payload))
            if src.type == Source.TEXT:
                print("Received text was:", sink.received_text)

            if len(received_payload) > 0:
                # Output BER
                hd = util.hamming(received_payload, src.payload)
                ber = float(hd) / len(received_payload)
                print('BER:', ber)
            else:
                print('Could not recover transmission.')

        except Exception as e:
            # In general, this is a fatal exception.  But we'd still like
            # to look at the graphs, so we'll continue with that output
            #            print('*** ERROR: Could not detect preamble. ***')
            print(repr(e))

        if config.graphs == "time":
            graphs.plot_samples(baseband_samples,
                                modulated_samples,
                                r.graph_info.received_samples,
                                stems=False)
        elif config.graphs == "freq":
            graphs.plot_sig_spectrum(modulated_samples,
                                     r.graph_info.demod_samples)
        elif config.graphs == "usr":
            graphs.plot_usr(r.graph_info.demod_samples)
Example #10
0
def pattern_pos(Text, Pattern, d = 0):
    positions = []
    for i in range(len(Text) - len(Pattern) + 1):
        if hamming(Text[i : i + len(Pattern)], Pattern) <= d:
            positions.append(i)
    return positions
Example #11
0
bytes = [ord(ch) for ch in base64.standard_b64decode(data)]
print bytes[:64]

NUM_BLOCKS = int(sys.argv[1])
distances = []
for keysize in range(2, 40):
	blocks = []
	for i in range(NUM_BLOCKS):
		block = bytes[i * keysize:(i + 1) * keysize]
		blocks.append(block)

	total = 0
	count = 0
	for block1, block2 in itertools.combinations(blocks, 2):
		distance = hamming(block1, block2)
		total += distance / float(keysize)
		count += 1

	avg = total / count
	# print keysize, avg
	distances.append((avg, keysize))

distances.sort()
distances = distances[:3]
print distances

for distance, keysize in distances[:1]:
	print keysize
	blocks = []
	start = 0
Example #12
0
def run(config):
    '''Primary Audiocom functionality.'''

    # Create the preamble to pre-pend to the transmission
    preamble = Preamble(config)

    # Create the sources
    sources = {}
    for i in range(len(config.channels)):
        frequency = config.channels[i]
        source = Source(config, i)
        print "Channel: %d Hz" % frequency
        print "\n".join(["\t%s" % source])
        sources[frequency] = source

    # Create a sender for each source, so we can process the bits to
    # get the modulated samples.  We combine all of the modulated
    # samples into a single array by adding them.
    modulated_samples = []
    for frequency in sources:
        src = sources[frequency]
        sender = Sender(frequency, preamble, config)
        sender.set_source(src)
        modulated_samples = util.add_arrays(sender.modulated_samples(), modulated_samples)

    # Create the channel
    if config.bypass:
        channel = AbstractChannel(config.bypass_noise, config.bypass_h, config.bypass_lag)
    else:
        channel = AudioChannel(config)

    # Transmit and receive data on the channel.  The received samples
    # (samples_rx) have not been processed in any way.
    samples_rx = channel.xmit_and_recv(modulated_samples)
    print 'Received', len(samples_rx), 'samples'

    for frequency in config.channels:
        r = Receiver(frequency, preamble, config)
        try:
            # Call the main receiver function.  The returned array of bits
            # EXCLUDES the preamble.
            bits  = r.process(samples_rx)

            # Push into a Sink so we can convert back to a useful payload
            # (this step will become more useful when we're transmitting
            # files or images instead of just bit arrays)
            src = sources[frequency]
            sink = Sink(src)
            received_payload = sink.process(bits)
            print "Received %d data bits" % len(received_payload)
            if src.type == Source.TEXT:
                print "Received text was:", sink.received_text
 
            if len(received_payload) > 0:
                # Output BER
                hd = util.hamming(received_payload, src.payload)
                ber = float(hd)/len(received_payload)
                print 'BER:', ber
            else:
                print 'Could not recover transmission.'

        except Exception as e:
            # In general, this is a fatal exception.  But we'd still like
            # to look at the graphs, so we'll continue with that output
            print '*** ERROR: Could not detect preamble. ***'
            print repr(e)

        # Plot graphs if necessary
        if config.graphs:
            try:
                len_demod = config.spb * (len(received_payload) + preamble.preamble_data_len())
            except:
                # If we didn't receive the payload, make a reasonable guess for the number of bits
                # (won't work for filetype, where n_bits changes)
                len_demod = config.spb * (config.n_bits + preamble.preamble_data_len())

            if config.demod_type == Receiver.QUADRATURE:
                filtered = r.graph_info.demod_samples
                graphs.plot_sig_spectrum(samples_rx, filtered, "received samples", "filtered samples")
            elif config.src_type == Source.U:
                demod_samples = r.graph_info.demod_samples
                plotrange = preamble.preamble_data_len()*config.spb
                graphs.plot_samples(demod_samples[plotrange:len_demod], 'unit-step response', show=True)
            else:
                graphs.plot_graphs(r.graph_info.demod_samples[:len_demod], r.graph_info.hist_samples[:len_demod], config.spb, preamble)
Example #13
0
from util import hamming, single_xor, encrypt_repeating_key_xor

print hamming('this is a test', 'wokka wokka!!!')
keysize = range(2, 41)

# return a list of the top n most likely keysizes
def guess_keysize(data, n=3):
    diffs = []
    for i in keysize:
        b1 = data[:i]
        b2 = data[i:i*2]
        b3 = data[i*2:i*3]
        b4 = data[i*3:i*4]
        dist = (hamming(b1, b2) + hamming(b2, b3) + hamming(b3, b4) + hamming(b4, b2)) / 4.
        diff = (dist / float(i), i)
        diffs.append(diff)
    diffs = sorted(diffs)
    print diffs
    return [x[1] for x in diffs[:n]]

def transpose(data, keysize):
    blocks = []
    for j in range(keysize):
        b = []
        for i in range(j, len(data), keysize):
            b.append(data[i])
        blocks.append(''.join(b))
    return blocks

def solve_block(block):
    letters = set(map(chr, range(65, 123)))
Example #14
0
def pattern_pos(Text, Pattern, d=0):
    positions = []
    for i in range(len(Text) - len(Pattern) + 1):
        if hamming(Text[i:i + len(Pattern)], Pattern) <= d:
            positions.append(i)
    return positions
Example #15
0
File: bio1.py Project: ilgo/biology
def pattern_count(text, pattern, d=0):
    return sum([util.hamming(kmer, pattern) <= d for kmer in util.kmer_gen(text, len(pattern))])
Example #16
0
def run(config):
    '''Primary Audiocom functionality.'''

    # Create the preamble to pre-pend to the transmission
    preamble = Preamble(config)

    # Create a source
    source = Source(config)
    frequency = config.channel
    print "Channel: %d Hz" % frequency
    print "\n".join(["\t%s" % source])

    # Create the Sender for this Source.  Process the bits to get
    # modulated samples.
    sender = Sender(frequency, preamble, config)
    sender.set_source(source)
    modulated_samples = sender.modulated_samples()

    # Create the channel
    if config.bypass:
        channel = AbstractChannel(config.bypass_noise, config.bypass_h)
    else:
        channel = AudioChannel(config)

    # Transmit and receive data on the channel.  The received samples
    # (samples_rx) have not been processed in any way.
    samples_rx = channel.xmit_and_recv(modulated_samples)
    print 'Received', len(samples_rx), 'samples'

    r = Receiver(frequency, preamble, config)
    try:
        # Call the main receiver function.  The returned array of bits
        # EXCLUDES the preamble.
        bits  = r.process(samples_rx)

        # Push into a Sink so we can convert back to a useful payload
        # (this step will become more useful when we're transmitting
        # files or images instead of just bit arrays)
        sink = Sink(source)
        received_payload = sink.process(bits)
        print "Received %d data bits" % len(received_payload)
 
        if len(received_payload) > 0:
            # Output BER
            hd = util.hamming(received_payload, source.payload)
            ber = float(hd)/len(received_payload)
            print 'BER:', ber
        else:
            print 'Could not recover transmission.'

    except Exception as e:
        # In general, this is a fatal exception.  But we'd still like
        # to look at the graphs, so we'll continue with that output
        print '*** ERROR: Could not detect preamble. ***'

    # Plot graphs if necessary
    if config.graphs:

        try:
            len_demod = config.spb * (len(received_payload) + preamble.preamble_data_len())
        except:
            # If we didn't receive the payload, make a reasonable guess for the number of bits
            len_demod = config.spb * (config.n_bits + preamble.preamble_data_len())

        if config.src_type == Source.U:
            demod_samples = r.graph_info.demod_samples
            plotrange = preamble.preamble_data_len()*config.spb
            graphs.plot_samples(demod_samples[plotrange:len_demod], 'unit-step response', show=True)

        else:
            graphs.plot_graphs(r.graph_info.demod_samples[:len_demod], r.graph_info.hist_samples[:len_demod], config.spb, preamble)