Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-----------------------------------------------------------
- # Parameters: plaintext (string)
- # key (b,r): (int,int)
- # Return: ciphertext (string)
- # Description: break plaintext into blocks of size b
- # rotate each block r times to the left
- #-----------------------------------------------------------
- def e_blockRotate(plaintext,key):
- #change to the one which removes non alpha
- key = adjustKey_blockRotate(key)
- ciphertext = ''
- b = key[0]
- r = key[1]
- nonAlpha = get_nonalpha(plaintext)
- plaintext = remove_nonalpha(plaintext)
- blocks = text_to_blocks(plaintext, b)
- size = len(blocks[-1])
- while size <(b):
- blocks[-1]+='q'
- size+=1
- i = 0
- while i<len(blocks):
- ciphertext+=utilities_A2.shift_string(blocks[i], r, 'l')
- i+=1
- ciphertext = insert_nonalpha(ciphertext,nonAlpha)
- return ciphertext
- #-----------------------------------------------------------
- # Parameters: ciphertext (string)
- # key (b,r): (int,int)
- # Return: plaintext (string)
- # Description: Decryption using Block Rotate Cipher
- #-----------------------------------------------------------
- def d_blockRotate(ciphertext,key):
- i = adjustKey_blockRotate(key)
- plaintext = ''
- a = i[0]
- w = i[1]
- alpha = get_nonalpha(ciphertext)
- modifiedText = ''
- for char in ciphertext:
- if char.isalpha():
- modifiedText += char
- block = text_to_blocks(modifiedText,a)
- r = 0
- while r < len(block):
- plaintext += utilities_A2.shift_string(block[r], w, 'r')
- r += 1
- plaintext = insert_nonalpha(plaintext, alpha)
- value = plaintext[-1]
- while(value == 'q'):
- plaintext = plaintext[:len(plaintext)-1]
- value = plaintext[-1]
- return plaintext
- #-----------------------------------------------------------
- # Parameters: ciphertext (string)
- # b1 (int): starting block size
- # b2 (int): end block size
- # Return: plaintext,key
- # Description: Cryptanalysis of Block Rotate Cipher
- # Returns plaintext and key (r,b)
- # Attempts block sizes from b1 to b2 (inclusive)
- # Prints number of attempts
- #-----------------------------------------------------------
- def cryptanalysis_blockRotate(ciphertext,b1,b2):
- # your code here
- attempts = 1
- for b in range(b1, b2+1):
- for i in range(b):
- testDecrypt = d_blockRotate(ciphertext, (b, i))
- if utilities_A2.is_plaintext(testDecrypt, "engmix.txt", .7):
- print("Key found after {:d} attempts".format(attempts))
- print("Key: ",(b,i))
- print("Plaintext:",testDecrypt)
- return testDecrypt, (b, i)
- else:
- attempts += 1
- print("Block Rotate Cryptanalysis Failed. No Key was found.")
- return "",(0,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement