Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cryptography help
#1
(encode/decode sections) Hello, I am having troubles actually taking the letter i found in the key or the alpha and making it the new plain for the main functions. Every time when I try to run the program it just says must be str not int. Which does make any sense because the string is a str. Any thoughts?

""" crypto.py
    Implements a simple substitution cypher
"""

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key =   "XPMGTDHLYONZBWEARKJUFSCIQV"


def menu():
    print('   Secret Decoder Menu')
    print()
    print('   0)' 'Quit')
    print('   1)' 'Encode')
    print('   2)' 'Decode')
    print()
    response = input('What would you like to do? ')
    return response

# when the user tpyes a number it sends the answer beck to the main function where it can be used in the while loop


def encode(plain):
    new_word = " "
    for letter in plain.upper():
        if letter in alpha:
            letter.find(key)
            keyLetter = letter.find(key)
            plain = new_word + keyLetter
    return plain


def decode(coded):
    new_word = " "
    for letter in coded.upper():
        if letter in key:
            letter.find(alpha)
            keyLetter = letter.find(alpha)
            coded = new_word + keyLetter
    return plain



def main():
  keepGoing = True
  while keepGoing:
    response = menu()
    if response == "1":
      plain = input("text to be encoded: ")
      print(encode(plain))
      print()
    elif response == "2":
      coded = input("code to be decyphered: ")
      print (decode(coded))
      print()
    elif response == "0":
      print ("Thanks for doing secret spy stuff with me.")
      keepGoing = False
    else:
      print ("I don't know what you want to do...")

main()
Reply
#2
not 100 percent sure...
on whatever it is you could put
str("""enter variable/info here""")
I would also try using ' instead of "
Reply
#3
It would be most helpful to post your entire error traceback verbatim, no modifications
I also fixed your closing python tag, it was missing the '/'
Reply
#4
The 'str.find()' is going to return a number (integer) starting with 0 For the beginning and ending with usually the length of the string, unless specified otherwise. It will be -1 if not found. You will need to rethink your logic to determine what exactly you want to do with the 'key' (index) as it relates to process.

Also, in your 'decoded' function, don't you want to return 'coded' rather than 'plain'?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [cryptography.io] How to convert DER signature to ECDSA fstefanov 1 2,986 Jul-04-2019, 08:59 AM
Last Post: fstefanov
  Cryptography in Python NLittle17 6 4,210 Jan-13-2019, 07:11 PM
Last Post: NLittle17
  Python cryptography or pycrypto lexlukkia 4 47,605 Jan-25-2017, 12:08 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020