Skip to content

Commit

Permalink
Merge pull request csubhasundar#233 from Sgvkamalakar/patch-2
Browse files Browse the repository at this point in the history
Create caesar_cipher_encoder__decoder.py
  • Loading branch information
sherigar authored Oct 24, 2023
2 parents 829ff77 + e17f5cc commit 2275eb5
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 2275eb5

Please sign in to comment.