Beispiel #1
0
def getRawKeyList(key):
    key_list = []
    key_weight_normalized = dict()
    i = 0
    for c in key:
        weight = alpha.giveAlphabet('u')[c]
        char = KeyChar(c, i, weight)
        i = i + 1
        key_list.append(char)
    return key_list
def encrypt(plain_text,key):
    plain_text = cmt.cleanText(plain_text,False) #remove punctutation and non alpha chars
    plain_text = plain_text.upper() # convert to uppercase
    cipher_text = '' #initalize empty string
    
    alf = alpha.giveAlphabet('u') #return a A-Z list with numbered indicies
    # if key is not in list format
    newMapping = []
    for k in key:
        newMapping.append(k) # create a new mapping based on the key

    #iterate over the entire plain text
    for p in plain_text:
        # replace letter in plain text, using the letter mapping in the key
        cipher_text += newMapping[ alf[p]] 

    return cipher_text
Beispiel #3
0
def getD(putative_text):
    #putative_text=putative_text.upper()
    aNums = alpha.giveAlphabet('u')
    # initialize the D matrix
    D = np.zeros((26, 26))
    # scan the text and update frequency
    for i in range(0, len(putative_text) - 1):
        letter1 = putative_text[i]
        letter2 = putative_text[i + 1]
        #print letter1,letter2
        D[aNums[letter1],
          aNums[letter2]] = D[aNums[letter1], aNums[letter2]] + 1
        #print D[aNums[letter1],aNums[lette r2]]

    fx = np.vectorize(normalizeMat)
    result_array = fx(D, len(putative_text))
    return result_array
def decrypt(cipher_text,key):
    alf = alpha.giveAlphabet('u') # Alphabet to number list (ie. A=0, B=1)
    regAlpha = alpha.giveAlphaList('u') # Number to alphabet list (ie. 0=A, 1=B)
   
    # make the key indexible to map to the actual alphabet
    key_dict = dict()
    i = 0
    for k in key:
        key_dict[k] = i
        i = i+1
    
    # decrypt plain text using the key to alphabet mapping
    plain_text = '' 
    #iterate over entire plaintext
    for c in cipher_text:
        # map the key position to the position in the normal alphabet
        plain_text += regAlpha[key_dict[c]]

    return plain_text
def genKeyBasedOnLetterFreq(letterFreqList):
    # ordered list of most common english letters
    ordered_list = ['E', 'T', 'A', 'O', 'I', 'N', 'S', 'H', 'R', 'D', 'L', 'C', 'U', 'M', 'W', 'F', 'G', 'Y', 'P', 'B', 'V', 'K', 'J', 'X', 'Q', 'Z']

    #sort our letter frequency analysis so we get the most common first
    sorted_list = sorted(letterFreqList,key=lambda x: x[1],reverse=True)
 
    alf = alpha.giveAlphabet('u')
    # generate a blank key so we can index the positions
    key = []
    for i in range(0,26):
        key.append('')

    # fill in our key based off the most common frequency words
    i = 0
    for o in ordered_list:
        key[alf[o]] = sorted_list[i][0]
        i=i+1

    return key