Page Nav

HIDE

Grid

GRID_STYLE

Classic Header

{fbt_classic_header}

Header Ads

program

latest

Caesar Cipher Function in Python

👉  Which cipher algorithm is varatian of Caesar cipher algorithm used for encryption and decryption in Python? I'm trying to create a s...

👉 Which cipher algorithm is varatian of Caesar cipher algorithm used for encryption and decryption in Python?


I'm trying to create a simple Caesar Cipher function in Python that shifts letters based on input from the user and creates a final, new string at the end. The only problem is that the final cipher text shows only the last shifted character, not an entire string with all the shifted characters



Here's my code:

       ðŸ‘‡

def encypt_func(txt, s):
result = ""

# transverse the plain txt
for i in range(len(txt)):
char = txt[i]
# encypt_func uppercase characters in plain txt

if (char.isupper()):
result += chr((ord(char) + s - 64) % 26 + 65)
# encypt_func lowercase characters in plain txt
else:
result += chr((ord(char) + s - 96) % 26 + 97)
return result


# check the above function
txt = "i am chiranjeevi"
s = 4

print("Plain txt : " + txt)
print("Shift pattern : " + str(s))
print("Cipher: " + encypt_func(txt, s))

Output:
   

How to decrypt?

Cipher(n) = De-cipher(26-n)







1 comment