mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
File rename
This commit is contained in:
38
ciphers/caesar_cipher.py
Normal file
38
ciphers/caesar_cipher.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# 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)
|
||||
62
ciphers/simple_substitution_cipher.py
Normal file
62
ciphers/simple_substitution_cipher.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import sys, random
|
||||
|
||||
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
|
||||
def main():
|
||||
message = input('Enter message: ')
|
||||
key = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'
|
||||
resp = input('Encrypt/Decrypt [e/d]: ')
|
||||
|
||||
checkValidKey(key)
|
||||
|
||||
if resp.lower().startswith('e'):
|
||||
mode = 'encrypt'
|
||||
translated = encryptMessage(key, message)
|
||||
elif resp.lower().startswith('d'):
|
||||
mode = 'decrypt'
|
||||
translated = decryptMessage(key, message)
|
||||
|
||||
print('\n%sion: \n%s' % (mode.title(), translated))
|
||||
|
||||
def checkValidKey(key):
|
||||
keyList = list(key)
|
||||
lettersList = list(LETTERS)
|
||||
keyList.sort()
|
||||
lettersList.sort()
|
||||
|
||||
if keyList != lettersList:
|
||||
sys.exit('Error in the key or symbol set.')
|
||||
|
||||
def encryptMessage(key, message):
|
||||
return translateMessage(key, message, 'encrypt')
|
||||
|
||||
def decryptMessage(key, message):
|
||||
return translateMessage(key, message, 'decrypt')
|
||||
|
||||
def translateMessage(key, message, mode):
|
||||
translated = ''
|
||||
charsA = LETTERS
|
||||
charsB = key
|
||||
|
||||
if mode == 'decrypt':
|
||||
charsA, charsB = charsB, charsA
|
||||
|
||||
for symbol in message:
|
||||
if symbol.upper() in charsA:
|
||||
symIndex = charsA.find(symbol.upper())
|
||||
if symbol.isupper():
|
||||
translated += charsB[symIndex].upper()
|
||||
else:
|
||||
translated += charsB[symIndex].lower()
|
||||
else:
|
||||
translated += symbol
|
||||
|
||||
return translated
|
||||
|
||||
def getRandomKey():
|
||||
key = list(LETTERS)
|
||||
random.shuffle(key)
|
||||
return ''.join(key)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
43
ciphers/transposition_cipher.py
Normal file
43
ciphers/transposition_cipher.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import math
|
||||
|
||||
def main():
|
||||
message = input('Enter message: ')
|
||||
key = int(input('Enter key [2-%s]: ' % (len(message) - 1)))
|
||||
mode = input('Encryption/Decryption [e/d]: ')
|
||||
|
||||
if mode.lower().startswith('e'):
|
||||
text = encryptMessage(key, message)
|
||||
elif mode.lower().startswith('d'):
|
||||
text = decryptMessage(key, message)
|
||||
|
||||
# Append pipe symbol (vertical bar) to identify spaces at the end.
|
||||
print('Output:\n%s' %(text + '|'))
|
||||
|
||||
def encryptMessage(key, message):
|
||||
cipherText = [''] * key
|
||||
for col in range(key):
|
||||
pointer = col
|
||||
while pointer < len(message):
|
||||
cipherText[col] += message[pointer]
|
||||
pointer += key
|
||||
return ''.join(cipherText)
|
||||
|
||||
def decryptMessage(key, message):
|
||||
numCols = math.ceil(len(message) / key)
|
||||
numRows = key
|
||||
numShadedBoxes = (numCols * numRows) - len(message)
|
||||
plainText = [""] * numCols
|
||||
col = 0; row = 0;
|
||||
|
||||
for symbol in message:
|
||||
plainText[col] += symbol
|
||||
col += 1
|
||||
|
||||
if (col == numCols) or (col == numCols - 1) and (row >= numRows - numShadedBoxes):
|
||||
col = 0
|
||||
row += 1
|
||||
|
||||
return "".join(plainText)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user