Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. #-----------------------------------------------------------
  2. # Parameters:   plaintext (string)
  3. #               key (b,r): (int,int)
  4. # Return:       ciphertext (string)
  5. # Description:  break plaintext into blocks of size b
  6. #               rotate each block r times to the left
  7. #-----------------------------------------------------------
  8. def e_blockRotate(plaintext,key):
  9.  
  10.     #change to the one which removes non alpha
  11.     key = adjustKey_blockRotate(key)
  12.     ciphertext = ''
  13.     b = key[0]
  14.     r = key[1]
  15.  
  16.     nonAlpha = get_nonalpha(plaintext)
  17.     plaintext = remove_nonalpha(plaintext)
  18.  
  19.     blocks = text_to_blocks(plaintext, b)
  20.  
  21.     size = len(blocks[-1])
  22.     while size <(b):
  23.         blocks[-1]+='q'
  24.         size+=1
  25.  
  26.     i = 0
  27.     while i<len(blocks):
  28.         ciphertext+=utilities_A2.shift_string(blocks[i], r, 'l')
  29.         i+=1
  30.  
  31.     ciphertext = insert_nonalpha(ciphertext,nonAlpha)
  32.     return ciphertext
  33.  
  34.  
  35. #-----------------------------------------------------------
  36. # Parameters:   ciphertext (string)
  37. #               key (b,r): (int,int)
  38. # Return:       plaintext (string)
  39. # Description:  Decryption using Block Rotate Cipher
  40. #-----------------------------------------------------------
  41. def d_blockRotate(ciphertext,key):
  42.     i = adjustKey_blockRotate(key)
  43.     plaintext = ''
  44.     a = i[0]
  45.     w = i[1]
  46.     alpha = get_nonalpha(ciphertext)
  47.  
  48.     modifiedText = ''
  49.     for char in ciphertext:
  50.         if char.isalpha():
  51.             modifiedText += char
  52.  
  53.     block = text_to_blocks(modifiedText,a)
  54.     r = 0
  55.     while r < len(block):
  56.         plaintext += utilities_A2.shift_string(block[r], w, 'r')
  57.         r += 1
  58.  
  59.     plaintext = insert_nonalpha(plaintext, alpha)
  60.     value = plaintext[-1]
  61.     while(value == 'q'):
  62.         plaintext = plaintext[:len(plaintext)-1]
  63.         value = plaintext[-1]
  64.     return plaintext
  65.  
  66. #-----------------------------------------------------------
  67. # Parameters:   ciphertext (string)
  68. #               b1 (int): starting block size
  69. #               b2 (int): end block size
  70. # Return:       plaintext,key
  71. # Description:  Cryptanalysis of Block Rotate Cipher
  72. #               Returns plaintext and key (r,b)
  73. #               Attempts block sizes from b1 to b2 (inclusive)
  74. #               Prints number of attempts
  75. #-----------------------------------------------------------
  76. def cryptanalysis_blockRotate(ciphertext,b1,b2):
  77.     # your code here
  78.     attempts = 1
  79.     for b in range(b1, b2+1):
  80.         for i in range(b):
  81.             testDecrypt = d_blockRotate(ciphertext, (b, i))
  82.             if utilities_A2.is_plaintext(testDecrypt, "engmix.txt", .7):
  83.                 print("Key found after {:d} attempts".format(attempts))
  84.                 print("Key: ",(b,i))
  85.                 print("Plaintext:",testDecrypt)
  86.                 return testDecrypt, (b, i)
  87.             else:
  88.                 attempts += 1
  89.     print("Block Rotate Cryptanalysis Failed. No Key was found.")
  90.     return "",(0,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement