프로그래밍 언어/Python
[Python] Encrypt / Decrypt
ShovelingLife
2024. 7. 10. 18:47
alphabet = []
def Init():
idx = 0
for i in range(97, 123):
alphabet.append(chr(i))
def Encrypt(plainTxt, shiftAmt, code = 0):
cipherTxt = ""
for letter in plainTxt:
pos = alphabet.index(letter)
newPos = pos + shiftAmt if code == 0 else pos - shiftAmt
cipherTxt += alphabet[newPos]
return cipherTxt
def Decrypt(plainTxt, shiftAmt):
return Encrypt(plainTxt, shiftAmt, 1)
Init()
dir = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
if not dir == 'encode' and not dir == 'decode':
print("Try Again")
exit()
txt = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
cipherTxt = Encrypt(txt, shift) if dir == "encode" else Decrypt(txt, shift)
print(f"The new text is : {cipherTxt}")