Here is my encryption program. The decryption program is what is giving me some trouble.
Here is the description of the first part:

# encrypt.py - ENCODE MESSAGE AND WRITE TO FILE
#
# initialize a cipher list to store the numeric value of each character
# input a string of text to encrypt
#
# for each character in the string:
# convert the character to its unicode value
# encode the unicode value into an integer with an encryption formula
# convert the encoded integer to a string
# append the encoded string to the cipher list
#
# for debugging purposes, print the cipher list - make sure data is correct
#
# open an output file to store the encrypted message
# write (print) the cipher list to the file
# close the file



def main():
    cipher = []

    #initialize string message

    message = input("Please enter your message for encryption: ")

    #loops through string message and encrypts 
    for ch in message:
        x = ord(ch)
        x = (2*x)-3
        x = chr(x)
        cipher.append(x)

    #Print cipher from prompt
    #print("Your code message is: ", cipher)

    #open file for writing
    outfile = open("Encryptedmessage.txt","w")

    print (cipher, file=outfile)
main()

Here is the description of the second part:

# decrypt.py - DECODE MESSAGE FROM FILE AND DISPLAY
#
# initialize a message list to contain the decoded message
# open the input file containing the encrypted message
# read the line containing the message from the file into a string
# split the string into a cipher list of individual strings
#
# for each string in the cipher list:
# convert the string to an integer
# decode the integer into its unicode value using the decryption formula
# convert the unicode value to its corresponding character
# append the character to the message list
#
# print the message list
# close the file

Here is what I have for the decryption program:

def main():
    deCipher = []

    infile = open("Encryptedmessage.txt","r")


    for ch in infile:
        x = ord(ch)
        x = (x/2)+3
        x = chr(x)
        deCipher.append(x)

    print (deCipher)

main()

my logic in decrypting is going awry somewhere. I can't figure out how to make the contents of the file available to iterate through for decrytion.
Any hints and tips would be appreciated.

I accidentally posted this in community center as well.

Recommended Answers

All 5 Replies

Try file modes "wb" and "rb"
Your algorithm will give you non-text characters.

Let me redo this, because I changed a bit.
Here is the encrypt.py program:

def main():
    cipher = []
    #initialize string message
    message=input('Please enter your message for encryption: ')

    #loops through string message and encrypts 
    for ch in message:
        x = ord(ch)
        x = (2*x)-3
        x = chr(x)
        cipher.append(x)
    #Print cipher from prompt
    #print("Your code message is: ", cipher)
    #open file for writing
    outFile = open("Encryptedmessage.txt","w")
    print(cipher, file=outFile)
main()

..And here is my decryption file so far, along with errors.
I'm including the instructions as comments at the top for clarity:

# decrypt.py - DECODE MESSAGE FROM FILE AND DISPLAY
#
# initialize a message list to contain the decoded message
# open the input file containing the encrypted message
# read the line containing the message from the file into a string
# split the string into a cipher list of individual strings
#
# for each string in the cipher list:
# convert the string to an integer
# decode the integer into its unicode value using the decryption formula
# convert the unicode value to its corresponding character
# append the character to the message list
#
# print the message list
# close the file

def main():
    deCipher=[]

    infile = open("Encryptedmessage.txt","r")
    with open('Encryptedmessage.txt') as f:
        lines=f.readlines()
    secMess=str.split

    for ch in secMess:
        x=ord(ch)
        x=(x//2)+3
        x=chr(x)
        deCipher.append(x)
        print(deCipher)



main()

I'm not really understanding your post's instructions HiHe. I appreciate any and all direction on this.

Oh, and here are my errors:

Traceback (most recent call last):
  File "G:/Python34/decrypt.py", line 34, in <module>
    main()
  File "G:/Python34/decrypt.py", line 25, in main
    for ch in secMess:
TypeError: 'method_descriptor' object is not iterable

Anyone?

I got a solution to this. I am posting for referencing purposes.

Encrypt.py:

# encrypt.py - ENCODE MESSAGE AND WRITE TO FILE
#
# initialize a cipher list to store the numeric value of each character
# input a string of text to encrypt
#
# for each character in the string:
# convert the character to its unicode value
# encode the unicode value into an integer with an encryption formula
# convert the encoded integer to a string
# append the encoded string to the cipher list
#
# for debugging purposes, print the cipher list - make sure data is correct
#
# open an output file to store the encrypted message
# write (print) the cipher list to the file
# close the file
def main():
    cipher = []
    #initialize string message
    message=input('Please enter your message for encryption: ')

    #loops through string message and encrypts 
    for ch in message:
        x = ord(ch)
        x = (2*x)-3
        x = chr(x)
        cipher.append(x)
    #Print cipher from prompt
    print("Your code message is: ",cipher)
    #open file for writing
    outFile = open("Encryptedmessage.txt","w")
    print(''.join(cipher), file=outFile)
main()

Decrypt.py:

def main():
    deCipher=[]

infile = open("Encryptedmessage.txt","r")
with open('Encryptedmessage.txt') as f:
    lines=f.readlines()
secMess=lines[0].rstrip()

for ch in secMess:
    y=ord(ch)
    y=(y+3)//2
    y=chr(y)
    deCipher.append(y)
print(''.join(deCipher))

main()

I received more of an answer on this than direction. I have to break it down now so I can understand why it wasn't working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.