CAESAR CIPHER - PE


def encrypt(plaintext, shift):
result = ""
for char in plaintext:
if 'A' <= char <= 'Z':
result += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
elif 'a' <= char <= 'z':
result += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
elif '0' <= char <= '9':
result += chr((ord(char) - ord('0') + shift) % 10 + ord('0'))
else:
result += char
return result

def decrypt(ciphertext, shift):
result = ""
for char in ciphertext:
if 'A' <= char <= 'Z':
result += chr((ord(char) - ord('A') - shift + 26) % 26 + ord('A'))
elif 'a' <= char <= 'z':
result += chr((ord(char) - ord('a') - shift + 26) % 26 + ord('a'))
elif '0' <= char <= '9':
result += chr((ord(char) - ord('0') - shift + 10) % 10 + ord('0'))
else:
result += char
return result

Main interaction

def main():
choice = input("Do you want to (E)ncrypt or (D)ecrypt? ").strip().upper()
message = input("Enter the message: ")
shift = int(input("Enter the shift value: "))

if choice == 'E':
    encrypted = encrypt(message, shift)
    print("Encrypted message:", encrypted)
elif choice == 'D':
    decrypted = decrypt(message, shift)
    print("Decrypted message:", decrypted)
else:
    print("Invalid choice. Please enter E or D.")

if name == "main":
main()