human
Program to perform a letter frequency attack on a monoalphabetic substitution cipher | Function to decrypt a monoalphabetic substitution cipher using the letter frequency attack.
Stores final 5 possible deciphered plaintext.
Store the frequency of each letter in cipher text.
Stores the frequency of each letter in cipher text in descending order.
Store which alphabet is used already.
Traverse the string S.
Copy the frequency array.
Stores the string formed from concatanating the english letters in the decreasing frequency in the english language.
Sort the array in descending order.
Itearate over the range [ 0 , 5 ].
Iterate over the range [ 0 , 26 ].
Store the numerical equivalent of letter at ith index of array letter_frequency.
Calculate the probable shift used in monoalphabetic cipher.
Temporary string to generate one plaintext at a time.
Generate the probable ith plaintext string using the shift calculated above.
Insert whitespaces as it is.
Shift the kth letter of the cipher by x.
Add the kth calculated / shifted letter to temporary string.
Print the generated 5 possible plaintexts.
Given string.
Function Call"
Solution in Python:
 Give me a code to program to perform a letter frequency attack on a monoalphabetic substitution cipher and the other steps required?
assistant
#Here is code to program to perform a letter frequency attack on a monoalphabetic substitution cipher and the other steps required.
 def printString ( S , N ) :
	plaintext = [ None ] * 5
	freq = [ 0 ] * 26
	freqSorted = [ None ] * 26
	used = [ 0 ] * 26
	for i in range ( N ) :
		if S [ i ] != ' ▁ ' :
			freq [ ord ( S [ i ] ) - 65 ] += 1
	for i in range ( 26 ) :
		freqSorted [ i ] = freq [ i ]
	T = " ETAOINSHRDLCUMWFGYPBVKJXQZ "
	freqSorted . sort ( reverse = True )
	for i in range ( 5 ) :
		ch = - 1
		for j in range ( 26 ) :
			if freqSorted [ i ] == freq [ j ] and used [ j ] == 0 :
				used [ j ] = 1
				ch = j
				break
		if ch == - 1 :
			break
		x = ord ( T [ i ] ) - 65
		x = x - ch
		curr = " "
		for k in range ( N ) :
			if S [ k ] == ' ▁ ' :
				curr += " ▁ "
				continue
			y = ord ( S [ k ] ) - 65
			y += x
			if y < 0 :
				y += 26
			if y > 25 :
				y -= 26
			curr += chr ( y + 65 )
		plaintext [ i ] = curr
	for i in range ( 5 ) :
		print ( plaintext [ i ] )
S = " B ▁ TJNQMF ▁ NFTTBHF "
N = len ( S )
printString ( S , N )