End characters from string being cut off when decrypting (Python) [on hold]











up vote
0
down vote

favorite












Making a program that encrypts a txt file of the users choice and then saving the encrypted version as a separate file. It should also be able to decrypt the a file if the correct 8 character key is entered. The problem I'm having is that when decrypting the file characters from the end of the file seem to be cut out or left off and I can't see why. When decrypting the outcome anything after the "an" of "lance" is left out.



Text File (Pastebin)
Python Code (Pastebin)



from random import randint

def menu(): #Main menu
choice = int(input("Encrypt [1] Decrypt [2] Exit [3]: ")) #Allows choice
print("")

if choice == 1: #Goes to the function for each option
encrypt()
elif choice == 2:
decrypt()
elif choice == 3:
print("Good bye")

def encrypt(): #Encrypt
print("Enter the name of the file you wish to encrypt(with the file
ending)") # Need the file ending to work
targetFile = input("-> ")
print('')
f = open(targetFile, "r+") #Opens the file as 'f'
print(f.read()) #Prints the text in the file

key =
for i in range(0,8):
chrKey = randint(33,126)
newKey = str(chr(chrKey)) #converts ascii number to text
key.append(newKey)
print('')
print("Remember the eight character key or you can't decrypt your file")
print('Your eight character key is:')
print(''.join(key))
print('')

keyTotal = 0
for j in range(0,8):
newValue = ord(key[j])
keyTotal += newValue
offset = keyTotal/8-32
offset = int(offset)

with open(targetFile) as f:
characters = 0
for line in f:
wordslist=line.split()
characters += sum(len(word) for word in wordslist)
characters = int(characters)

cypherText =
with open(targetFile, 'r+') as f:
data = f.read().replace('n','')
for k in range(0,characters):
if data[k] != ' ':
asciiValue = ord(data[k])
asciiValue += offset
if asciiValue > 126:
asciiValue -= 94
cypherText.append(str(chr(asciiValue)))
else:
cypherText.append(' ')
cypherText = ''.join(cypherText)

encryptedFilename = input("Enter the filename for the encrypted file
(with file ending): ")
with open(encryptedFilename, 'w+') as cf:
cf.write(cypherText)
print("Encryption Complete")
print("")
menu()

def decrypt():
print("Enter the name of the file you wish to decrypt(with the file
ending)")
targetFile = input("-> ")
f = open(targetFile, "r+")
print(f.read())

print("Please enter the eight character code for decryption")
key = input("-> ")
keyLength = len(key)
keyTotal = 0
if keyLength != 8:
print("Please enter the eight character code for decryption")
key = input("-> ")
for k in range(0,8):
newValue = ord(key[k])
keyTotal += newValue
offset = keyTotal/8-32
offset = int(offset)

with open(targetFile) as f:
characters = 0
for line in f:
wordslist=line.split()
characters += sum(len(word) for word in wordslist)
characters = int(characters)

realText =
with open(targetFile, 'r+') as f:
data = f.read().replace('n','')
for k in range(0,characters):
if data[k] != ' ':
asciiValue = ord(data[k])
asciiValue -= offset
if asciiValue < 33:
asciiValue += 94
realText.append(str(chr(asciiValue)))
else:
realText.append(' ')
realText = ''.join(realText)
print(realText)

menu()









share|improve this question







New contributor




ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast 5 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast

If this question can be reworded to fit the rules in the help center, please edit the question.

















    up vote
    0
    down vote

    favorite












    Making a program that encrypts a txt file of the users choice and then saving the encrypted version as a separate file. It should also be able to decrypt the a file if the correct 8 character key is entered. The problem I'm having is that when decrypting the file characters from the end of the file seem to be cut out or left off and I can't see why. When decrypting the outcome anything after the "an" of "lance" is left out.



    Text File (Pastebin)
    Python Code (Pastebin)



    from random import randint

    def menu(): #Main menu
    choice = int(input("Encrypt [1] Decrypt [2] Exit [3]: ")) #Allows choice
    print("")

    if choice == 1: #Goes to the function for each option
    encrypt()
    elif choice == 2:
    decrypt()
    elif choice == 3:
    print("Good bye")

    def encrypt(): #Encrypt
    print("Enter the name of the file you wish to encrypt(with the file
    ending)") # Need the file ending to work
    targetFile = input("-> ")
    print('')
    f = open(targetFile, "r+") #Opens the file as 'f'
    print(f.read()) #Prints the text in the file

    key =
    for i in range(0,8):
    chrKey = randint(33,126)
    newKey = str(chr(chrKey)) #converts ascii number to text
    key.append(newKey)
    print('')
    print("Remember the eight character key or you can't decrypt your file")
    print('Your eight character key is:')
    print(''.join(key))
    print('')

    keyTotal = 0
    for j in range(0,8):
    newValue = ord(key[j])
    keyTotal += newValue
    offset = keyTotal/8-32
    offset = int(offset)

    with open(targetFile) as f:
    characters = 0
    for line in f:
    wordslist=line.split()
    characters += sum(len(word) for word in wordslist)
    characters = int(characters)

    cypherText =
    with open(targetFile, 'r+') as f:
    data = f.read().replace('n','')
    for k in range(0,characters):
    if data[k] != ' ':
    asciiValue = ord(data[k])
    asciiValue += offset
    if asciiValue > 126:
    asciiValue -= 94
    cypherText.append(str(chr(asciiValue)))
    else:
    cypherText.append(' ')
    cypherText = ''.join(cypherText)

    encryptedFilename = input("Enter the filename for the encrypted file
    (with file ending): ")
    with open(encryptedFilename, 'w+') as cf:
    cf.write(cypherText)
    print("Encryption Complete")
    print("")
    menu()

    def decrypt():
    print("Enter the name of the file you wish to decrypt(with the file
    ending)")
    targetFile = input("-> ")
    f = open(targetFile, "r+")
    print(f.read())

    print("Please enter the eight character code for decryption")
    key = input("-> ")
    keyLength = len(key)
    keyTotal = 0
    if keyLength != 8:
    print("Please enter the eight character code for decryption")
    key = input("-> ")
    for k in range(0,8):
    newValue = ord(key[k])
    keyTotal += newValue
    offset = keyTotal/8-32
    offset = int(offset)

    with open(targetFile) as f:
    characters = 0
    for line in f:
    wordslist=line.split()
    characters += sum(len(word) for word in wordslist)
    characters = int(characters)

    realText =
    with open(targetFile, 'r+') as f:
    data = f.read().replace('n','')
    for k in range(0,characters):
    if data[k] != ' ':
    asciiValue = ord(data[k])
    asciiValue -= offset
    if asciiValue < 33:
    asciiValue += 94
    realText.append(str(chr(asciiValue)))
    else:
    realText.append(' ')
    realText = ''.join(realText)
    print(realText)

    menu()









    share|improve this question







    New contributor




    ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    put on hold as off-topic by janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast 5 hours ago


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast

    If this question can be reworded to fit the rules in the help center, please edit the question.















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Making a program that encrypts a txt file of the users choice and then saving the encrypted version as a separate file. It should also be able to decrypt the a file if the correct 8 character key is entered. The problem I'm having is that when decrypting the file characters from the end of the file seem to be cut out or left off and I can't see why. When decrypting the outcome anything after the "an" of "lance" is left out.



      Text File (Pastebin)
      Python Code (Pastebin)



      from random import randint

      def menu(): #Main menu
      choice = int(input("Encrypt [1] Decrypt [2] Exit [3]: ")) #Allows choice
      print("")

      if choice == 1: #Goes to the function for each option
      encrypt()
      elif choice == 2:
      decrypt()
      elif choice == 3:
      print("Good bye")

      def encrypt(): #Encrypt
      print("Enter the name of the file you wish to encrypt(with the file
      ending)") # Need the file ending to work
      targetFile = input("-> ")
      print('')
      f = open(targetFile, "r+") #Opens the file as 'f'
      print(f.read()) #Prints the text in the file

      key =
      for i in range(0,8):
      chrKey = randint(33,126)
      newKey = str(chr(chrKey)) #converts ascii number to text
      key.append(newKey)
      print('')
      print("Remember the eight character key or you can't decrypt your file")
      print('Your eight character key is:')
      print(''.join(key))
      print('')

      keyTotal = 0
      for j in range(0,8):
      newValue = ord(key[j])
      keyTotal += newValue
      offset = keyTotal/8-32
      offset = int(offset)

      with open(targetFile) as f:
      characters = 0
      for line in f:
      wordslist=line.split()
      characters += sum(len(word) for word in wordslist)
      characters = int(characters)

      cypherText =
      with open(targetFile, 'r+') as f:
      data = f.read().replace('n','')
      for k in range(0,characters):
      if data[k] != ' ':
      asciiValue = ord(data[k])
      asciiValue += offset
      if asciiValue > 126:
      asciiValue -= 94
      cypherText.append(str(chr(asciiValue)))
      else:
      cypherText.append(' ')
      cypherText = ''.join(cypherText)

      encryptedFilename = input("Enter the filename for the encrypted file
      (with file ending): ")
      with open(encryptedFilename, 'w+') as cf:
      cf.write(cypherText)
      print("Encryption Complete")
      print("")
      menu()

      def decrypt():
      print("Enter the name of the file you wish to decrypt(with the file
      ending)")
      targetFile = input("-> ")
      f = open(targetFile, "r+")
      print(f.read())

      print("Please enter the eight character code for decryption")
      key = input("-> ")
      keyLength = len(key)
      keyTotal = 0
      if keyLength != 8:
      print("Please enter the eight character code for decryption")
      key = input("-> ")
      for k in range(0,8):
      newValue = ord(key[k])
      keyTotal += newValue
      offset = keyTotal/8-32
      offset = int(offset)

      with open(targetFile) as f:
      characters = 0
      for line in f:
      wordslist=line.split()
      characters += sum(len(word) for word in wordslist)
      characters = int(characters)

      realText =
      with open(targetFile, 'r+') as f:
      data = f.read().replace('n','')
      for k in range(0,characters):
      if data[k] != ' ':
      asciiValue = ord(data[k])
      asciiValue -= offset
      if asciiValue < 33:
      asciiValue += 94
      realText.append(str(chr(asciiValue)))
      else:
      realText.append(' ')
      realText = ''.join(realText)
      print(realText)

      menu()









      share|improve this question







      New contributor




      ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Making a program that encrypts a txt file of the users choice and then saving the encrypted version as a separate file. It should also be able to decrypt the a file if the correct 8 character key is entered. The problem I'm having is that when decrypting the file characters from the end of the file seem to be cut out or left off and I can't see why. When decrypting the outcome anything after the "an" of "lance" is left out.



      Text File (Pastebin)
      Python Code (Pastebin)



      from random import randint

      def menu(): #Main menu
      choice = int(input("Encrypt [1] Decrypt [2] Exit [3]: ")) #Allows choice
      print("")

      if choice == 1: #Goes to the function for each option
      encrypt()
      elif choice == 2:
      decrypt()
      elif choice == 3:
      print("Good bye")

      def encrypt(): #Encrypt
      print("Enter the name of the file you wish to encrypt(with the file
      ending)") # Need the file ending to work
      targetFile = input("-> ")
      print('')
      f = open(targetFile, "r+") #Opens the file as 'f'
      print(f.read()) #Prints the text in the file

      key =
      for i in range(0,8):
      chrKey = randint(33,126)
      newKey = str(chr(chrKey)) #converts ascii number to text
      key.append(newKey)
      print('')
      print("Remember the eight character key or you can't decrypt your file")
      print('Your eight character key is:')
      print(''.join(key))
      print('')

      keyTotal = 0
      for j in range(0,8):
      newValue = ord(key[j])
      keyTotal += newValue
      offset = keyTotal/8-32
      offset = int(offset)

      with open(targetFile) as f:
      characters = 0
      for line in f:
      wordslist=line.split()
      characters += sum(len(word) for word in wordslist)
      characters = int(characters)

      cypherText =
      with open(targetFile, 'r+') as f:
      data = f.read().replace('n','')
      for k in range(0,characters):
      if data[k] != ' ':
      asciiValue = ord(data[k])
      asciiValue += offset
      if asciiValue > 126:
      asciiValue -= 94
      cypherText.append(str(chr(asciiValue)))
      else:
      cypherText.append(' ')
      cypherText = ''.join(cypherText)

      encryptedFilename = input("Enter the filename for the encrypted file
      (with file ending): ")
      with open(encryptedFilename, 'w+') as cf:
      cf.write(cypherText)
      print("Encryption Complete")
      print("")
      menu()

      def decrypt():
      print("Enter the name of the file you wish to decrypt(with the file
      ending)")
      targetFile = input("-> ")
      f = open(targetFile, "r+")
      print(f.read())

      print("Please enter the eight character code for decryption")
      key = input("-> ")
      keyLength = len(key)
      keyTotal = 0
      if keyLength != 8:
      print("Please enter the eight character code for decryption")
      key = input("-> ")
      for k in range(0,8):
      newValue = ord(key[k])
      keyTotal += newValue
      offset = keyTotal/8-32
      offset = int(offset)

      with open(targetFile) as f:
      characters = 0
      for line in f:
      wordslist=line.split()
      characters += sum(len(word) for word in wordslist)
      characters = int(characters)

      realText =
      with open(targetFile, 'r+') as f:
      data = f.read().replace('n','')
      for k in range(0,characters):
      if data[k] != ' ':
      asciiValue = ord(data[k])
      asciiValue -= offset
      if asciiValue < 33:
      asciiValue += 94
      realText.append(str(chr(asciiValue)))
      else:
      realText.append(' ')
      realText = ''.join(realText)
      print(realText)

      menu()






      python-3.x






      share|improve this question







      New contributor




      ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 11 hours ago









      ConAar2

      11




      11




      New contributor




      ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      ConAar2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      put on hold as off-topic by janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast 5 hours ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast

      If this question can be reworded to fit the rules in the help center, please edit the question.




      put on hold as off-topic by janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast 5 hours ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Sᴀᴍ Onᴇᴌᴀ, Dannnno, Gerrit0, Mast

      If this question can be reworded to fit the rules in the help center, please edit the question.



























          active

          oldest

          votes






















          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre