forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
525b945
commit f68690f
Showing
5 changed files
with
133 additions
and
53 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,53 @@ | ||
message = input("Encrypted message: ") | ||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
def decrypt(message): | ||
""" | ||
>>> decrypt('TMDETUX PMDVU') | ||
Decryption using Key #0: TMDETUX PMDVU | ||
Decryption using Key #1: SLCDSTW OLCUT | ||
Decryption using Key #2: RKBCRSV NKBTS | ||
Decryption using Key #3: QJABQRU MJASR | ||
Decryption using Key #4: PIZAPQT LIZRQ | ||
Decryption using Key #5: OHYZOPS KHYQP | ||
Decryption using Key #6: NGXYNOR JGXPO | ||
Decryption using Key #7: MFWXMNQ IFWON | ||
Decryption using Key #8: LEVWLMP HEVNM | ||
Decryption using Key #9: KDUVKLO GDUML | ||
Decryption using Key #10: JCTUJKN FCTLK | ||
Decryption using Key #11: IBSTIJM EBSKJ | ||
Decryption using Key #12: HARSHIL DARJI | ||
Decryption using Key #13: GZQRGHK CZQIH | ||
Decryption using Key #14: FYPQFGJ BYPHG | ||
Decryption using Key #15: EXOPEFI AXOGF | ||
Decryption using Key #16: DWNODEH ZWNFE | ||
Decryption using Key #17: CVMNCDG YVMED | ||
Decryption using Key #18: BULMBCF XULDC | ||
Decryption using Key #19: ATKLABE WTKCB | ||
Decryption using Key #20: ZSJKZAD VSJBA | ||
Decryption using Key #21: YRIJYZC URIAZ | ||
Decryption using Key #22: XQHIXYB TQHZY | ||
Decryption using Key #23: WPGHWXA SPGYX | ||
Decryption using Key #24: VOFGVWZ ROFXW | ||
Decryption using Key #25: UNEFUVY QNEWV | ||
""" | ||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
for key in range(len(LETTERS)): | ||
translated = "" | ||
for symbol in message: | ||
if symbol in LETTERS: | ||
num = LETTERS.find(symbol) | ||
num = num - key | ||
if num < 0: | ||
num = num + len(LETTERS) | ||
translated = translated + LETTERS[num] | ||
else: | ||
translated = translated + symbol | ||
print("Decryption using Key #%s: %s" % (key, translated)) | ||
|
||
message = message.upper() | ||
def main(): | ||
message = input("Encrypted message: ") | ||
message = message.upper() | ||
decrypt(message) | ||
|
||
for key in range(len(LETTERS)): | ||
translated = "" | ||
for symbol in message: | ||
if symbol in LETTERS: | ||
num = LETTERS.find(symbol) | ||
num = num - key | ||
if num < 0: | ||
num = num + len(LETTERS) | ||
translated = translated + LETTERS[num] | ||
else: | ||
translated = translated + symbol | ||
|
||
print("Decryption using Key #%s: %s" % (key, translated)) | ||
if __name__ == '__main__': | ||
import doctest | ||
doctest.testmod() | ||
main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,51 @@ | ||
# The Caesar Cipher Algorithm | ||
|
||
message = input("Enter message: ") | ||
key = int(input("Key [1-26]: ")) | ||
mode = input("Encrypt or Decrypt [e/d]: ") | ||
|
||
if mode.lower().startswith('e'): | ||
mode = "encrypt" | ||
elif mode.lower().startswith('d'): | ||
mode = "decrypt" | ||
|
||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
translated = "" | ||
|
||
message = message.upper() | ||
|
||
for symbol in message: | ||
if symbol in LETTERS: | ||
num = LETTERS.find(symbol) | ||
if mode == "encrypt": | ||
num = num + key | ||
elif mode == "decrypt": | ||
num = num - key | ||
|
||
if num >= len(LETTERS): | ||
num = num - len(LETTERS) | ||
elif num < 0: | ||
num = num + len(LETTERS) | ||
|
||
translated = translated + LETTERS[num] | ||
else: | ||
translated = translated + symbol | ||
|
||
if mode == "encrypt": | ||
print("Encryption:", translated) | ||
elif mode == "decrypt": | ||
print("Decryption:", translated) | ||
def main(): | ||
message = input("Enter message: ") | ||
key = int(input("Key [1-26]: ")) | ||
mode = input("Encrypt or Decrypt [e/d]: ") | ||
|
||
if mode.lower().startswith('e'): | ||
mode = "encrypt" | ||
elif mode.lower().startswith('d'): | ||
mode = "decrypt" | ||
|
||
translated = encdec(message, key, mode) | ||
if mode == "encrypt": | ||
print("Encryption:", translated) | ||
elif mode == "decrypt": | ||
print("Decryption:", translated) | ||
|
||
def encdec(message, key, mode): | ||
""" | ||
>>> encdec('Harshil Darji', 12, 'encrypt') | ||
'TMDETUX PMDVU' | ||
>>> encdec('TMDETUX PMDVU', 12, 'decrypt') | ||
'HARSHIL DARJI' | ||
""" | ||
message = message.upper() | ||
translated = "" | ||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
for symbol in message: | ||
if symbol in LETTERS: | ||
num = LETTERS.find(symbol) | ||
if mode == "encrypt": | ||
num = num + key | ||
elif mode == "decrypt": | ||
num = num - key | ||
|
||
if num >= len(LETTERS): | ||
num = num - len(LETTERS) | ||
elif num < 0: | ||
num = num + len(LETTERS) | ||
|
||
translated = translated + LETTERS[num] | ||
else: | ||
translated = translated + symbol | ||
return translated | ||
|
||
if __name__ == '__main__': | ||
import doctest | ||
doctest.testmod() | ||
main() |
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
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
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