0

I am trying to encrypt a file using RSA algo in python for that i have stored all the possible values that can be written in a normal text file. like this

SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'

now i have a text file which i want to encrypt. It contains only a single line; "I am a boy" (without these quote). But when i am trying to encrypt the file its showing that: "the SYMBOLS does not have the character", which is a message that will be delivered by the program if the character does not match. here's the code where i have declared the SYMBOLS and open the text file:

 SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?'

 def main():
 # Runs a test that encrypts a message to a file or decrypts a message
 # from a file.
 filename = 'encrypted_file.txt' # The file to write to/read from.
 mode = 'encrypt' # Set to either 'encrypt' or 'decrypt'.
 if mode == 'encrypt':
     message1 = open('afile.txt', 'r') #open the file which will be encrypted
     message = str((message1.read())
     print(message)

I think that i am making a mistake in opening the text file, as for this script only the contains will be needed as str, but i don't know how to do it. Looking forward to some si=uggestions. Thank you.

Sandipan
  • 683
  • 8
  • 25

1 Answers1

-1

Try the following code following code to read the content from the file. Error message seems irrelevant to code as you are not using SYMBOLS anywhere.

in message = str(message1.read()) you have unmatched opening and closing bracket.

SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?'

def main():
    # Runs a test that encrypts a message to a file or decrypts a message
    # from a file.
    filename = 'encrypted_file.txt' # The file to write to/read from.
    mode = 'encrypt' # Set to either 'encrypt' or 'decrypt'.
    if mode == 'encrypt':
        message1 = open(filename, 'r') #open the file which will be encrypted
        message = str(message1.read())
        print(message)

main()
Gr8ayu
  • 36
  • 4