Skip to content

Commit

Permalink
Create caesar_cipher_encoder__decoder.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgvkamalakar authored Oct 23, 2023
1 parent 829ff77 commit e17f5cc
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions python/caesar_cipher_encoder__decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
def encrypt(text, n):
encrypted = ""
for char in text:
if char.isupper():
c = ord(char) + (n % 26)
if c > ord('Z'):
c -= 26
elif char.islower():
c = ord(char) + (n % 26)
if c > ord('z'):
c -= 26
else:
c = ord(char) + 1
encrypted += chr(c)
return encrypted

def decrypt(text, n):
decrypted = ""
for char in text:
if char.isupper():
c = ord(char) - (n % 26)
if c < ord('A'):
c += 26
elif char.islower():
c = ord(char) - (n % 26)
if c < ord('a'):
c += 26
else:
c = ord(char) - 1
decrypted += chr(c)
return decrypted

def main():
text = input("Enter a string: ")
n = int(input("Enter key: "))
encrypted_string = encrypt(text, n)
print("\nEncrypted text:", encrypted_string)
decrypted_string = decrypt(encrypted_string, n)
print("Decrypted text:", decrypted_string)

if __name__ == "__main__":
main()

0 comments on commit e17f5cc

Please sign in to comment.