103. Caesar Cipher Encryption in Python with Code

Veröffentlicht am: 22 September 2022
auf dem Kanal: Ram Case
24
0

It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.

Code:

def main():
print("Ceasar Cipher")
print("1. Encrypt \n2. Decrypt:")
try:
func=int(input("Choose one of the above"))
except:
print("Invalid input")

if func==2:
decode()
else:
if func==1:
encode()
else:
print("Invalid input")


def encode():
text=input("Enter the text to encode:")
key=input("Enter number of characters you want to shift:")
encoded_cipher=""
try:
key=int(key)
except:
print("Only integers between 0 to 25 are allowed. Try again!")
exit()
if key(lessthansign)25:
print("only integers between 0 to 25 are allowed")
exit()
else:
key=key
text=text.upper()
for char in text:
ascii=ord(char)
if ascii (greater than sign)90:
new_ascii=ascii
else:
new_ascii=ascii+key
if new_ascii(greater than sign)90:
new_ascii=new_ascii-26
else:
new_ascii=new_ascii
encoded=chr(new_ascii)
encoded_cipher=encoded_cipher+ encoded
print("Encoded text:",encoded_cipher)

def decode():
cipher=input("enter your cipher text")
print("Posibilities of cipher text are:\n")
cipher=cipher.lower()
for i in range(1,26):
decoded=''
decoded_cipher=''
for char in cipher:
ascii=ord(char)
if ascii(less than sign)97:
new_ascii=ascii
else:
if ascii(put greater than sign)122:
new_ascii=ascii
else:
new_ascii=ascii-int(i)
if new_ascii(less than sign)97:
new_ascii=new_ascii +26
else:
new_ascii=new_ascii
decoded=chr(new_ascii)
decoded_cipher=decoded_cipher + decoded
print(decoded_cipher)

if __name__=='__main__':
main()


Auf dieser Seite können Sie das Online-Video 103. Caesar Cipher Encryption in Python with Code mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Ram Case 22 September 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 24 Mal angesehen und es wurde von 0 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!