Пример #1
0
def match_gtf_9th(tmpfiles, outfile, strmatch, optstrand="None"):
    """
    Matches string to the 9th field of GTF and an optional strand that defaults to None;
    if the pattern is found and the provided strand match then the line is excluded

    Parameters
    ----------
    tmpfiles : str
        GTF files

    outfile : str
        gzipped output GTF file

    strmatch : list
        List of strings to match in the 9th field of the GTF. Must be list

    optstrand : str
        String to match to the strand. Default is None
    """
    regex_strmatch = re.compile(r'|'.join(strmatch))

    with gzip.open(outfile, 'wt') as fout:
        for tmpfn in tmpfiles:
            with openfile(tmpfn, 'rt') as tmp:
                for line in tmp:
                    if line.startswith("#"):
                        fout.write(line)
                    else:
                        toks = line.split('\t')
                        if not (regex_strmatch.search(toks[8]) != None
                                and toks[6] == optstrand):
                            fout.write(line)
Пример #2
0
def main():
    count = 0
    list_of_strings = common.openfile('cryp4_input_file')
    for string in list_of_strings:
        count += 1
        string = re.sub('\n', '', string)

        plaintext = utility.xor_with_all_ascii(string)
        is_ascii = utility.check_if_all_ascii(plaintext)

        print '\n============ ' + str(
            count) + ' ---- ' + string + ' ===========\n'
        for key in is_ascii.keys():
            print key + ' ----- ' + is_ascii[key]
Пример #3
0
def convert_fasta_chroms(tmpfiles, outfile, conv_table):
    """
    Convert chrom names in fasta file according to conversion table.

    Parameters
    ----------
    tmpfiles : str
        fasta files to look through

    outfile : str
        gzipped output fasta file

    conv_table : str
        Lookup table file for the chromosome name conversion. Uses pandas to
        read lookup table, so it can be file://, a path relative to the
        snakefile, or an http://, https://, or ftp:// URL.
    """

    lookup = pd.read_csv(conv_table, sep='\t', header=None,
                         names=('a', 'b')).set_index('a')['b'].to_dict()

    with gzip.open(outfile, 'wt') as fout:
        for tmpfn in tmpfiles:
            with openfile(tmpfn, 'rt') as tmp:
                for line in tmp:
                    if line.startswith(">"):
                        line = line.rstrip("\n")
                        toks = line.split(' ')
                        chrom = toks[0].lstrip(">")
                        chrom = chrom.rstrip("\n")
                        if chrom in lookup.keys():
                            toks[0] = ">" + lookup[chrom]
                            line = ' '.join(toks) + "\n"
                        else:
                            raise ValueError(
                                'Chromosome "{chrom}" not found in conversion table '
                                '"{conv_table}"'.format(chrom=chrom,
                                                        conv_table=conv_table))
                    fout.write(line)
Пример #4
0
def main():
    list_of_strings = common.openfile('cryp8_input_file')
    ecb_blocksize = 16
    are_strings_hex = 1
    key = block.generate_random_string(ecb_blocksize)

    #Get 16 byte blocks from every string separated by an _
    l1 = [
        utility.get_ecb_blocks(string, ecb_blocksize, are_strings_hex)[:-1]
        for string in list_of_strings
    ]
    for count, ciphertext in enumerate(l1, start=1):
        encrypted_blocks = []
        blocks = ciphertext.split('_')

        #Pass the plaintext and encrypted blocks to the detection function, not the raw string
        is_aes_mode_ecb = utility.detect_ecb(blocks)
        if is_aes_mode_ecb == 1:
            print "String %d that is %s is ECB encrypted" % (
                count, ciphertext.replace("_", ""))
        else:
            continue
Пример #5
0
- Now think of an attacker who has ciphertext, can choose offset and newtext but NOT the key, nonce or counter. This attacker must be able to recover plaintext

Questions:
- What does "but hold on to it" mean for the random key I am not supposed to know?
    - Internally the program should use the key for decryption, but attacker doesn't have this

- Should I be using random nonces during encryption or does it not matter, since attacker won't have it anyway.
    - Doesn't matter

- If nonces are random per block, isn't that the correct way to implement CTR? Why is this breakable?
    - nonces are generated per-message, not per-block. if you generate them per block you have to transmit a list of nonces that's as long as your original message
'''

if __name__ == "__main__":
    filename = '25.txt'
    content = common.openfile(filename)

    key = '71e6efcfb44e362b6e14f7abbecf5503'
    nonce = '0' * 8
    enc_string = block.ctr_encrypt_string(''.join(content), key, nonce)

    plaintext = ''
    for offset in range(0, len(enc_string)):
        for guess in range(0, 127):
            t1 = block.decrypt_ctr_byte(enc_string[offset], offset, chr(guess))
            if t1 is not None:
                plaintext += chr(guess)
                break
            else:
                continue
    print plaintext
Пример #6
0
import sys
import os
import re
#Adding directory to the path where Python searches for modules
cmd_folder = os.path.dirname(
    '/home/arvind/Documents/Me/My_Projects/Git/Crypto/modules/')
sys.path.insert(0, cmd_folder)

#Importing common crypto module
import block
import common
import utility

if __name__ == "__main__":
    list_of_strings = common.openfile('c20_strings')
    decoded_str = []
    key = '71e6efcfb44e362b6e14f7abbecf5503'
    nonce = '0' * 8

    for string in list_of_strings:
        decoded_str.append(string.decode("base64"))

    #Use the key and a non-random counter to generate a keystream. This keystream is what is used to encrypt stuff eventually.
    list_encrypted = block.ctr_encrypt(decoded_str, key, nonce)

    #Find smallest encrypted string and truncate all other strings to that length
    lengths = {}
    for l1 in list_encrypted:
        lengths[l1] = len(l1)
    keylen = min(lengths.itervalues())
Пример #7
0
            f1.append(chr(ord(c1dash[i]) ^ i2[i]))

        blockcount -= 1
        #Since we have solved stuff in the reverse order, we need to reverse it here to get the plaintext in the right order
        final = ''.join(reversed(f1)) + final

    return final


if __name__ == "__main__":
    input_file = 'c17_strings'
    blocklen = 16

    #Get list of strings, base64 decode them and add them to an array
    list_of_strings = []
    t1 = common.openfile(input_file)
    for string in t1:
        list_of_strings.append(string.decode('base64'))

    #Pick a random string
    random_index = pick_random_string_index(len(list_of_strings))
    input_str = list_of_strings[random_index]

    #Generate random key to use for all future encryption
    key = '71e6efcfb44e362b6e14f7abbecf5503'

    #Generate a random IV to use in CBC
    iv = '29b28d9f2f56c08a8df1778d7408ba79'
    ivsplit = []
    for count in range(0, len(iv), 2):
        ivsplit.append((iv[count] + iv[count + 1]).decode("hex"))