
I have to calculate plaintext, when I am given ciphertext, n, and e only. I was solving a crypto question but now I am stuck for a long time. replace ( ' \n ', '' ) plaintext2 = affine_decipher ( ciphertext, a, b ) choice_2 = int ( input ( "1.Print to Screen 2.Write to File \n " )) if choice_2 = 1 : print ( "The Decrypted text: \n " ) print ( plaintext2 ) elif choice_2 = 2 : encode_file = input ( "Enter the file to write decoded text (with extension) \n " ) with open ( encode_file, "w" ) as text_file : print ( plaintext2, file = text_file ) else : sys. replace ( ' \n ', '' ) # encipherĬiphertext = affine_encipher ( plaintext, a, b ) choice_1 = int ( input ( "1.Print to Screen 2.Write to File \n " )) if choice_1 = 1 : print ( "The Encrypted text: \n " ) print ( ciphertext ) elif choice_1 = 2 : encode_file = input ( "Enter the file to write encoded text (with extension) \n " ) with open ( encode_file, "w" ) as text_file : print ( ciphertext, file = text_file ) elif choice = 2 : file_name = input ( "Enter the file to decode (with extension) \n " ) a, b = with open ( file_name, 'r' ) as myfile : ciphertext = myfile. Inv_a = - 1 for x in range ( 1, 26 ): if ( a * x ) % 26 = 1 : inv_a = x if inv_a = - 1 : raise ValueError ( "a doesn't have an inverse" ) choice = int ( input ( "Enter 1.Encrypt 2.Decrypt 3.Exit \n " )) if choice = 1 : file_name = input ( "Enter the file to encode (with extension) \n " ) a, b = validate_a ( a ) with open ( file_name, 'r' ) as myfile : plaintext = myfile. isalpha (): plaintext += I2L - b ) ) % 26 ] else : plaintext += c return plaintext def validate_a ( a ): # to find inverse of a isalpha (): ciphertext += I2L * a + b ) % 26 ] else : ciphertext += c return ciphertext def affine_decipher ( ciphertext, a, b ): # decipher


#GET PLAIN TEXT FROM CYPHER TEXT ZIP#
L2I = dict ( zip ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", range ( 26 ))) I2L = dict ( zip ( range ( 26 ), "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )) def affine_encipher ( plaintext, a, b ): # encipherĬiphertext = "" for c in plaintext. # we need 2 helper mappings, from letters to ints and the inverse
#GET PLAIN TEXT FROM CYPHER TEXT CODE#
The below code takes text file as input and outputs a text file or on terminal based on your choice Follow the same steps for decryption but perform below operation on each character of cipher text to obtain plain text.Covert the obtained number back to alphabet character to get cipher text character.Perform the below operation on each character of plaintext to get corresponding cipher text number.Convert plaintext characters to numbers (0-25).if addition is the last operation in encryption then subtraction should be the first in encryption. whenever we use a combination of ciphers we should be sure that each one has an inverse on the other side of the line and they are used in reverse order in encryption and decryption. one key is used with additive cipher while the other is used with multiplicative cipher. Disclaimer : this content is provided for reference and knowledge purpose only, any other use of this material will be with your own riskĪffine cipher is the combination of additive and multiplicative cipher with a pair of keys ( a and b), the two keys are applied one after the other to generate cipher text.
