Author Topic: [Python] Caesar Cipher + Assistance Required  (Read 4755 times)

0 Members and 4 Guests are viewing this topic.

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
[Python] Caesar Cipher + Assistance Required
« on: December 31, 2012, 11:17:46 pm »
So I was feeling bored and decided to search for ROT-13 implementations in Python. I stumbled upon this:

Code: [Select]
import string
letters = string.ascii_lowercase
key = 13
trans_table = letters[key:] + letters[0:key]
trans = string.maketrans(letters, trans_table)
text = "i am crazy"
print text.translate(trans)

So I decided to modify it into a general Caesar cipher input/output script. End result I made (complete with arbitrary exception handling):

Code: [Select]
import string

print "--[Basic Caesar Cipher Input/Output]--\n"

try:
    value = int(raw_input("Type in shift value (1-26): "))
except ValueError:
    print "Type in a number."
    exit(0)

letters = string.letters
convert_table = letters[value:0] + letters[0:value]
convert = string.maketrans(letters, convert_table)

print "\nEnter a string to encode/decode:"

while True: #infinite loop so user will keep receiving prompts
   
    try:
        text = str(raw_input("> "))
    except KeyboardInterrupt:
        print "\nSee ya."
        exit(0)

    print "Result:", text.translate(convert).title()

So it works, except for the fact that the output is in mixed case letters. I attempted several ways to fix it, but to no real avail. Finally, I settled on using the built-in title() function, which obviously capitalizes every first letter. I find that to be a kludge and rather unsatisfactory. Anyone got any ideas? Still a beginner here, but I think the slicing operator might be an issue?
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Caesar Cipher + Assistance Required
« Reply #1 on: January 01, 2013, 12:15:04 pm »
Use upper()

Code: [Select]
'foo'.upper()
'FOO'

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: [Python] Caesar Cipher + Assistance Required
« Reply #2 on: January 01, 2013, 12:35:24 pm »
Use upper()

Code: [Select]
'foo'.upper()
'FOO'
But you said that you don't do python. >:( >:(

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: [Python] Caesar Cipher + Assistance Required
« Reply #3 on: January 01, 2013, 06:58:07 pm »
Use upper()

Code: [Select]
'foo'.upper()
'FOO'

That's another way to do it. Thank you. I took a look at all available string methods now, so I'll keep them in mind.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Caesar Cipher + Assistance Required
« Reply #4 on: January 01, 2013, 09:20:27 pm »
But you said that you don't do python. >:( >:(

I don't, but I am able to read the API.