Created
February 24, 2015 14:57
-
-
Save nchitalov/2f2b03e5cf1e19da1525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
(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
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)