def e_SDES_CBC(plaintext, key): undefined = utilities.get_undefined(plaintext, utilities.get_B6Code()) text = utilities.remove_undefined(plaintext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) while len(blocks[-1]) != size: blocks[-1] += 'Q' IV = get_IV() ciphertext = '' for block in blocks: cipher_block = '' for b in block: cipher_block += encode_B6(b) cipher_block = utilities.xor(cipher_block, IV) for i in range(int(get_SDES_value('rounds'))): cipher_block = feistel(cipher_block, get_subKey(key, i + 1)) cipher_block = cipher_block[ int(len(cipher_block) / 2):] + cipher_block[:int(len(cipher_block) / 2)] b_blocks = utilities.text_to_blocks(cipher_block, 6) for b_b in b_blocks: ciphertext += decode_B6(b_b) IV = cipher_block ciphertext = utilities.insert_undefinedList(ciphertext, undefined) return ciphertext
def d_SDES_ECB(ciphertext, key): # your code here if not isinstance(ciphertext, str) or len(ciphertext) == 0: print('Error(d_SDES_ECB): Invalid input', end='') return '' if get_SDES_value('encoding_type') == '' or get_SDES_value( 'block_size') == '' or get_SDES_value('key_size') == '': print('Error(d_SDES_ECB): Invalid configuration', end='') return '' if len(key) == 0: if get_SDES_value('p') == '' or get_SDES_value( 'q') == '' or get_SDES_value('key_size') == '': print('Error(d_SDES_ECB): Invalid key', end='') return '' key = generate_key_SDES() if not utilities.is_binary(key): print('Error(d_SDES_ECB): Invalid key', end='') return '' if len(key) != int(get_SDES_value('key_size')): print('Error(d_SDES_ECB): Invalid key', end='') return '' undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code()) text = utilities.remove_undefined(ciphertext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) plaintext = '' for block in blocks: plain_block = '' for b in block: plain_block += encode_B6(b) rounds = int(get_SDES_value('rounds')) for i in range(rounds): plain_block = feistel(plain_block, get_subKey(key, rounds - i)) plain_block = plain_block[int( len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)] b_blocks = utilities.text_to_blocks(plain_block, 6) for b_b in b_blocks: plaintext += decode_B6(b_b) while plaintext[-1] == 'Q': plaintext = plaintext[:-1] plaintext = utilities.insert_undefinedList(plaintext, undefined) return plaintext
def e_SDES_ECB(plaintext, key): # your code here if not isinstance(plaintext, str) or len(plaintext) == 0: print('Error(e_SDES_ECB): Invalid input', end='') return '' if get_SDES_value('encoding_type') == '' or get_SDES_value( 'block_size') == '' or get_SDES_value('key_size') == '': print('Error(e_SDES_ECB): Invalid configuration', end='') return '' if len(key) == 0: if get_SDES_value('p') == '' or get_SDES_value( 'q') == '' or get_SDES_value('key_size') == '': print('Error(e_SDES_ECB): Invalid key', end='') return '' key = generate_key_SDES() if not utilities.is_binary(key): print('Error(e_SDES_ECB): Invalid key', end='') return '' if len(key) != int(get_SDES_value('key_size')): print('Error(e_SDES_ECB): Invalid key', end='') return '' undefined = utilities.get_undefined(plaintext, utilities.get_B6Code()) text = utilities.remove_undefined(plaintext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) while len(blocks[-1]) != size: blocks[-1] += 'Q' ciphertext = '' for block in blocks: cipher_block = '' for b in block: cipher_block += encode_B6(b) for i in range(int(get_SDES_value('rounds'))): cipher_block = feistel(cipher_block, get_subKey(key, i + 1)) cipher_block = cipher_block[ int(len(cipher_block) / 2):] + cipher_block[:int(len(cipher_block) / 2)] b_blocks = utilities.text_to_blocks(cipher_block, 6) for b_b in b_blocks: ciphertext += decode_B6(b_b) ciphertext = utilities.insert_undefinedList(ciphertext, undefined) return ciphertext
def d_columnarTrans(ciphertext, key): # your code here key_order = get_keyOrder_columnarTrans(key) c = len(key_order) r = int(math.ceil(len(ciphertext) / c)) matrix = utilities.new_matrix(r, c, '') blocks = utilities.text_to_blocks(ciphertext, r) # fill the matrix according to the key_order counter = 0 for i in key_order: for j in range(r): matrix[j][i] = blocks[counter][j] counter += 1 # read the matrix and ignore the 'q' plaintext = '' for i in range(r): for j in range(c): if matrix[i][j] != 'q': plaintext += matrix[i][j] return plaintext
def d_hill(ciphertext, key): # your code here if len(ciphertext) == 0: print('Error(d_hill): invalid ciphertext') return '' new_key = '' if len(key) > 4: new_key += key[:4] elif len(key) == 4: new_key += key else: new_key += key counter = 0 while len(new_key) < 4: new_key += key[counter] counter += 1 baseString = utilities.get_lower() key_matrix = matrix.new_matrix(2, 2, 0) count = 0 for i in range(2): for j in range(2): key_matrix[i][j] = baseString.index(new_key[count].lower()) count += 1 if mod.gcd(matrix.det(key_matrix), 26) != 1: print('Error(d_hill): key is not invertible') return '' inverse_key_matrix = matrix.inverse(key_matrix, 26) plaintext = '' non_alpha = utilities.get_nonalpha(ciphertext) blocks = utilities.text_to_blocks(utilities.remove_nonalpha(ciphertext), 2) for block in blocks: block_m = matrix.new_matrix(2, 1, 0) block_m[0][0] = baseString.index(block[0].lower()) block_m[1][0] = baseString.index(block[1].lower()) result_m = matrix.matrix_mod(matrix.mul(inverse_key_matrix, block_m), 26) plaintext += baseString[result_m[0][0]].lower() plaintext += baseString[result_m[1][0]].lower() plaintext = utilities.insert_nonalpha(plaintext, non_alpha) while plaintext[-1] == 'q': plaintext = plaintext[:-1] return plaintext
def cryptanalysis_vigenere(ciphertext): # yoru code here FriedmanL = utilities.getKeyL_friedman(ciphertext) ShiftL = utilities.getKeyL_shift(ciphertext) non_alpha = utilities.get_nonalpha(ciphertext) if ShiftL != 1: # try every possible size start = min(FriedmanL, ShiftL) if start >= 3: start -= 1 end = max(FriedmanL, ShiftL) + 2 modified = utilities.remove_nonalpha(ciphertext) for size in range(start, end): plaintext = '' shiftList = [] blocks = utilities.text_to_blocks(modified, size) baskets = utilities.blocks_to_baskets(blocks) for element in baskets: # get the list of chi_squared for every shift chiList = [round(utilities.get_chiSquared(utilities.d_shift(element, (i, 'l'))), 4) for i in range(26)] shiftList.append(chiList.index(min(chiList))) for i in range(len(baskets)): baskets[i] = utilities.d_shift(baskets[i], (shiftList[i], 'l')) result = utilities.blocks_to_baskets(baskets) for e in result: plaintext += e plaintext = utilities.insert_nonalpha(plaintext, non_alpha) if utilities.is_plaintext(plaintext, 'engmix.txt', 0.90): plain = utilities.remove_nonalpha(plaintext)[:size] key = '' v_square = utilities.get_vigenereSquare() counter = 0 for pChar in plain: pIndex = v_square[0].index(pChar) kIndex = v_square[pIndex].index(blocks[0][counter]) counter += 1 key += v_square[0][kIndex] utilities.text_to_file(plaintext, 'plaintext_Jiayao_Pang_q4.txt') return key, plaintext key = 0 plaintext = '' return key, plaintext
def e_RSA(plaintext, key): # your code here blocks = utilities.text_to_blocks(plaintext, 6) while len(blocks[-1]) != 6: blocks[-1] += 'q' ciphertext = '' for block in blocks: num = encode_mod96(block) y = LRM(num, key[1], key[0]) ciphertext += decode_mod96(y, 8) return ciphertext
def d_SDES_OFB(ciphertext, key): undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code()) text = utilities.remove_undefined(ciphertext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) IV = get_IV() plaintext = '' counter = 0 for block in blocks: temp = '' plain_block = IV rounds = int(get_SDES_value('rounds')) for i in range(rounds): plain_block = feistel(plain_block, get_subKey(key, i + 1)) plain_block = plain_block[int( len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)] IV = plain_block for b in block: temp += encode_B6(b) if counter <= len(blocks) - 2: plain_block = utilities.xor(plain_block, temp) else: plain_block = utilities.xor(temp, plain_block[:len(temp)]) b_blocks = utilities.text_to_blocks(plain_block, 6) for b_b in b_blocks: plaintext += decode_B6(b_b) counter += 1 plaintext = utilities.insert_undefinedList(plaintext, undefined) return plaintext
def d_RSA(ciphertext, key): # your code here blocks = utilities.text_to_blocks(ciphertext, 8) plaintext = '' for block in blocks: c = encode_mod96(block) p = LRM(c, key[1], key[0]) plaintext += decode_mod96(p, 6) while plaintext[-1] == 'q': plaintext = plaintext[:-1] return plaintext
def e_xcrypt(plaintext, key): # your code here ciphertext = '' blocks = utilities.text_to_blocks(plaintext, int(key)) while len(blocks[-1]) != int(key): blocks[-1] += 'q' baskets = utilities.blocks_to_baskets(blocks) for basket in baskets: ciphertext += basket return ciphertext
def d_SDES_CBC(ciphertext, key): undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code()) text = utilities.remove_undefined(ciphertext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) plaintext = '' IV = get_IV() temp = '' for block in blocks: plain_block = '' for b in block: plain_block += encode_B6(b) temp = plain_block rounds = int(get_SDES_value('rounds')) for i in range(rounds): plain_block = feistel(plain_block, get_subKey(key, rounds - i)) plain_block = plain_block[int( len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)] plain_block = utilities.xor(IV, plain_block) b_blocks = utilities.text_to_blocks(plain_block, 6) for b_b in b_blocks: plaintext += decode_B6(b_b) IV = temp while plaintext[-1] == 'Q': plaintext = plaintext[:-1] plaintext = utilities.insert_undefinedList(plaintext, undefined) return plaintext
def d_xcrypt(ciphertext, key): # your code here plaintext = '' size = int(math.ceil(len(ciphertext) / key)) blocks = utilities.text_to_blocks(ciphertext, size) baskets = utilities.blocks_to_baskets(blocks) for basket in baskets: plaintext += basket while plaintext[-1] == 'q': plaintext = plaintext[:-1] return plaintext
def cryptanalysis_vigenere(ciphertext): # yoru code here alphabet = utilities.get_lower() key = '' values=[0] * 26 x = 0 keyLength = utilities.getKeyL_shift(ciphertext) nonalpha = utilities.remove_nonalpha(ciphertext) blocks = utilities.text_to_blocks(nonalpha, keyLength) baskets = utilities.blocks_to_baskets(blocks) for text in baskets: for x in range(26): values[x] = utilities.get_chiSquared(utilities.d_shift(text, (x,'l'))) index = values.index(min(values)) key = key + alphabet[index] plaintext = d_vigenere(ciphertext, key) return key,plaintext
def cryptanalysis_vigenere(ciphertext): # yoru code here alphabet = "abcdefghijklmnopqrstuvwxyz" key = '' values = [0] * 26 x = 0 key_length = utilities.getKeyL_shift(ciphertext) #print(key_length) nonalpha = utilities.remove_nonalpha(ciphertext) blox = utilities.text_to_blocks(nonalpha, key_length) baskits = utilities.blocks_to_baskets(blox) #dictList = utilities.load_dictionary_modified("engmix.txt",key_length) #print(len(dictList)) print("Decrypting....") for text in baskits: for x in range(26): #only 26 characters in alpha values[x] = utilities.get_chiSquared( utilities.d_shift(text, (x, 'l'))) index = values.index(min(values)) key = key + alphabet[index] plaintext = d_vigenere(ciphertext, key) #i = 0 #tupleList = [] #for word in dictList: #print(str(i) + ": " + str(word)) #print(word) #word.strip() #plaintext = d_vigenere(ciphertext, word) #I = utilities.get_indexOfCoin(plaintext) #tupleList.append(tuple((word,I))) #if I >= 0.063 and I <= 0.067: #time2 = time.time() #print("Decryption took: " + str(round(time2-time1)) + " seconds") #print(plaintext) #return word,plaintext #targetIoCIndex = i #targetIVal = I #i = i + 1 #key = tupleList[targetIoCIndex] #key = key[0] #plaintext = d_vigenere(ciphertext,key) #print(plaintext) return key, plaintext
def d_myszkowski(ciphertext, key): # your code here key_order = get_keyOrder_myszkowski(key) c = len(key_order) r = int(math.ceil(len(ciphertext) / c)) matrix = utilities.new_matrix(r, c, '') blocks = utilities.text_to_blocks(ciphertext, r) # combine all the blocks of the same key character to one block for i in range(c): Index = 0 while i in key_order[Index:c]: if Index != 0: Index = key_order.index(i, Index, c) blocks[key_order[Index]] += blocks[key_order[Index] + 1] blocks.pop(key_order[Index] + 1) Index = key_order.index(i, Index, c) Index += 1 # fill the matrix for i in range(c): counter = 0 for j in range(r): Index = 0 while i in key_order[Index:c]: Index = key_order.index(i, Index, c) matrix[j][Index] = blocks[i][counter] Index += 1 counter += 1 # read the matrix and ignore the 'q' plaintext = '' for i in range(r): for j in range(c): if matrix[i][j] != 'q': plaintext += matrix[i][j] return plaintext