Caesar Cipher using python can use a little help
I am trying to do "Caesar Cipher" while using python. This is what I have so far. Can anyone tell me what it looks like? Am I going in the right direction? What am I missing? When I run the program to say, for example (josh is cool), I don't get the cipher on the same line. It looks like this when I domain(3)
m
r
v
k
l
v
f
r
r
o
But it puts each letter on a new line. How can I make it so that it is in line?
def main(k):
if k<0 or k>231:
print "complaint"
raise SystemExit
Input = raw_input("Please enter Plaintext to Cipher")
for x in range(len(Input)):
letter=Input[x]
if letter.islower():
x=ord(letter)
x=x+k
if x>122:
x=x-122+97
print chr(x),
if letter.isupper():
x=ord(letter)
x=x+k
if x>90:
x=x-90+65
print chr(x),
I like kaizer.se's answer, but I think I can simplify it using string.maketrans :
import string
first = raw_input("Please enter Plaintext to Cipher: ")
k = int(raw_input("Please enter the shift: "))
shifted_lowercase = ascii_lowercase[k:] + ascii_lowercase[:k]
translation_table = maketrans(ascii_lowercase, shifted_lowercase)
print first.translate(translation_table)
source to share
This code should work pretty well. It also handles arbitrary offsets, including negative ones.
phrase = raw_input("Please enter plaintext to Cipher: ")
shift = int(raw_input("Please enter shift: "))
result = ''
for char in phrase:
x = ord(char)
if char.isalpha():
x = x + shift
offset = 65
if char.islower():
offset = 97
while x < offset:
x += 26
while x > offset+25:
x -= 26
result += chr(x)
print result
Another way to do it with slightly different encryption is to simply rotate all characters, top and bottom, or even all ascii> 0x20.
phrase = raw_input("Please enter plaintext to Cipher: ")
shift = int(raw_input("Please enter shift: "))
result = ''
for char in phrase:
x = ord(char)
x = x + shift
while x < 32:
x += 96
while x > 127:
x -= 96
result += chr(x)
print result
source to share
Here's another way to show how we can handle this in a very simple way. We define an input alphabet and an output alphabet, then a translation table and use it unicode.translate()
for the actual encryption.
import string
# Blatantly steal Lennart UI design
first = unicode(raw_input("Please enter Plaintext to Cipher: "), "UTF-8")
k = int(raw_input("Please enter the shift: "))
in_alphabet = unicode(string.ascii_lowercase)
out_alphabet = in_alphabet[k:] + in_alphabet[:k]
translation_table = dict((ord(ic), oc) for ic, oc in zip(in_alphabet, out_alphabet))
print first.translate(translation_table)
It can be expanded to uppercase letters if necessary.
source to share
Disallowing syntax errors seems to work for your code.
However, I took the liberty of removing all duplicates and cleaning it up:
first = raw_input("Please enter Plaintext to Cipher: ")
k = int(raw_input("Please enter the shift: "))
result = ''
for second in first:
x=ord(second)
x=x+k
if x>90 and x<122:
x=x-26
elif x>122:
x=x-26
result += chr(x)
print first
print result
Also "first" and "second" are really bad names for these variables. "Enter" and "letter" are probably better.
source to share
I am a very simple, 3-shift solution without Umlauts and this will be:
def caesar(inputstring):
shifted=string.lowercase[3:]+string.lowercase[:3]
return "".join(shifted[string.lowercase.index(letter)] for letter in inputstring)
and vice versa:
def brutus(inputstring):
shifted=string.lowercase[-3:]+string.lowercase[:-3]
return "".join(shifted[string.lowercase.index(letter)] for letter in inputstring)
using it:
caesar("xerxes")
source to share
For Python 3.3, try using the ord (), chr () and .isalpha functions:
m = input("What is your message?: ")
s = int(input("What is the shift?: "))
for i in m:
if i.isalpha():
if (ord(i)+s)>90:
print(chr(ord(i)+s-26),end=""),
elif chr(ord(i)+s-26)<65:
print("The shift is invalid")
else:
print(chr(ord(i)+s),end=""),
else:
pass
source to share
Here is an onliner.
>>> brutus=lambda message,cipher,direction:''.join([chr((ord(letter)+(cipher*direction))%256) for letter in message])
>>> encrypted= brutus('Message to be encrypted',14,1) #direction=1 for encryption
>>> encrypted
'[s\x81\x81ous.\x82}.ps.s|q\x80\x87~\x82sr'
>>> brutus(encrypted,14,-1) # direction=-1 for decryption
'Message to be encrypted'
>>>
source to share