I am trying to make a decrypt script for a Caeser cipher script to encrypt but can't get it to work. This is my encryption script:
def encrypt(text,s):
result = ""
# traverse text
for i in range(len(text)):
char = text[i]
# Encrypt uppercase characters
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
# Encrypt lowercase characters
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
#def decrypt(ciphertext, s):
text = "0123456789"
s = 4
Cipher=encrypt(text,s)
print("Text : " + text)
print ("Shift : " + str(s))
print ("Cipher: " + encrypt(text,s))
I need help with creating a decrypt script the same way.
Thanks in advance!
Here is some code of mine for a function used to decrypt a ceaser cipher. The approach used when the shift is not known is simply to get every possible value, then pick the one with more then half the decoded words being in the English dictionary. For non alphabetic characters, there is a placeholder character used.
There are some extra parts required, such as the dictionary or extra functions / lists so I will include them in the below links.
A note, it does only work for on lowercase letters however, I do hope it will help you to understand the logic a bit better.
def Decode(message, shift=-1):
"""
Decodes a message from a caesar cipher
Params:
- message (str) : Message to decode
Optional Params:
- shift (int) : If shift is known
Returns:
- decodedMessage (str) : The decoded message
"""
decodedMessage = ''
message = message.lower() # lowercase is easier to work with
# If the shift is known it is simple to decode
if shift != -1:
for letterIndex in range(len(message)):
if message[letterIndex] in [' ', '!', '?', '.', '-', ',', '_']:
decodedMessage += message[letterIndex]
else:
try:
index = ALPHABET.index(message[letterIndex]) - shift
# If the index is smaller then ALPHABET, handle it
while index < 0:
index += len(ALPHABET)
decodedMessage += ALPHABET[index]
except Exception as e:
print("A problem occured:", e)
decodedMessage += '?'
return decodedMessage
else: #If shift is not known, figure it out thru brute force
data = read_json('words_dictionary')
for i in range(len(ALPHABET)):
decodedMessage = Decode(message, i+1)
wordList = decodedMessage.split(" ")
try:
# Loop over words counting english words
count = 0
for word in wordList:
if word in data.keys():
count += 1
# More accurate this way compared to only one word checks
if count > len(wordList) / 2:
return decodedMessage
except KeyError:
continue
Full code here
Word dictionary here
Related
I have the following caesar code function: I want to be able to undo the cyphering and return the original word, what i have here works for the word "hello" it cyphers the word as "xvdei", and decyphers it as "hello". However it will not work for any other words i put, i want to know how i can go about to edit this out so that any word i put will decypher back into the original word.So like if i put xvdei i will get back "hello" or if i put in hijklm and shift it by 7 i will get back "aaaaaa"
alphabet=['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 decypher(text,shift):
# initialize ciphertext as blank string
dtext = ""
# loop through the length of the plaintext
for i in range(len(text)):
# get the ith letter from the plaintext
l = text[i]
# find the number position of the ith letter
n_alphabet = alphabet.index(l)
# find the number position of the cipher by adding the shift
c_num = (n_alphabet + shift ) % len(alphabet) - 6 - i
# find the cipher letter for the cipher number you computed
c_letter = alphabet[c_num]
# add the cipher letter to the ciphertext
dtext = dtext + c_letter
# return the computed ciphertext
return dtext
I need to decypher what this function gives me to the original word:
def caesar(plaintext,shift):
# initialize ciphertext as blank string
ciphertext = ""
# loop through the length of the plaintext
for i in range(len(plaintext)):
# get the ith letter from the plaintext
letter = plaintext[i]
# find the number position of the ith letter
num_in_alphabet = alphabet.index(letter)
# find the number position of the cipher by adding the shift
cipher_num = (num_in_alphabet + shift + i) % len(alphabet)
# find the cipher letter for the cipher number you computed
cipher_letter = alphabet[cipher_num]
# add the cipher letter to the ciphertext
ciphertext = ciphertext + cipher_letter
# return the computed ciphertext
return ciphertext
Reversing the cypher only needs to continue shifting up to the point where it reaches a complete cycle (i.e. by the rest of the letters):
def decypher(text,shift): return cypher(text,len(alphabet)-shift)
also, in your code:
cipher_num = (num_in_alphabet + shift + i) % len(alphabet)
should be
cipher_num = (num_in_alphabet + shift) % len(alphabet)
def caesar_cipher(offset, string):
words = string.replace(" ", " ")
cipher_chars = "abcdefghijklmnopqrstuvwxyz"
word_i = 0
while word_i < len(words):
word = words[word_i]
letter_i = 0
while letter_i < len(word):
char_i = ord(word[letter_i]) - ord("c")
new_char_i = (char_i + offset) % 26
value = chr(new_char_i + ord("c"))
letter_i += 1
word_i += 1
return words.join(value)
print caesar_cipher(3, "abc")
Hey everyone, for some reason my ceasar cipher is only printing the last letter in my string, when I want it to cipher the whole string, for example, if i print an offset of 3 with string "abc" it should print def, but instead is just printing the f. Any help is greatly appreciated!
value is overwritten in the loop. You want to create a list passed to join (ATM you're joining only 1 character):
value = []
then
value.append(chr(new_char_i + ord("c")))
the join statement is also wrong: just do:
return "".join(value)
Note that there are other issues in your code. It seems to intent to process several words, but it doesn't, so a lot of loops don't loop (there's no list of words, it's just a word), so what you are doing could be summarized to (using a simple list comprehension):
def caesar_cipher(offset, string):
return "".join([chr((ord(letter) - ord("c") + offset) % 26 + ord("c")) for letter in string])
and for a sentence:
print(" ".join([caesar_cipher(3, w) for w in "a full sentence".split()]))
As a nice commenter noted, using c as start letter is not correct since it trashes sentences containing the 3 last letters. There's no reason not to start by a (the result are the same for the rest of the letters):
def caesar_cipher(offset, string):
return "".join([chr((ord(letter) - ord("a") + offset) % 26 + ord("a")) for letter in string])
Aside: a quick similar algorithm is rot13. Not really a cipher but it's natively supported:
import codecs
print(codecs.encode("a full sentence","rot13"))
(apply on the encoded string to decode it)
My question is how to improve the code so that it can adapt to however long the input message is. As is, the message must be 5 letters. I would like to improve the code such that a message of any length can be inputted and the cipher will work with it. Help would be much appreciated. :-) See the code below!
#Enter your message
message=raw_input('Enter your message here. Make sure to use all CAPS througout your message and leave no spaces in between words.')
length=len(message)
print 'The length of your message is ',length
#This statement is a possible idea to let the program know how many letters it will be need to shift. But I don't know how to actually do this.
print chr(length+64)
#Indexes letters out of message.
A=message[length-length]
B=message[length-length+1]
C=message[length-length+2]
D=message[length-length+3]
E=message[length-length+4]
#Shifts letters and accounts for shifting XYZ to ABC.
def shift(x):
if ord(x)+3==91:
return 65
if ord(x)+3==92:
return 66
if ord(x)+3==93:
return 67
else:
return ord(x)+3
a2=shift(A)
b2=shift(B)
c2=shift(C)
d2=shift(D)
e2=shift(E)
#Converts shifted ordinals back to characters
def convert(x):
return chr(x)
first=convert(a2)
second=convert(b2)
third=convert(c2)
fourth=convert(d2)
fifth=convert(e2)
#Prints resultant characters
print first,second,third,fourth,fifth
import string
shift_amt = 13
alphabet_lc = string.ascii_lowercase
shifted_lc = alphabet_lc[shift_amt:]+alphabet_lc[:shift_amt]
alphabet_uc = alphabet_lc.upper()
shifted_uc = shifted_lc.upper()
trans_tab = string.maketrans(alphabet_lc+alphabet_uc,shifted_lc+shifted_uc)
message = "Encode Me To a new MessaGez!"
print message.translate(trans_tab)
is one way of doing it in Python2 at least
Use two for loops, one for looping through each character, and one for shifting the character the desired amount of times. We use a function upper() to shift a character.
def upper(char):
from string import ascii_letters as _all
if char == ' ':
return ' '
return _all[_all.index(char)+1] if char != 'Z' else 'a'
def shift(message, key):
temp = []
for i in message:
char = i
for k in range(key):
char = upper(char)
temp.append(char)
return ''.join(temp)
message=raw_input('Enter your message here: ')
key = int(raw_input('Enter the desired key: '))
length=len(message)
print 'The length of your message is', length
print 'Your encrypted message is {0}'.format(shift(message, key))
This runs as:
bash-3.2$ python caesar.py
Enter your message here: This works WITH or without CAPS
Enter the desired key: 10
The length of your message is 31
Your encrypted message is drsC GyBuC gSdR yB GsDryED MKZc
bash-3.2$
The Ceasar cipher is built in in python 2;
In [6]: 'The Caesar cipher is built-in.'.encode('rot13')
Out[6]: 'Gur Pnrfne pvcure vf ohvyg-va.'
As you can see, this encoding only acts on letters, and it works for upper and lower case.
But is you want to remove spaces and make every thing upper-case, Python can do that as well;
In [9]: 'this is a string'.translate(None, ' \t')
Out[9]: 'thisisastring'
In [10]: 'this is a string'.translate(None, ' \t').upper()
Out[10]: 'THISISASTRING'
In [11]: 'this is a string'.translate(None, ' \t').upper().encode('rot13')
Out[11]: 'GUVFVFNFGEVAT'
Or in a different way;
In [15]: ''.join('this is a string'.split()).upper().encode('rot13')
Out[15]: 'GUVFVFNFGEVAT'
Bibliography:
Kid Snippets: "Math Class" (Imagined by Kids) - YouTube http://youtu.be/KdxEAt91D7k
Mary Had A Little Lamb Nursery Rhyme With Lyrics - YouTube http://youtu.be/CkRdvGmcCBE
Mary Had a Little Lamb - Wikipedia, the free encyclopedia http://goo.gl/FNEuyd
Python source code:
Note: working for negative shift numbers also
Note: if reverse shift then we do encode - decode message
Note: preserving spaces also
small_chars = [chr(item) for item in range(ord('a'), ord('z')+1)]
upper_chars = [item.upper() for item in small_chars]
def encode_chr(chr_item, is_upper_case):
'''
Cipher each chr_item.
'''
# setting orig and end order.
if is_upper_case:
orig_ord = ord('A')
end_ord = ord('Z')
else:
orig_ord = ord('a')
end_ord = ord('z')
# calculating shift
temp_ord = ord(chr_item)+shift
# calculating offset order with modulo.
# char is after end_ord, calculating offset
num_of_chars = 26
offset_ord = (temp_ord - end_ord - 1)%num_of_chars
return chr(orig_ord + offset_ord)
# enable while loop to repeat until status not 'y'
status = 'y'
while status == 'y':
# enter word to cipher.
word = raw_input("Word: ")
# enter char shift
shift = input("Shift: ")
print
# create cipher list variable
cipher = list()
# loop trough each char in word
for chr_item in word:
# encode just letters.
# replace non-alfa with underscore: "_"
if chr_item in upper_chars or chr_item in small_chars:
# set is_uppser_case to True for upper case chars.
is_upper_case = (chr_item in upper_chars) and True
# cipher char.
temp_chr = encode_chr(chr_item, is_upper_case)
# append ciphered char to list
cipher.append(temp_chr)
elif chr_item is ' ':
cipher.append(chr_item)
else:
cipher.append('_')
# print word
print word
# print ciphered word
print ''.join(cipher)
# repeat again for another word?
status = raw_input("Repeat? [y|n]: ")
print
Test cases:
>>>
Word: aAzZ!#
Shift: 1
aAzZ!#
bBaA__
Repeat? [y|n]: y
Word: aAzZ#!
Shift: -1
aAzZ#!
zZyY__
Repeat? [y|n]: y
Word: aAzZ#$
Shift: 27
aAzZ#$
bBaA__
Repeat? [y|n]: y
Word: aAzZ%^
Shift: -27
aAzZ%^
zZyY__
Repeat? [y|n]: n
>>>
Output:
Note: if reverse shift then we do encode - decode message
>>>
Word: "Mary Had a Little Lamb"
Shift: 1
"Mary Had a Little Lamb"
_Nbsz Ibe b Mjuumf Mbnc_
Repeat? [y|n]: y
Word: _Nbsz Ibe b Mjuumf Mbnc_
Shift: -1
_Nbsz Ibe b Mjuumf Mbnc_
_Mary Had a Little Lamb_
Repeat? [y|n]: n
>>>
Here is a simple Caesar cipher program written for Python 3 that should not be very difficult to rewrite for Python 2:
import string
def main():
key = 5
table = str.maketrans(string.ascii_letters,
string.ascii_lowercase[key:] +
string.ascii_lowercase[:key] +
string.ascii_uppercase[key:] +
string.ascii_uppercase[:key])
plaintext = input('Please enter a phrase: ')
ciphertext = plaintext.translate(table)
print('Your encrypted phrase is:', ciphertext)
if __name__ == '__main__':
main()
I need help with my ceaser cipher code. I need to be able to shift/decrypt a letter even when the shift value is greater than 122. My code only works for shift values that are less than 22. The code fails when the user specifies an input for the shifter that is greater than 122 How can I do this? For a with shifter 123 the output should be r
k = int(raw_input("Please enter a value for k: ")) #Shifter number
original = raw_input("plaintext: ") #Message user wants ciphered
original_as_array = list(original) ##I am turning the input into an array
for i in range(0,len(original)): ##Now seperating each character to add k
char = original_as_array[i]
charint = ord(char)
if charint >= 65 and charint <=90:
cipher_int = ((charint-65 + k) % 26)+65
code_char = chr(cipher_int)
print code_char,
elif charint >= 97 and charint <=122:
cipher_int = ((charint-97 + k) % 26)+97
code_char = chr(cipher_int)
print code_char,
else:
print char,
#Caesar Cipher
#Function that reads and writes data to the file, and calls the encryption and decryption methods.
#It isolates the key and choice of the user, and then calls the appropriate function.
def isolate():
#Open input file in read mode
fin = open('Input3.txt', 'r')
#Open output file in write mode
fout = open('Out1.txt', 'w')
#Create a list that holds each line of the file as an element of the list
container = fin.readlines()
#For loop that goes through each line of the file (contianed in list) and isolates the choice and key of the user
#Then it removes the key and choice from the split input list and calls the appropriate method.
for i in container:
# List of words, with whitespace and trailing '\n' removed
words = i.split()
# Interpret and remove the last two words
key = int(words[len(words) - 1])
choice = words[len(words) - 2]
words.remove(str(key))
words.remove(choice)
# Re-join the words of the message. The message may differ from the original if there is consecutive whitespace.
message = ' '.join(words)
message = message.upper()
# Encrypt or decrypt to fout
if choice == 'e':
fout.write(encrypt(message, key) + '\n')
else:
fout.write(decrypt(message, key) + '\n')
#Close the file to make sure data is written properly to the file.
fout.close()
#Encryption method, which takes two parameters, the users message and key, and returns the newly encrypted message.
def encrypt(message, key):
#Empty string that will contain the encrypted message.
encrypted_message = ""
#Alphabet string, which contains all the letters of the alphabet at their index locations.
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#For loop that goes through each character of the users message and, if a space, adds it straight to the encrypted message or encrypts it through modular division.
for i in message:
#If the character is a space, add it to the final message.
if i == ' ':
#Concatenate the space to the final message
encrypted_message += ' '
#If the character is not a space, determine the final locatin out of index range, % by 26 and re-add the character at this index to the encrypted_message.
else:
#Determine the final location of the character (out of index range)
location = key + alpha.index(i)
#% this location by 26 to determine the location within the proper index.
location %= 26
#Finally, concatenate the letter at this location to the final message.
encrypted_message += alpha[location]
#When the function is called, have it return the final encrypted message.
return encrypted_message
#Decryption method that takes two parameters, the users message and key
def decrypt(message, key):
#Create a string that reverses the alphabet, since we are moving backwards when we decrypt.
reverse_alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[::-1]
decrypted_message = ""
#For loop that runs through each element of the users message and, if space, adds it to the message, or encrypts using % division, then concatenates to final message.
for i in message:
if i == ' ':
decrypted_message += ' '
else:
#Determine the final location out of index range of the encrypted character
location = key + reverse_alpha.index(i)
#% this position by 26 to determine the index location in range of the letter.
location %= 26
#Finally, add the letter at this location to the decrypted message.
decrypted_message += reverse_alpha[location]
#Converts the decrypted message into lowercase form.
decrypted_message = decrypted_message.lower()
return decrypted_message
#Call the function isolate5(), which initiates the program.
isolate()
Your code fails because you didn't specify a condition when ord(char) > 122. Instead, you threw it to the else clause.
You can just cut the part <= 122:
for k in range(20):
for i in range(0,len(original)): ##Now seperating each character to add k
char = original[i]
charint = ord(char)
if charint >= 65 and charint <=90:
cipher_int = ((charint-65 + k) % 26)+65
code_char = chr(cipher_int)
print code_char,
elif charint >= 97:
cipher_int = ((charint-97 + k) % 26)+97
code_char = chr(cipher_int)
print code_char,
else:
print char,
With k set to 18, this will produce r. If this solution wasn't what you meant, feel free to coment below!
I'm writing code so you can shift text two places along the alphabet: 'ab cd' should become 'cd ef'. I'm using Python 2 and this is what I got so far:
def shifttext(shift):
input=raw_input('Input text here: ')
data = list(input)
for i in data:
data[i] = chr((ord(i) + shift) % 26)
output = ''.join(data)
return output
shifttext(3)
I get the following error:
File "level1.py", line 9, in <module>
shifttext(3)
File "level1.py", line 5, in shifttext
data[i] = chr((ord(i) + shift) % 26)
TypError: list indices must be integers, not str
So I have to change the letter to numbers somehow? But I thought I already did that?
You are looping over the list of characters, and i is thus a character. You then try to store that back into data using the i character as an index. That won't work.
Use enumerate() to get indexes and the values:
def shifttext(shift):
input=raw_input('Input text here: ')
data = list(input)
for i, char in enumerate(data):
data[i] = chr((ord(char) + shift) % 26)
output = ''.join(data)
return output
You can simplify this with a generator expression:
def shifttext(shift):
input=raw_input('Input text here: ')
return ''.join(chr((ord(char) + shift) % 26) for char in input)
But now you'll note that your % 26 won't work; the ASCII codepoints start after 26:
>>> ord('a')
97
You'll need to use the ord('a') value to be able to use a modulus instead; subtracting puts your values in the range 0-25, and you add it again afterwards:
a = ord('a')
return ''.join(chr((ord(char) - a + shift) % 26) + a) for char in input)
but that will only work for lower-case letters; which might be fine, but you can force that by lowercasing the input:
a = ord('a')
return ''.join(chr((ord(char) - a + shift) % 26 + a) for char in input.lower())
If we then move asking for the input out of the function to focus it on doing one job well, this becomes:
def shifttext(text, shift):
a = ord('a')
return ''.join(chr((ord(char) - a + shift) % 26 + a) for char in text.lower())
print shifttext(raw_input('Input text here: '), 3)
and using this on the interactive prompt I see:
>>> print shifttext(raw_input('Input text here: '), 3)
Input text here: Cesarsalad!
fhvduvdodgr
Of course, now punctuation is taken along. Last revision, now only shifting letters:
def shifttext(text, shift):
a = ord('a')
return ''.join(
chr((ord(char) - a + shift) % 26 + a) if 'a' <= char <= 'z' else char
for char in text.lower())
and we get:
>>> print shifttext(raw_input('Input text here: '), 3)
Input text here: Ceasarsalad!
fhdvduvdodg!
Looks you're doing cesar-cipher encryption, so you can try something like this:
strs = 'abcdefghijklmnopqrstuvwxyz' #use a string like this, instead of ord()
def shifttext(shift):
inp = raw_input('Input text here: ')
data = []
for i in inp: #iterate over the text not some list
if i.strip() and i in strs: # if the char is not a space ""
data.append(strs[(strs.index(i) + shift) % 26])
else:
data.append(i) #if space the simply append it to data
output = ''.join(data)
return output
output:
In [2]: shifttext(3)
Input text here: how are you?
Out[2]: 'krz duh brx?'
In [3]: shifttext(3)
Input text here: Fine.
Out[3]: 'Flqh.'
strs[(strs.index(i) + shift) % 26]: line above means find the index of the character i in strs and then add the shift value to it.Now, on the final value(index+shift) apply %26 to the get the shifted index. This shifted index when passed to strs[new_index] yields the desired shifted character.
Martijn's answer is great. Here is another way to achieve the same thing:
import string
def shifttext(text, shift):
shift %= 26 # optional, allows for |shift| > 26
alphabet = string.lowercase # 'abcdefghijklmnopqrstuvwxyz' (note: for Python 3, use string.ascii_lowercase instead)
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
return string.translate(text, string.maketrans(alphabet, shifted_alphabet))
print shifttext(raw_input('Input text here: '), 3)
It's easier to write a straight function shifttext(text, shift). If you want a prompt, use Python's interactive mode python -i shift.py
> shifttext('hello', 2)
'jgnnq'
Tried with Basic python.
may useful for someone.
# Caesar cipher
import sys
text = input("Enter your message: ")
cipher = ''
try:
number = int(input("Enter Number to shift the value : "))
except ValueError:
print("Entered number should be integer. please re0enter the value")
try:
number = int(input("Enter Number to shift the value : "))
except:
print("Error occurred. please try again.")
sys.exit(2)
for char in text:
if not char.isalpha():
flag = char
elif char.isupper():
code = ord(char) + number
if 64 < code <= 90:
flag = chr(code)
elif code > 90:
flag = chr((code - 90) + 64)
elif char.islower():
code = ord(char) + number
if 96 < code <= 122:
flag = chr(code)
elif code > 122:
flag = chr((code - 122) + 96)
else:
print("not supported value by ASCII")
cipher += flag
print(cipher)