def padding(artificial_payload, raw_payload):

    padding = ""

    # Get frequency of raw_payload and artificial profile payload
    artificial_frequency = frequency(artificial_payload)
    raw_payload_frequency = frequency(raw_payload)

    # To simplify padding, you only need to find the maximum frequency difference for each byte in raw_payload
    # and artificial_payload, and pad that byte to the end of the raw_payload.
    # Note: only consider the difference when artificial profile has higher frequency.

    # Your code here ...
    max_freq = 0
    max_freq_char = chr(0)  #default
    for i in artificial_frequency:
        if i in raw_payload_frequency:
            a_freq = artificial_frequency[i]
            if max_freq == 0:
                max_freq = a_freq
                max_freq_char = i
            r_freq = raw_payload_frequency[i]

            if a_freq > r_freq:
                diff = a_freq - r_freq
                diff_char = i

                if diff > max_freq:
                    max_freq = diff
                    max_freq_char = diff_char

    # Depending on the difference, call raw_payload.append
    raw_payload.append(max_freq_char)
示例#2
0
def getSubstitutionTable(artificial_payload, attack_payload):
    # You will need to generate a substitution table which can be used to encrypt the attack body by replacing the most frequent byte in attack body by the most frequent byte in artificial profile one by one

    # Note that the frequency for each byte is provided below in dictionay format. Please check frequency.py for more details
    artificial_frequency = frequency(artificial_payload)
    attack_frequency = frequency(attack_payload)

    sorted_artificial_frequency = sorting(artificial_frequency)
    sorted_attack_frequency = sorting(attack_frequency)

    substitution_table = []

    # Your code here ...

    # create an alphabet.  This will ensure no duplicates later on.
    alphabet = []
    for i in range(255):
        alphabet.append(chr(i))
   
    # loop control variables.
    i = 0
    j = 0
    
    while i < len(sorted_attack_frequency) and j < len(sorted_artificial_frequency):
        # for each character in the attack payload, we will find one or several characters in the artificial payload whose frequency probability 
        # will add up to less than the probability of the attack character.  Our substitute method will choose one of those artificial payload
        # characters to replace the attack character at random.
        probsum = 0
        # as long as the probability is less than the given probability, add a new char to the sub table.  add it's probabliity, and remove it from the alpahbet.
        while probsum < sorted_attack_frequency[i][1] and j < len(sorted_artificial_frequency):
            substitution_table.append((sorted_attack_frequency[i][0],sorted_artificial_frequency[j][0]))
            probsum = probsum + sorted_artificial_frequency[j][1]
            alphabet.remove(sorted_artificial_frequency[j][0])
            j = j + 1
        i = i + 1
      
    # if we have chars left in our attack frequency, they have probability less than the probabilities in our artificial list. so we'll replace them
    # with random characters from our alphabet (removing them as they are used.)
    
    import random
    
    while i < len(sorted_attack_frequency):
        k = alphabet[random.randint(0,len(alphabet)-1)]
        substitution_table.append((sorted_attack_frequency[i][0],k))
        alphabet.remove(k)
        i = i + 1
      
    # for any chars in our alphabet, they'll substitute for themselves, one-to-one.
    
    while len(alphabet) > 0:
        substitution_table.append((alphabet[0],alphabet[0]))
        alphabet.remove(alphabet[0])
    
    # You may implement substitution table in your way. Just make sure it can be used in substitute(attack_payload, subsitution_table)
    return substitution_table
示例#3
0
def padding(artificial_payload, raw_payload):
    padding = ""
    # Get frequency of raw_payload and artificial profile payload
    artificial_frequency = frequency(artificial_payload)
    raw_payload_frequency = frequency(raw_payload)

    # To simplify padding, you only need to find the maximum frequency difference for each byte in raw_payload and artificial_payload, and pad that byte at the end of the raw_payload. Note: only consider the differences when artificial profile has higher frequency.

    # Depending upon the difference, call raw_payload.append

    # Your code here ...
    print("khgog")
示例#4
0
def getSubstitutionTable(artificial_payload, attack_payload):
    substitution_table = {}
    # You will need to generate a substitution table which can be used to encrypt the attack
    # body by replacing the most frequent byte in attack body by the most frequent byte in
    # artificial profile one by one

    # Note that the frequency for each byte is provided below in dictionay format.
    # Please check frequency.py for more details
    # print("in here")
    artificial_frequency = frequency(artificial_payload)
    attack_frequency = frequency(attack_payload)

    sorted_artificial_frequency = sorting(artificial_frequency)
    sorted_attack_frequency = sorting(attack_frequency)
    # print(sorted_artificial_frequency)
    # print(sorted_attack_frequency)

    # for x in sorted_attack_frequency:
        # print (x)
        # substitution_table[x] = 
    for i in range(len(sorted_attack_frequency)):
        # print (sorted_attack_frequency)
        substitution_table[sorted_attack_frequency[i][0]] = [sorted_artificial_frequency[i]]
    

    # count = 0
    for item in sorted_artificial_frequency[len(sorted_attack_frequency):]:
        max_ratio_frequency = calculateFrequencyWithMaxRatio(sorted_attack_frequency, substitution_table)
        substitution_table[max_ratio_frequency[0]].append(item)
        # print(max_ratio_frequency)
        # print('Adding a new item to the dictionary')
        # print(item)

        # print('doing stuff')
        # count = count + 1
    # print('Count for times sorted artificial was run through')
    # print(count)
    # print('Length of artifical sorted array is... ')
    # print(len(sorted_artificial_frequency))
    # print('Length of attack sorted array is... ')
    # print(len(sorted_attack_frequency))
    # print(substitution_table)
    # Your code here ...

    
    # Make sure your substitution table can be used in
    # substitute(attack_payload, subsitution_table)
    # print(substitution_table)
    return substitution_table
示例#5
0
def getSubstitutionTable(artificial_payload, attack_payload):
    # You will need to generate a substitution table which can be used to encrypt the attack body by replacing the most frequent byte in attack body by the most frequent byte in artificial profile one by one

    # Note that the frequency for each byte is provided below in dictionay format. Please check frequency.py for more details
    artificial_frequency = frequency(artificial_payload)
    attack_frequency = frequency(attack_payload)

    sorted_artificial_frequency = sorting(artificial_frequency)
    sorted_attack_frequency = sorting(attack_frequency)

    # Your code here ...
    substitution_table = {}

    # You may implement substitution table in your way. Just make sure it can be used in substitute(attack_payload, subsitution_table)
    print substitution_table
    return substitution_table
示例#6
0
def decode(file, tab_letter):
    #Ouverture fichier
    f = open(file, "r")
    contenu = f.read().casefold()
    #Traitement du texte

    #Remplie la matrice au endroit ou on est sure de l'association avec tab_connu
    mat_liaison = np.zeros((nb_anglais, nb_anglais))
    for key, value in tab_connu.items():
        mat_liaison[tab_indice[key]][tab_indice[value]] += infini

    #Analyse frequentielle
    tab = frequency(contenu, tab_lettre)
    map_assoc_frequence = {}
    for i in range(nb_anglais):
        map_assoc_frequence[tab_anglais_sorted[i]] = tab[i]
        i += 1

    for key, value in map_assoc_frequence.items():
        mat_liaison[tab_indice[key]][tab_indice[value]] += 10
        mat_liaison[tab_indice[value]][tab_indice[key]] += 10

    #Recopie du message decodé
    decode = ''
    for letter in contenu:
        if (letter.isalpha()):
            if (tab_connu.__contains__(letter)):
                decode += tab_connu[letter]
            else:
                decode += tab_anglais_sorted[tab.index(letter)]
        else:
            decode += letter
    print(decode)
    f.close()
示例#7
0
def padding(artificial_payload, raw_payload):
    padding = ""
    
    # Get frequency of raw_payload and artificial profile payload
    artificial_frequency = frequency(artificial_payload)
    raw_payload_frequency = frequency(raw_payload)


    max = 0
    padding_byte = ''
    #Loop through all the keys and values in the raw_payload_frequency
    #dict. For each key that exists in artificial_payload ,  find the difference in 
    #frequencies. Get the max frequency and append that to raw_payload
    for key,value in raw_payload_frequency.items() :
        if key in artificial_frequency:
            artificial_freq = artificial_frequency[key]
            diff =  value - artificial_freq
            if diff > max: 
                padding_byte = key
                max = diff

    raw_payload.append(padding_byte)
示例#8
0
def getSubstitutionTable(artificial_payload, attack_payload):
    # You will need to generate a substitution table which can be used to encrypt the attack body by replacing
    # the most frequent byte in attack body by the most frequent byte in artificial profile one by one

    # Note that the frequency for each byte is provided below in dictionay format. Please check frequency.py for
    # more details
    artificial_frequency = frequency(artificial_payload)
    attack_frequency = frequency(attack_payload)

    sorted_artificial_frequency = sorting(artificial_frequency)
    sorted_attack_frequency = sorting(attack_frequency)
    # sorted_artificial_frequency = [('b', 0.4), ('a', 0.3), ('c', 0.3)]
    # sorted_attack_frequency = [('p', 0.5), ('q', 0.5)]
    # Your code here ...
    substitution_table = {}
    for i in range(len(sorted_artificial_frequency)):
        att = sorted_attack_frequency[i if i < len(sorted_attack_frequency
                                                   ) else 0]
        att_key = att[0]
        att_freq = att[1]
        reg = sorted_artificial_frequency[i]
        reg_key = reg[0]
        reg_freq = reg[1]
        if att_key not in substitution_table:
            substitution_table[att_key] = [reg]
        else:
            substitution_table[att_key].append(reg)
        sorted_attack_frequency[i if i < len(sorted_attack_frequency
                                             ) else 0] = (att_key,
                                                          att_freq / reg_freq)
        if i >= len(sorted_attack_frequency) - 1:
            sorted_attack_frequency = sorted(sorted_attack_frequency,
                                             key=lambda x: x[1],
                                             reverse=True)

    # You may implement substitution table in your way. Just make sure it can be used in
    # substitute(attack_payload, substitution_table)
    return substitution_table
示例#9
0
def padding(artificial_payload, raw_payload):
    padding = ""
    # Get frequency of raw_payload and artificial profile payload
    artificial_frequency = frequency(artificial_payload)
    raw_payload_frequency = frequency(raw_payload)
    sorted_artifical_frequency = sorting(artificial_frequency)
    sorted_raw_payload_frequency = sorting(raw_payload_frequency)

    # Your code here ...
    diffs = []
    nope = []

    for k in raw_payload:
        if k in artificial_frequency and artificial_payload:
            m = raw_payload_frequency[k]
            n = artificial_frequency[k]
            if n > m:
                diffs.append((k, n - m))
        else:
            nope.append(k)
    if diffs:
        max_diff = sorted(diffs)[0]
        raw_payload.append(max_diff[0])
示例#10
0
def padding(artificial_payload, raw_payload):
    padding = ""
    # Get frequency of raw_payload and artificial profile payload
    artificial_frequency = frequency(artificial_payload)
    raw_payload_frequency = frequency(raw_payload)

    # To simplify padding, you only need to find the maximum frequency difference for each byte in raw_payload and artificial_payload, and pad that byte to the end of the raw_payload. Note: only consider the difference when artificial profile has higher frequency.
    #print raw_payload
    artificial_frequency = sorting(artificial_frequency)
    raw_payload_frequency = sorting(raw_payload_frequency)

    # Your code here ...
    mmax = 0
    char = ''
    for i in range(len(artificial_payload)):
        lookfor = artificial_payload[i]
        artprob = 0
        rawprob = 0
        for artspot in range(len(artificial_frequency)):
            if lookfor == artificial_frequency[artspot][0]:
                artprob = artificial_frequency[artspot][1]
                break
        for rawspot in range(len(raw_payload_frequency)):
            if lookfor == raw_payload_frequency[rawspot][0]:
                rawprob = raw_payload_frequency[rawspot][1]
                break
        if artprob == 0 or rawprob == 0:
            continue
        if mmax < artprob - rawprob:
            mmax = artprob - rawprob
            char = lookfor

    # Depending on the difference, call raw_payload.append

    amounttopad = (len(artificial_payload) - len(raw_payload))
    for i in range(amounttopad):
        raw_payload.append(char)
示例#11
0
def padding(artificial_payload, raw_payload):
    padding = ""

    # Get frequency of raw_payload and artificial profile payload
    artificial_frequency = frequency(artificial_payload)
    raw_payload_frequency = frequency(raw_payload)
    letterOfChoice = ''
    # print(artificial_frequency)
    max_difference = 0
    for normal_letter, normal_frequency in artificial_frequency.items():
        attack_frequency = 0
        # print(normal_letter)
        # print(normal_frequency)
        if normal_letter in raw_payload_frequency:
            attack_frequency = raw_payload_frequency[normal_letter]
        difference = normal_frequency - attack_frequency
        if (difference > max_difference):
            max_difference = difference
            print(difference)
            letterOfChoice = normal_letter

    print('Adding the letter...')
    print(letterOfChoice)
    raw_payload.append(letterOfChoice)
示例#12
0
def decode(file, tab_letter):
    #Ouverture fichier
    f = open(file, "r")
    contenu = f.read().casefold()
    #Traitement du texte

    map_assoc_lettre = {}
    for lettre in tab_lettre:
        map_assoc_lettre[lettre] = []

    for key, value in tab_connu.items():
        map_assoc_lettre[key].append(value)

    #Analyse frequentielle
    tab = frequency(contenu, tab_lettre)
    map_assoc_frequence = {}
    for i in range(nb_anglais):
        map_assoc_frequence[tab_anglais_sorted[i]] = tab[i]
        i += 1

    for key, value in map_assoc_frequence.items():
        map_assoc_lettre[key].append(value)

    print(map_assoc_lettre)
    tab_choisi = [False for i in range(nb_anglais)]
    map_final_assoc = {}
    for key, value in map_assoc_lettre.items():
        if (not (tab_choisi[tab_indice[value[0]]])):
            tab_choisi[tab_indice[value[0]]] = True
            map_final_assoc[key] = value[0]
        else:
            map_final_assoc[key] = value[0]

    print(map_final_assoc)
    #Recopie du message decodé
    decode = ''
    for letter in contenu:
        if (letter.isalpha()):
            decode += map_final_assoc[letter]
        else:
            decode += letter
    print(decode)
    f.close()
示例#13
0
def padding(artificial_payload, raw_payload):
    padding = ""
    # Get frequency of raw_payload and artificial profile payload
    artificial_frequency = frequency(artificial_payload)
    raw_payload_frequency = frequency(raw_payload)
示例#14
0
def getSubstitutionTable(artificial_payload, attack_payload):
    # You will need to generate a substitution table which can be used to encrypt the attack
    # body by replacing the most frequent byte in attack body by the most frequent byte in
    # artificial profile one by one



    # Note that the frequency for each byte is provided below in dictionay format.
    # Please check frequency.py for more details
    artificial_frequency = frequency(artificial_payload)

    attack_frequency = frequency(attack_payload)
    sorted_artificial_frequency = sorting(artificial_frequency)
    sorted_attack_frequency = sorting(attack_frequency)


    #number of distint characters in attack traffic
    attack_len = len(sorted_attack_frequency);

    #number of distincy characters in  normal/artificial
    normal_len = len(sorted_artificial_frequency);


    temp_sub_table = sorted_attack_frequency;
    temp_values = [[] for i in range(attack_len)]  #initialize list of attack_len elements

    for i in range(attack_len):
        temp_values[i].append(sorted_artificial_frequency[i])


    substitution_table = {}
    for i in range(len(temp_sub_table)):
        temp_total = temp_sub_table[i]   #ex ('t',.44) , need to get t and place in new table
        temp_key= temp_total[0];
        temp_val = temp_total[1]
        substitution_table[temp_key]=temp_values[i]



    values_left = normal_len-attack_len;
    #Loop through the remaining values in the artifical payload
    for j in range(values_left):
        temp_list_comparison = {}
        largest_ration = 0
        largest_ration_key = '';
        largest_ration_value = 0;

        for i in range(attack_len):
            #get the original frequency/ divide by new frequency
            original_freq = (sorted_attack_frequency[i])[1]
            original_key =  (sorted_attack_frequency[i])[0]
            total =0;
            #Get the total frequency
            for k in range(len(substitution_table[original_key])):
                total += ((substitution_table[original_key])[k])[1]

            new_freq = total;
            comparison = round(original_freq/new_freq,3);
            if comparison > largest_ration :
                largest_ration_key = original_key
                largest_ration_value = comparison;
                largest_ration = comparison;

            temp_list_comparison[original_key] = comparison;



        substitution_table[largest_ration_key].append(sorted_artificial_frequency[attack_len +j])

    # Make sure your substitution table can be used in
    print(substitution_table)
    #substitute(attack_payload, substitution_table)

    return substitution_table
示例#15
0
def getSubstitutionTable(artificial_payload, attack_payload):
    # You will need to generate a substitution table which can be used to encrypt the attack body by replacing the most frequent byte in attack body by the most frequent byte in artificial profile one by one

    # Note that the frequency for each byte is provided below in dictionay format. Please check frequency.py for more details
    artificial_frequency = frequency(artificial_payload)
    attack_frequency = frequency(attack_payload)

    sorted_artificial_frequency = sorting(artificial_frequency)
    sorted_attack_frequency = sorting(attack_frequency)

    # Your code here ...

    # Create an empty dictionary for the substitute table whose keys are
    # characters and who values are list of tuples.  The list of tuples
    # are characters with floats.
    substitution_table = {}

    # Copy each character into the substitute table keys from the sorted
    # attack frequency data structure.
    for char_tup in sorted_attack_frequency:
        substitution_table[char_tup[0]] = []

    # For the first m characters, and first n, (Using m and n as a tuples here)
    for m, n in zip(sorted_attack_frequency, sorted_artificial_frequency):
        # Map attack characters to normal characters.
        substitution_table[m[0]] = [n]

    print("-----------------------")
    #substitution_table['!'].append(('b',0))
    print(substitution_table)
    # Create counter for size of m to go to size of n
    m_plus_n_cnt = len(substitution_table)

    # Create a temporary table of all of the character left from the
    # artificial table
    chars_left = []

    for cnt in range(m_plus_n_cnt, len(sorted_artificial_frequency)):
        chars_left.append(sorted_artificial_frequency[cnt])

    # For each of the m+nth characters in the artificial table:
    for art_char in chars_left:
        # Hold the largest of the substitute table subsub ratios
        highest_ratio = 0
        # ... and it's character tuple
        highest_ratio_char = ()

        # For each of the characters in the substition table
        for att_char in sorted_attack_frequency:
            sub_sub_total = 0
            sub_ratio = 0

            # For each of the tuples in the substituion table character list
            for subst_char in substitution_table[att_char[0]]:
                sub_sub_total += subst_char[1]

            # Now create the ratio of current subst table char and compare it
            # with the highest. If higher, set as new
            sub_ratio = att_char[1] / sub_sub_total
            print("sub_ratio = " + str(sub_ratio))
            print("highest Ratio = " + str(highest_ratio))

            if sub_ratio > highest_ratio:
                highest_ratio = sub_ratio
                highest_ratio_char = att_char
                print("rat = " + str(sub_ratio))

        substitution_table[highest_ratio_char[0]].append(art_char)
    print("substitution_table = ")
    print(substitution_table)

    # You may implement substitution table in your way. Just make sure it can be used in substitute(attack_payload, subsitution_table)
    return substitution_table