How can I fix the IndexError? - python

When I run this program (It is supposed to encode and decode things in and out of the Caesar cipher) and opt for the decode option, I get the error saying that the string index is out of range. Can anyone tell me how to fix this and tell me why it is happening? The text I entered for it to decode was ibmmp and the key was 1.Thanks.
alphabet = "abcdefghijklmnopqrstuvwxyz"
encdec = input("Would you like to encode or decode a message? ")
if encdec == "decode":
keyyn = input("Do you know the key to your encoded text? (Y/N) ")
if keyyn == "Y":
plaintext = input("Please type in your text ")
text = plaintext
key = int(input("What is the key? "))
for i in range(len(plaintext)):
letter = plaintext[i]
alphletter = alphabet.find(letter)
alphletter = alphletter - key
if alphletter < 0 or alphletter == 0:
alphletter = alphletter + 26
letter = alphabet[alphletter]
plaintext = plaintext + letter
else:
letter = alphabet[alphletter]
plaintext = plaintext + letter
print(plaintext.strip(text))
else:
print("This program is unable to decode a message without the key")

Problem: ibmmp and key of 1
i works, b gives you an error. Here is why:
alphletter = alphabet.find(letter) # ==> 1
alphletter = alphletter - key # ==> 0
if alphletter < 0 or alphletter == 0: # ==> True
alphletter = alphletter + 26 # ==> 26
letter = alphabet[alphletter] # only has indexes from 0 to 25
plaintext = plaintext + letter # ~~~~ crash ~~~~
# (also: indentation error for the last 2 lines)
You can use the modulo-operator % to fix over/underflow:
alphletter = (alphletter - key) % 26 # for -1 : 25
You could also use if alphletter < 0: - this will not handle keys thats wrap around multiple times (f.e. 210) or negative keys -22
Some optimizations
# create a mapping dictionary so we do not need index()
alph = "abcdefghijklmnopqrstuvwxyz"
len_alph = len(alph)
d = {c:i for i,c in enumerate(alph)} # mapping char to index
d.update( {v:k for k,v in d.items()} ) # mapping index to char
d.update( {c:i for i,c in enumerate(alph.upper())} ) # mapping CHAR to index
def encode(text,key):
indexes = [d.get(c,"?") for c in text] # only mapped things, others get ?
# if ? use ? else lookup the correct replacement using % to make the index
# wrap around if above/below the index into alph
return ''.join(d.get((i+key)%len_alph if i != "?" else "?","?") for i in indexes)
def decode(text,key):
return encode(text,-key)
print(encode("tataaaa",5))
Output:
yfyffff

Related

vigenere encryption probleme with 'spaces' on python

i can t pinpoint the reason why the encryption goes wrong after 'spaces'
here is the code :
def chiffre_vigenere(message,key):
message = message.lower()
key = key.lower()
encrypted = []
d = dict(a=0,b=1,c=2,d=3,e=4,f=5,g=6,h=7,i=8,j=9,k=10,l=11,m=12,n=13,o=14,p=15,q=16,r=17,s=18,t=19,u=20,v=21,w=22,x=23,y=24,z=25)
long_key = key
while len(message) > len(long_key) :
long_key = long_key + key
i=0
while i < len(message) :
decalage = d.get(long_key[i])
if message[i].isalpha() :
c = ord(message[i]) + decalage
if c > 122 :
c = c - 26
encrypted.append(chr(c))
else :
encrypted.append(message[i])
i = i+1
print(listToString(encrypted))
for example when i do : chiffre_vigenere('stack overflow','apple')
it gives : sipno dkpvfadh
the first word is encrypted right but after the space the encryption is wrong
any help is appreciated thanks in advance
The problem is that you're using the same counter variable i to mark your place in two different strings: message and long_key. Whenever you encounter a non-alphabet character in message, you're stepping to the next character, but you're also stepping to the next character in long_key, which is not what you want to do.
If you just use i to count characters in long_key then the problem goes away. For example:
def chiffre_vigenere(message,key):
message = message.lower()
key = key.lower()
encrypted = []
d = dict(a=0,b=1,c=2,d=3,e=4,f=5,g=6,h=7,i=8,j=9,k=10,l=11,m=12,n=13,o=14,p=15,q=16,r=17,s=18,t=19,u=20,v=21,w=22,x=23,y=24,z=25)
long_key = key
while len(message) > len(long_key):
long_key = long_key + key
i=0
for ch in message:
decalage = d.get(long_key[i])
if ch.isalpha() :
c = ord(ch) + decalage
if c > 122 :
c = c - 26
encrypted.append(chr(c))
i = i+1
else :
encrypted.append(ch)
print(listToString(encrypted))
You might also consider simplifying your code a little. You can use the ord function to convert ASCII values to integers, and a modulus operator to cycle through the values of key without having to expand it to the size of the input message.
def chiffre_vigenere(message,key):
key = key.lower()
encrypted = ''
i = 0
for ch in message.lower():
if 'a' <= ch <= 'z':
a = ord(ch) + ord(key[i]) - ord('a')
if a > ord('z'):
a -= 26
encrypted += chr(a)
i = (i + 1) % len(key)
else:
encrypted += ch
print(encrypted)

Improving Encryption Algorithm

Recently I had started this assignment just meant for school purposes but I have recently decided that I would want to continue this as a new project and wanted suggestions on how I can improve my algorithm. I had an idea where I wanted the encryption key to change per character to make my encryption more secure but I have been having difficulty with this as It wouldn't work out and I couldnt decrypt my final encrypted text
Here is the encrypter:
text = input('Enter A Word : ') ##This asks a user for an input of text integers etc
encrypt = '' ##Empty Value used to store the encrypted value
temp = '' ##Empty temp value replaced at every iteration of the encryption process
temp2 =0 ##Empty temp value replaced at every iteration of the encryption process
temp_val=0
temp_char=''
rtext= text[::-1]
key=int(input('Enter your key (Your Encrypted Sentence Will Further be Encrypted Later) : '))##key used to shift the letters
for a in range (0,len(rtext)):
if len(rtext) % 2==0:
hlength=len(rtext)/2
else:
hlength=(len(rtext)+1)/2
print(hlength)
for i in range(int(hlength),len(rtext)):
##Rearranges text in a caps switch
if str.islower(rtext[i])==True:
temp=temp+str.upper(rtext[i])
elif str.isupper(rtext[i])==True:
temp=temp+str.lower(rtext[i])
else:
temp=temp+rtext[i]
for b in range(0,int(hlength)):
##Rearranges text in a caps switch
if str.islower(rtext[b])==True:
temp=temp+str.upper(rtext[b])
elif str.isupper(rtext[b])==True:
temp=temp+str.lower(rtext[b])
else:
temp=temp+rtext[b]
for j in range(0,len(temp)):
temp_val=0
temp2=0
temp_val=ord(temp[j])
temp2=temp2+temp_val+int(key)
temp_char=temp_char+chr(temp2)
encrypt=temp_char
print(encrypt)
print(temp)
print(temp2)
The Decrypter:
text = input('Enter A Word : ') ##This asks a user for an input of text integers etc
decrypt = '' ##Empty Value used to store the encrypted value
order=0
characters=''
temp=''
rtext=text[::-1]
key=int(input('Enter your key (decryption) : '))##key used to shift the letters
for i in range (0,len(rtext)):
order=0
order=order+ord(rtext[i])-int(key)
characters=characters+chr(order)
for a in range (0,len(rtext)):
if len(rtext) % 2==0:
hlength=len(rtext)/2
else:
hlength=(len(rtext)+1)/2
for j in range (int(hlength),len(characters)):
if str.islower(characters[j])==True:
temp=temp+str.upper(characters[j])
elif str.isupper(characters[j])==True:
temp=temp+str.lower(characters[j])
else:
temp=temp+characters[j]
for b in range (0,int(hlength)):
if str.islower(characters[b])==True:
temp=temp+str.upper(characters[b])
elif str.isupper(characters[b])==True:
temp=temp+str.lower(characters[b])
else:
temp=temp+characters[b]
print(temp)
I specifically want to change the variable key.
ord() - Turns characters into its Ascii equivalent
chr() - Turns Ascii numbers into its character equivalent
rtext - gets the inverse of the users input
If we simplify the code in the encryptor a little, we get:
def encrypt_text(text: str, key: int):
print("TEXT:", text, "KEY:", key)
temp = ''
temp2 = 0
temp_val = 0
temp_char = ''
rtext = text[::-1]
print("RTEXT:", rtext)
hlength = len(rtext) // 2 + len(rtext) % 2 # half, round up
print("HLENGTH:", hlength)
for i in range(hlength, len(rtext)):
# Rearrange text in a caps switch
if rtext[i].islower():
temp += rtext[i].upper()
elif rtext[i].isupper():
temp += rtext[i].lower()
else:
temp += rtext[i]
print("TEMP:", temp)
for b in range(0, int(hlength)):
# Rearrange text in a caps switch
if rtext[b].islower():
temp += rtext[b].upper()
elif rtext[b].isupper():
temp += rtext[b].lower()
else:
temp += rtext[b]
for j in range(len(temp)):
temp_val = 0
temp2 = 0
temp_val = ord(temp[j])
temp2 = temp2 + temp_val + int(key)
temp_char = temp_char + chr(temp2)
encrypt = temp_char
print("ENCRYPT:", encrypt)
print("TEMP:", temp)
print("TEMP2:", temp2)
return encrypt
text = "hello world"
key = 42
print("ENCRYPTED:", encrypt_text(text, key))
I've put it inside a function (and added some print statements), so it becomes easier to work with while developing. The code is essentially the same as yours, except
for a in range (0,len(rtext)):
if len(rtext) % 2==0:
hlength=len(rtext)/2
else:
hlength=(len(rtext)+1)/2
is replaced by
hlength = len(rtext) // 2 + len(rtext) % 2 # half, round up
which gives the same result (except hlength is an integer).
Your first two for loops do the same operation (switches case on a string). We can write a function for that:
def swap_case(str):
res = ''
for ch in str:
if ch.islower():
res += ch.upper()
elif ch.isupper():
res += ch.lower()
else:
res += ch
return res
and now we can replace the first two for loops with calls to our function:
temp += swap_case(rtext[hlength:len(rtext)]) # or shorter rtext[hlength:]
temp += swap_case(rtext[0:hlength]) # or shorter rtext[:hlength]
it just happend that .swapcase() is already a string method, so we didn't really need our swap_case function, and could just write:
temp += rtext[hlength:].swapcase()
temp += rtext[:hlength].swapcase()
Your third for-loop:
for j in range(len(temp)):
temp_val = 0 # this value is not used (it's overwritten 2 lines down)
temp2 = 0
temp_val = ord(temp[j])
temp2 = temp2 + temp_val + int(key) # temp2 is always zero at this point
temp_char = temp_char + chr(temp2)
encrypt = temp_char
can be simplified to (the initial value of temp_char is set to the empty string above):
for j in range(len(temp)): # for each index position (j)
temp_val = ord(temp[j]) # use the character at from temp at index j
temp2 = temp_val + int(key) # key is already an int from your: key=int(input('Enter your key (decryption) : '))
temp_char += chr(temp2)
encrypt = temp_char # hmm... just overwriting encrypt on every iteration
the comments mean that it could be even simpler:
encrypt = ''
for character in temp:
temp_val = ord(character)
temp2 = temp_val + key
encrypt += chr(temp2)
This leaves us with (the comments enumerate the steps taken):
def encrypt_text(text: str, key: int):
temp = ''
rtext = text[::-1] # (1) reverse the string
hlength = len(rtext) // 2 + len(rtext) % 2 # (2) split the string on hlength
second_part = rtext[hlength:].swapcase() # .. and swap case on the parts
first_part = rtext[:hlength].swapcase()
temp += second_part # (3) and put the second part..
temp += first_part # .. before the first part
encrypt = ''
for character in temp:
temp_val = ord(character)
temp2 = temp_val + key # (4) add key to every character
encrypt += chr(temp2)
return encrypt
to decrypt a string encrypted with this function, we need to do the operations "backwards and opposite":
def decrypt_text(encrypted, key):
temp = ''
for ch in encrypted:
temp += chr(ord(ch) - key) # subtract key from every character (4)
hlength = len(encrypted) // 2 + len(encrypted) % 2
half = len(encrypted) - hlength # the split point is a mirror image of what it is in encrypt_text (3)
rtext = ''
rtext += temp[half:].swapcase() # re-assemble the string and swap case (2)
rtext += temp[:half].swapcase()
text = rtext[::-1] # finally reverse (1)
return text
The standard way of using longer keys (similar to your one-key-per-character), is to use the xor function, which in Python is written as ^ (pronounced either 'hat' or 'xor'), as in:
a ^ b # true if either a, or b are true, but not both
Here is some background on how it works, although you don't really need to understand this to use it...
This operator work on bits. To see what is happening, lets define a
function to print the bit representation of an integer (you don't need
to understand this):
def bits(n):
return bin(n)[2:].zfill(4)
then we have we can show the bit patterns of integers 5 and 9, and the
operation 5 ^ 9:
bits(5) => 0101
bits(9) => 1001
--------------------
bits(5 ^ 9) => 1100
====================
if you look at the bit patterns, there is a 1 in the result where
there is exactly one 1 in the column above, so from left to right (0 ^
1 = 1, 1 ^ 0 = 1, 0 ^ 0 = 0, and 1 ^ 1 = 0).
Knowing the above, you can verify that for any number k ^ k == 0,
and n ^ 0 == n, and therefore n ^ k ^ k == n.
The useful thing about xor is that for any number n:
n ^ key ^ key == n
ie. xor-ing the number with key, twice, gives you back the number.
Let's use this to encrypt (zip(text, key) returns one character from text and key at a time, in lock-step, until one of them is "used up"):
def encrypt_xor(text: str, key: str):
if len(key) < len(text):
# key must be longer because of how zip works..
raise ValueError("Key must be at least as long as text")
res = ''
for ch, k in zip(text, key):
res += chr(ord(ch) ^ ord(k))
return res
if you try to print(encrypt_text('hello', 'world')) you'll get gibberish printed to your screen (since the value you get by xor-ing two characters isn't necessarily printable). The cool thing about xor is that the decrypt function is exactly the same as the encrypt function, so encrypting twice gives you the original value:
text = 'hello'
key = 'world'
cipher = encrypt_xor(text, key) # encrypted text is often called cipher
print(encrypt_xor(cipher, key)) # prints 'hello'
You can use a similar structure for shift-type encryption (but without the convenience that the decrypt function is the same as the encrypt), e.g.:
def encrypt_shift(text: str, key: str):
res = ''
for ch, k in zip(text, key):
res += chr(ord(ch) + ord(k)) # add the char from the key
return res
def decrypt_shift(text: str, key: str):
res = ''
for ch, k in zip(text, key):
res += chr(ord(ch) - ord(k)) # subtract the char from the key
return res
text = 'hello'
key = 'world'
cipher = encrypt_shift(text, key)
print(decrypt_shift(cipher, key)) # prints 'hello
to avoid the unpleasantness of needing a key that is longer than the text, we can start using the key from the beginning again if there is more text left. The itertools.cycle(..) function does this for us:
import itertools
def encrypt_shift(text: str, key: str):
res = ''
for ch, k in zip(text, itertools.cycle(key)):
res += chr(ord(ch) + ord(k))
return res
def decrypt_shift(text: str, key: str):
res = ''
for ch, k in zip(text, itertools.cycle(key)):
res += chr(ord(ch) - ord(k))
return res
now
text = 'hello world'
key = 'world'
cipher = encrypt_shift(text, key)
print(decrypt_shift(cipher, key)) # prints 'hello world' (not just 'hello' -- the first len(key) characters)
This can be plugged into the encrypt_text and decrypt_text functions from the other answer:
def encrypt_text(text: str, key: str): # key is now a string
temp = ''
rtext = text[::-1] # (1) reverse the string
hlength = len(rtext) // 2 + len(rtext) % 2 # (2) split the string on hlength
second_part = rtext[hlength:].swapcase() # .. and swap case on the parts
first_part = rtext[:hlength].swapcase()
temp += second_part # (3) and put the second part..
temp += first_part # .. before the first part
encrypt = encrypt_shift(temp, key) # (4) shift each char using key
return encrypt
and
def decrypt_text(encrypted, key):
temp = decrypt_shift(encrypted, key) # unshift each char using key
hlength = len(encrypted) // 2 + len(encrypted) % 2
half = len(encrypted) - hlength # the split point is a mirror image of what it is in encrypt_text (3)
rtext = ''
rtext += temp[half:].swapcase() # re-assemble the string and swap case (2)
rtext += temp[:half].swapcase()
text = rtext[::-1] # finally reverse (1)
return text

String index not working? - Python

I'm Making a sort of cipher that requires a string to be read backwards and i get an indexing error even though the index I reference is well within range:
M = str(input("Input Message: "))
M = M.upper()
L = len(M)
A = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
def DECRYPT():
global L
global M
global A
if L%2 != 0:
POS = False
else:
POS = True
i = L-1
NM = ""
while 1:
if M[i] != " ":
INDEX = A.index(M[i])
if POS == True:
INDEX += (i + 1)
else:
INDEX -= (i)
INDEX %= 26
NM = NM+A[INDEX]
i += 1
if POS == True:
POS = False
else:
POS = True
print("\n"+NM)
def ENCRYPT():
global L
global A
global M
POS = True
M = M[::-1]
i = 0
NM = ""
while 1:
if i == L:
break
if M[i] != " ":
INDEX = A.index(M[i])
if POS == True:
INDEX += (i + 1)
else:
INDEX -= (i + 1)
INDEX %= 26
NM = NM+A[INDEX]
i += 1
if POS == True:
POS = False
else:
POS = True
print("\n"+NM)
while 1:
C = int(input("\nWhat do you want to do:\n1) Encrypt Something\n2)Decrypt Something\n\n"))
if C == 1:
ENCRYPT()
if C == 2:
DECRYPT()
where i is a placeholder value. I run it and get this:
Input Message: ABC
What do you want to do:
1) Encrypt Something
2)Decrypt Something
2
Traceback (most recent call last):
File "C:\Users\Danny\Google Drive\SHIFT.py", line 67, in <module>
DECRYPT()
File "C:\Users\Danny\Google Drive\SHIFT.py", line 19, in DECRYPT
if M[i] != " ":
IndexError: string index out of range
I have tried changing value of i to no avail.
There are multiple problems with your code: as fernand0 noted, your index runs the wrong way; your inversion of POS happens at different levels in the code, the encryptor does it on every character, the decryptor does it on every letter -- they should work the same; five of your six global declarations aren't needed; you don't deal with word breaks correctly so the decryption won't match the original; once you encrypt, there's no way to decrypt in the same session as the local NM doesn't feedback into the global M.
Below is my rework of your code addressing the above problems and some style issues. The key phrase here is 'simplify'. I've kept your odd uppercase variable names but expanded them from single characters to what they represent:
MESSAGE = input("Input Message: ").upper()
ALPHABET = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
ALPHABET_LENGTH = len(ALPHABET)
def DECRYPT(MESSAGE):
LENGTH = len(MESSAGE)
IS_NEGATIVE = True
NEW_MESSAGE = ""
for I in range(LENGTH):
if MESSAGE[I] in ALPHABET:
INDEX = ALPHABET.index(MESSAGE[I])
if IS_NEGATIVE:
INDEX -= (I + 1)
else:
INDEX += (I + 1)
INDEX %= ALPHABET_LENGTH
NEW_MESSAGE += ALPHABET[INDEX]
IS_NEGATIVE = not IS_NEGATIVE
else:
NEW_MESSAGE += MESSAGE[I]
return NEW_MESSAGE[::-1]
def ENCRYPT(MESSAGE):
MESSAGE = MESSAGE[::-1]
LENGTH = len(MESSAGE)
IS_POSITIVE = True
NEW_MESSAGE = ""
for I in range(LENGTH):
if MESSAGE[I] in ALPHABET:
INDEX = ALPHABET.index(MESSAGE[I])
if IS_POSITIVE:
INDEX += (I + 1)
else:
INDEX -= (I + 1)
INDEX %= ALPHABET_LENGTH
NEW_MESSAGE += ALPHABET[INDEX]
IS_POSITIVE = not IS_POSITIVE
else:
NEW_MESSAGE += MESSAGE[I]
return NEW_MESSAGE
while True:
print("\nWhat do you want to do:")
print("1) Encrypt Message")
print("2) Decrypt Message")
CHOICE = int(input("\n"))
if CHOICE == 1:
MESSAGE = ENCRYPT(MESSAGE)
if CHOICE == 2:
MESSAGE = DECRYPT(MESSAGE)
print("\n" + MESSAGE)
TEST
> python3 file.py
Input Message: An opportunity to teach is an opportunity to learn
What do you want to do:
1) Encrypt Message
2) Decrypt Message
1
OPDAQ HB OEWAGIBFXIU JD RI JZEZZ GC NFVBFJAGWJT KC
What do you want to do:
1) Encrypt Message
2) Decrypt Message
2
AN OPPORTUNITY TO TEACH IS AN OPPORTUNITY TO LEARN
What do you want to do:
1) Encrypt Message
2) Decrypt Message
You are starting i at the end of the string and then, you are increasing it. I think the line i += 1 should be i -= 1

Python shift with alphabet

Locked. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
def menu():
choice = input("Press 1 to encode, 2 to decode, 9 to exit ")
return choice
def makeKeycode(message):
key = input("What is the key? ").upper()
length = len(message)
keycode = ""
counter = 0
while length >0:
if counter == len(key):
counter = 0
keycode = keycode + key[counter]
counter = counter + 1
length = length - 1
print(keycode)
return keycode
def enterMessage():
message = input("What is the message ").upper()
return message
def encodeMessage(message, keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(message)):
character = message[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode + keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext + cipherLetter
return ciphertext
def decodeMessage(ciphertext,keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(ciphertext)):
character = ciphertext[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode - keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext - cipherLetter
return message
def enterCipher ():
ciphertext = input("Enter the text to be decoded")
return ciphertext
def encode():
message = enterMessage()
keycode = makeKeycode(message)
ciphertext = encodeMessage(message,keycode)
print(ciphertext)
def decode():
keycode = makeKeycode(ciphertext)
message = decodeMessage(ciphertext, keycode)
print (message)
def main():
MyDictionary=("A:1","B:2","C:3","D:4","E:5","F:6","G:7","H:8","I:9","J:10","K:11","L:12","M:13","N:14","O:15","P:16","Q:17","R:18","S:19","T:20","U:21","V:22","W:23","X:24","Y:25","X:26")
print (MyDictionary)
choice = 0
while choice !=9:
choice = int(menu())
if choice == 1:
encode()
elif choice == 2:
decode()
if __name__ == "__main__":
main()
Hi, I cant get my code to work, my encode function works but i am struggling to fix my decode function. I dont understand where i am going wrong. I want to be able to decode the message i will have encoded, but this doesn't work as the decode function stops the program. Have I made an error in the coding? Thanks for all the help
Regards
def decodeMessage(ciphertext,keycode):
ciphertext =""
It appears that you are resetting ciphertext to a null string. as a result, your len(ciphertext) is 0 which means you ae not doing anything.
python 2.75
sig = 'abcd'
def test(sig):
sig = ""
print 'sig = ', sig
>>> test(sig)
sig = ''
>>> print sig
'abcd'
I am finding two problems with your decode message function:
def decodeMessage(ciphertext,keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(ciphertext)):
character = ciphertext[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode - keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext - cipherLetter
return message
The first issue that I found was the fact that you pass in ciphertext to decodeMessage as a mandatory parameter and then you change the value of ciphertext to "" (an empty string). I would recommend changing the ciphertext in the parameters to something like "ct" to prevent that overlap.
The second issue I found was the fact you return message, but you don't perform any action on message. When running this program, you'll get a global error because message is not global yet you're calling message in the local scope without having defined it locally.
Now, onto another matter: Your encode method doesn't encode anything correctly. Look at the following output:
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 18
YNLD
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 5
YNLD
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 1
YNLD
All 'some's get encoded to 'YNLD' regardless of the key. To cipher something using a Caesar Cypher, you're going to want to use ords and chrs. Ord makes the character numerical, Chr makes the number into a letter. Consider the following function made in Python 3, for example:
from string import ascii_letters
def cypher_letter(letter, key):
if letter not in ascii_letters: #commas, question marks, etc
return letter
if letter.islower(): #lowercase and uppercase have differing ord values
lower, upper = ord('a'), ord('z') # values: 97 to 122
elif letter.isupper():
lower, upper = ord('A'), ord('Z') # values: 65 to 90
else:
print("Something went wrong.") #probably didn't enter valid letters
cypher = ord(letter) + key #if key is "1" and letter is 'a', 97 + 1
if cypher < lower: cypher += 26 #26 letters in alphabet, this is for looping
if cypher > upper: cypher -= 26 #back around if it exceeds upper,lower limits
return chr(cypher) #chr(98) = 'b'
The following code:
cyphered_words = []
keys = [18, 5, 1]
for key in keys:
encoded = ""
for letter in 'some':
encoded += (cypher_letter(letter, key)) #to cypher we pass the key
print(encoded)
cyphered_word.append(encoded)
Outputs this:
Kgewlzafy hjwllq dgfy, oalz ugehdwp dwllwjk!
Xtrjymnsl uwjyyd qtsl, bnym htruqjc qjyyjwx!
Tpnfuijoh qsfuuz mpoh, xjui dpnqmfy mfuufst!
Cyphering and decyphering is almost the same thing. It's like they're yin and yang or something, so the decypher function would look like:
def decrypt_message(message, key):
decyphered_text = ""
for letter in message:
decyphered_text += cypher_letter(letter, (key*-1)) #to decypher we pass the negative key
return decyphered_text
And a loop to decrypt our 3 'some's:
for count, word in enumerate(cyphered_words):
print(decrypt_message_k(word, keys[count]))
And the output:
Something pretty long, with complex letters!
Something pretty long, with complex letters!
Something pretty long, with complex letters!
I hope this helps you get on the right track.

Caesar Cipher Function in Python

I'm trying to create a simple Caesar Cipher function in Python that shifts letters based on input from the user and creates a final, new string at the end. The only problem is that the final cipher text shows only the last shifted character, not an entire string with all the shifted characters.
Here's my code:
plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))
def caesar(plainText, shift):
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText = ""
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
caesar(plainText, shift)
I realize that this answer doesn't really answer your question, but I think it's helpful anyway. Here's an alternative way to implementing the caesar cipher with string methods:
def caesar(plaintext, shift):
alphabet = string.ascii_lowercase
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
table = string.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
In fact, since string methods are implemented in C, we will see an increase in performance with this version. This is what I would consider the 'pythonic' way of doing this.
You need to move cipherText = "" before the start of the for loop. You're resetting it each time through the loop.
def caesar(plainText, shift):
cipherText = ""
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
This is an improved version of the code in the answer of #amillerrhodes that works with different alphabets, not just lowercase:
def caesar(text, step, alphabets):
def shift(alphabet):
return alphabet[step:] + alphabet[:step]
shifted_alphabets = tuple(map(shift, alphabets))
joined_aphabets = ''.join(alphabets)
joined_shifted_alphabets = ''.join(shifted_alphabets)
table = str.maketrans(joined_aphabets, joined_shifted_alphabets)
return text.translate(table)
Example of usage:
>>> import string
>>> alphabets = (string.ascii_lowercase, string.ascii_uppercase, string.digits)
>>> caesar('Abc-xyZ.012:789?жñç', step=4, alphabets=alphabets)
'Efg-bcD.456:123?жñç'
References:
Docs on str.maketrans.
Docs on str.translate.
Docs on the string library
Using some ascii number tricks:
# See http://ascii.cl/
upper = {ascii:chr(ascii) for ascii in range(65,91)}
lower = {ascii:chr(ascii) for ascii in range(97,123)}
digit = {ascii:chr(ascii) for ascii in range(48,58)}
def ceasar(s, k):
for c in s:
o = ord(c)
# Do not change symbols and digits
if (o not in upper and o not in lower) or o in digit:
yield o
else:
# If it's in the upper case and
# that the rotation is within the uppercase
if o in upper and o + k % 26 in upper:
yield o + k % 26
# If it's in the lower case and
# that the rotation is within the lowercase
elif o in lower and o + k % 26 in lower:
yield o + k % 26
# Otherwise move back 26 spaces after rotation.
else: # alphabet.
yield o + k % 26 -26
x = (''.join(map(chr, ceasar(s, k))))
print (x)
Batteries included
while 1:
phrase = raw_input("Could you please give me a phrase to encrypt?\n")
if phrase == "" : break
print "Here it is your phrase, encrypted:"
print phrase.encode("rot_13")
print "Have a nice afternoon!"
https://docs.python.org/2/library/codecs.html#python-specific-encodings
Python 3 update
The fine docs say
[Now the rot_13] codec provides a text transform: a str to str mapping. It is not supported by str.encode() (which only produces bytes output).
Or, in other words, you have to import encode from the codecs module and use it with the string to be encoded as its first argument
from codecs import decode
...
print(encode(phrase, 'rot13'))
The problem is that you set cipherText to empty string at every cycle iteration, the line
cipherText = ""
must be moved before the loop.
As pointed by others, you were resetting the cipherText in the iteration of the for loop. Placing cipherText before the start of the for loop will solve your problem.
Additionally, there is an alternate approach to solving this problem using Python's Standard library. The Python Standard Library defines a function maketrans() and a method translate that operates on strings.
The function maketrans() creates translation tables that can be used with the translate method to change one set of characters to another more efficiently. (Quoted from The Python Standard Library by Example).
import string
def caesar(plaintext, shift):
shift %= 26 # Values greater than 26 will wrap around
alphabet_lower = string.ascii_lowercase
alphabet_upper = string.ascii_uppercase
shifted_alphabet_lower = alphabet_lower[shift:] + alphabet_lower[:shift]
shifted_alphabet_upper = alphabet_upper[shift:] + alphabet_upper[:shift]
alphabet = alphabet_lower + alphabet_upper
shifted_alphabet = shifted_alphabet_lower + shifted_alphabet_upper
table = string.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
Here, a more functional way:
(if you use shift i to encode, then use -i to decode)
def ceasar(story, shift):
return ''.join([ # concentrate list to string
(lambda c, is_upper: c.upper() if is_upper else c) # if original char is upper case than convert result to upper case too
(
("abcdefghijklmnopqrstuvwxyz"*2)[ord(char.lower()) - ord('a') + shift % 26], # rotate char, this is extra easy since Python accepts list indexs below 0
char.isupper()
)
if char.isalpha() else char # if not in alphabet then don't change it
for char in story
])
plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))
def caesar(plainText, shift):
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
#####HERE YOU RESET CIPHERTEXT IN EACH ITERATION#####
cipherText = ""
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
caesar(plainText, shift)
As an else to if ch.isalpha() you can put finalLetter=ch.
You should remove the line: cipherText = ""
Cheers.
As #I82much said, you need to take cipherText = "" outside of your for loop. Place it at the beginning of the function. Also, your program has a bug which will cause it to generate encryption errors when you get capital letters as input. Try:
if ch.isalpha():
finalLetter = chr((ord(ch.lower()) - 97 + shift) % 26 + 97)
>>> def rotate(txt, key):
... def cipher(i, low=range(97,123), upper=range(65,91)):
... if i in low or i in upper:
... s = 65 if i in upper else 97
... i = (i - s + key) % 26 + s
... return chr(i)
... return ''.join([cipher(ord(s)) for s in txt])
# test
>>> rotate('abc', 2)
'cde'
>>> rotate('xyz', 2)
'zab'
>>> rotate('ab', 26)
'ab'
>>> rotate('Hello, World!', 7)
'Olssv, Dvysk!'
I have a hard time remember the char to int conversions so this could be optimized
def decryptCaesar(encrypted, shift):
minRange = ord('a')
decrypted = ""
for char in encrypted:
decrypted += chr(((ord(char) - minRange + shift) % 26) + minRange)
return decrypted
def encrypt():
plainText = input("What is your plaintext? ")
shift = int(input("What is your shift? "))
cipherText = ""
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText += finalLetter
print ("Your ciphertext is: ", cipherText,"with a shift of",shift)
def decrypte():
encryption=input("enter in your encrypted code")
encryption_shift=int(input("enter in your encryption shift"))
cipherText1 = ""
for c in encryption:
if c.isalpha():
stayInAlphabet1 = ord(c) - encryption_shift
if stayInAlphabet1 > ord('z'):
stayInAlphabet1 += 26
finalLetter1 = chr(stayInAlphabet1)
cipherText1 += finalLetter1
print ("Your ciphertext is: ", cipherText1,"with negative shift of",encryption_shift)
from tkinter import *
menu=Tk()
menu.title("menu")
menu.geometry("300x300")
button1= Button(menu,text="encrypt",command=encrypt)
button1.pack()
button2= Button(menu,text="decrypt",command=decrypte)
button2.pack()
button3= Button(menu,text="exit",command=exit)
button3.pack()
menu.mainloop()
message = 'The quick brown fox jumped over the lazy dog. 1234567890 !##$%^&*()_+-'
encrypted = ''.join(chr(ord(char)+3) for char in message)
decrypted = ''.join(chr(ord(char)-3) for char in encrypted)
print(encrypted)
print(decrypted)
# Wkh#txlfn#eurzq#ir{#mxpshg#ryhu#wkh#od}|#grj1#456789:;<3#$C&'(a)-+,b.0
# The quick brown fox jumped over the lazy dog. 1234567890 !##$%^&*()_+-
def encrypt(text,shift):
'''
INPUT: text as a string and an integer for the shift value.
OUTPUT: The shifted text after being run through the Caeser cipher.
'''
# Create a placeholder list
encrypted_text = list(range(len(text)))
alphabet = string.ascii_lowercase
# Create shifted alphabet
first_half = alphabet[:shift]
second_half = alphabet[shift:]
shifted_alphabet = second_half+first_half
for i,letter in enumerate(text.lower()):
# Check for spaces or punctuation
if letter in alphabet:
# Find the original index position
original_index = alphabet.index(letter)
# Shifted letter
new_letter = shifted_alphabet[original_index]
encrypted_text[i] = new_letter
# Punctuation or space
else:
encrypted_text[i] = letter
return ''.join(encrypted_text)
For example, decod string:
"uo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!".
This message has an offset of 10.
Code below:
import string
alphabet = list(string.ascii_lowercase)
print(alphabet, len(alphabet))
messege = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!"
messege_split = messege.split()
print(messege_split)
encrypted_messege = ""
position = 0
for i in messege_split:
for j in i:
if ord(j) < 65:
encrypted_messege += j
else:
for k in alphabet:
if j == k:
position = alphabet.index(k)
if (position + 10) >= len(alphabet):
encrypted_messege += alphabet[abs((position + 10) - len(alphabet))]
else:
encrypted_messege += alphabet[position + 10]
encrypted_messege += " "
print(encrypted_messege)
Decoded string:
"hey there! this is an example of a caesar cipher. were you able to decode it? i hope so! send me a message back with the same offset!"
TRY IT!
Using cyclic generator:
import string
from itertools import cycle
def caesarCipherEncryptor(s, key):
def generate_letters():
yield from cycle(string.ascii_lowercase)
def find_next(v, g, c):
# Eat up characters until we arrive at the plaintext character
while True:
if v == next(g):
break
# Increment the plaintext character by the count using the generator
try:
for _ in range(c):
item = next(g)
return item
except UnboundLocalError:
return v
return "".join([find_next(i, generate_letters(), key) for i in s])
# Outputs
>>> caesarCipherEncryptor("xyz", 3)
>>> 'abc'
from string import ascii_lowercase as alphabet
class CaesarCypher:
alpha_len = len(alphabet)
min_guess_rate = 0.2
Encryption and decryption is a same stuff. when you want to decrypt for example with shift 10 that means that you can encrypt it with shift 26 - 10. In this case cycle will repeat at if you going to shift whole alphabet it will be the same. Also here i've proceed upper case and non chars
def __call__(self, text, offset, encrypt=True):
if not encrypt:
offset = self.alpha_len - offset
result = []
for letter in text:
if not letter.isalpha():
result.append(letter)
continue
letter_to_process = letter.lower()
processed_letter = self._encrypt_letter(letter_to_process, offset)
if letter.isupper():
processed_letter = processed_letter.upper()
result.append(processed_letter)
return ''.join(result)
all encryption goes here at most.
def _encrypt_letter(self, letter, offset=0):
position = (alphabet.find(letter) + offset) % self.alpha_len
return alphabet[position]
this part is for broot force and guess throug dictionary frequency.
#staticmethod
def __how_many_do_i_know(text):
clean_words = filter(lambda x: x.isalpha(), text.split())
clean_words = ['\'{}\''.format(x) for x in clean_words]
cursor = conn.cursor()
query = 'SELECT COUNT(*) FROM mydictionary WHERE word IN ({})'.format(",".join(clean_words))
cursor.execute(query)
response = cursor.fetchone()[0]
return response / len(clean_words)
def guess_encode(self, text):
options = [self(text, offset, encrypt=False) for offset in range(self.alpha_len)]
best_option = [self.__how_many_do_i_know(option) for option in options]
best_key, guess_rate = max(enumerate(best_option), key=lambda x: x[-1])
guess_text = options[best_key]
return best_key, guess_rate, guess_text
import string
wrd=raw_input("Enter word").lower()
fwrd=""
for let in wrd:
fwrd+=string.ascii_lowercase[(string.ascii_lowercase).index(let)+3]
print"Original word",wrd
print"New word",fwrd
according to me this answer is useful for you:
def casear(a,key):
str=""
if key>26:
key%=26
for i in range(0,len(a)):
if a[i].isalpha():
b=ord(a[i])
b+=key
#if b>90: #if upper case letter ppear in your string
# c=b-90 #if upper case letter ppear in your string
# str+=chr(64+c) #if upper case letter ppear in your string
if b>122:
c=b-122
str+=chr(96+c)
else:
str+=chr(b)
else:
str+=a[i]
print str
a=raw_input()
key=int(input())
casear(a,key)
This function shifts all letter to right according to given key.
Why not use the function reverse on the shift input, and and join the plain_text with the shift, and input it as the cipher text:
Plain = int(input("enter a number "))
Rev = plain[::-1]
Cipher = " ".join(for cipher_text in Rev)
The code is very large, but easy to understand. I think it fits your situation.
alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
class CaesarCipher(object):
def __init__(self, shift):
self.shift = shift
def encode(self, str):
encode = ''
str = str.lower()
for i in str:
if i in alphabet:
encode += alphabet[alphabet.index(i) + self.shift]
else:
encode += i
return encode.upper()
def decode(self, str):
decode = ''
str = str.lower()
for i in str:
if i in alphabet:
decode += alphabet[alphabet.index(i) - self.shift]
else:
decode += i
return decode.upper()
Using map:
def caesar(text, key):
return ''.join(map(lambda c:
chr((ord(c.lower()) - ord('a') + key) % 26 + ord('a')) if c.isalpha() else ''
, text))
This solution is more intuitively without the use of ord function:
def caesar_cipher(raw_text, key):
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
shifted_alphabet = alphabet[26-key:]+alphabet[0:(26-key)]
cipher_text = ""
for i in range(len(raw_text)):
char = raw_text[i]
idx = alphabet.find(char.upper())
if idx == -1:
cipher_text = cipher_text + char
elif char.islower():
cipher_text = cipher_text + shifted_alphabet[idx].lower()
else:
cipher_text = cipher_text + shifted_alphabet[idx]
return(cipher_text)
And an example:
plain_text = "The quick brown fox jumps over the lazy dog!"
caesar_cipher(plain_text,3)
And we get:
'Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald!'
If we want to decrypt it:
caesar_cipher(caesar_cipher(plain_text,3),26-3)
and we get:
'The quick brown fox jumps over the lazy dog!'
More details here:https://predictivehacks.com/caesar-cipher-in-python/
caesar-cipher
message = str(input("Enter you message:"))
shift = int(input("Enter a number:"))
# encode
stringValue = [ord(message) - 96 for message in message]
print(stringValue)
encode_msg_val = []
[encode_msg_val.append(int(stringValue[i])+shift) for i in
range(len(stringValue))]
encode_msg_array = []
for i in range(len(encode_msg_val)):
encode_val = encode_msg_val[i] + 96
encode_msg_array.append(chr(encode_val))
print(encode_msg_array)
encode_msg = ''.join(encode_msg_array)
# dedcode
[deocde_msg_val = [ord(encode_msg) - 96 for encode_msg in encode_msg]
decode_val = []
[decode_val.append(deocde_msg_val[i] - shift) for i in
range(len(deocde_msg_val))]
decode_msg_array = []
[decode_msg_array.append(decode_val[i] + 96) for i in range(len(decode_val))]
decode_msg_list = []
[decode_msg_list.append(chr(decode_msg_array[i])) for i in
range(len(decode_msg_array))]
decode_msg = ''.join(decode_msg_list)
print(decode_msg)
alph = 'abcdefghijklmnopqrstuvwxyz'
# shift = int(input("Please enter the number of places to shift:"))
shift = 15
text = "python is fun!"
alph_len = len(alph)
if shift >=0 and shift <= alph_len:
# text = input("Please enter a sentence:")
shifted_alph = alph[shift:] + alph[:shift] # rotate
text = text.lower()
crypted_text = ""
for letter in text:
if letter in alph:
ind = alph.index(letter)
crypted_letter = shifted_alph[ind]
else:
crypted_letter = letter
crypted_text += crypted_letter
print(crypted_text)
else:
print(f"You need to enter a number between 0 and {alph_len}!")
# eniwdc xh ujc! # output
key = 3
def wub():
def choice():
choice = input("Do you wish to Encrypt of Decrypt?")
choice = choice.lower()
if choice == "e" or "encrypt":
return choice
elif choice == "d" or "decrypt":
return choice
else:
print("Invalid response, please try again.")
choice()
def message():
user = input("Enter your message: ")
return user
def waffle(choice, message, key):
translated = ""
if choice == "e" or "encrypt":
for character in message:
num = ord(character)
num += key
translated += chr(num)
derek = open('Encrypted.txt', 'w')
derek.write(translated)
derek.close()
return translated
else:
for character in message:
num = ord(character)
num -= key
translated += chr(num)
return translated
choice = choice() #Runs function for encrypt/decrypt selection. Saves choice made.
message = message() #Run function for user to enter message. Saves message.
final = waffle(choice, message, key) #Runs function to translate message, using the choice, message and key variables)
print("\n Operation complete!")
print(final)
wub()

Categories

Resources