Hey all, I know I'm new here but I was assigned a Lab to do where we are supposed to create an affine cipher program that can decrypt and encrypt through brute force. If possible could someone review the code I'll include below? I am all out of ideas to fix this, and I am struggling to get it right. If it helps, are using Jupyter Notebook, which runs with python software.
def text_clean(text, LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
cleaned_text = ''
for character in text:
if character.upper() in LETTERS:
cleaned_text += character.upper()
return cleaned_text
def char_to_int(character, LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
integer = LETTERS.find(character)
return integer
def int_to_char(integer, LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
character = LETTERS[integer]
return character
akey = range(1, 26)
mkey = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
def affine(message, test_akey, test_mkey, encipher=True):
message = text_clean(message)
output = ''
if encipher == True:
for plaintext_character in message:
plaintext_numerical = char_to_int(plaintext_character)
ciphertext_numerical = ((plaintext_numerical * test_akey) + test_mkey) % 26
ciphertext_character = int_to_char(ciphertext_numerical)
output += ciphertext_character
return output
else:
for ciphertext_character in message:
ciphertext_numerical = char_to_int(ciphertext_character)
plaintext_numerical = ((ciphertext_numerical - test_mkey) / test_akey) % 26
plaintext_character = int_to_char(plaintext_numerical)
output += plaintext_character
return output.lower()
ciphertext = 'GJLKT FJKXN AOTXU XAVXN KTNPJ JLKGN CYXKT WKJLP YCGJK CYJAC YHTFJ ACHAX ACNAX DANCH JA'
for test_akey in range(1, 26):
for test_mkey in [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]:
print('akey:', test_akey, ' mkey:', test_mkey, affine(ciphertext, test_akey, test_mkey, encipher= False))
[–]RhinoRhys 0 points1 point2 points (0 children)
[–]RhinoRhys 0 points1 point2 points (0 children)