Skip to content

Instantly share code, notes, and snippets.

@nchitalov
Created February 24, 2015 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nchitalov/2f2b03e5cf1e19da1525 to your computer and use it in GitHub Desktop.
Save nchitalov/2f2b03e5cf1e19da1525 to your computer and use it in GitHub Desktop.
def caesar_encrypt(realText, step):
outText = []
cryptText = []
uppercase = ['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']
lowercase = ['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']
for eachLetter in realText:
if eachLetter in uppercase:
index = uppercase.index(eachLetter)
crypting = (index + step) % 26
cryptText.append(crypting)
newLetter = uppercase[crypting]
outText.append(newLetter)
elif eachLetter in lowercase:
index = lowercase.index(eachLetter)
crypting = (index + step) % 26
cryptText.append(crypting)
newLetter = lowercase[crypting]
outText.append(newLetter)
return outText
code = caesar_encrypt('abc', 2)
print(code)
@VishyNilesh
Copy link

VishyNilesh commented Aug 12, 2022

What do you think about this logic is it missing something or does the job?

actual_data = input("Enter the string to cipher using ceaser encryption ")
selection_shift = input ('Enter the type of shift you want to apply [Left | Right] ')
encrypted_data=''
if selection_shift.lower()=='left':
n = int(input('no of characters to shift by '))
for x in actual_data:
encrypted_data += x.replace(x,chr(ord(x)-n))
else:
n = int(input('no of characters to shift by '))
for x in actual_data:
encrypted_data += x.replace(x,chr(ord(x)+n))
print(encrypted_data)

@boyLern
Copy link

boyLern commented Nov 15, 2022

(crypting = (index + step) % 26) in the above code, what is the role of "% 26". How does it work to give the right answer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment