题目
This commit is contained in:
19
crypto/easy_caesar/encrypt.py
Normal file
19
crypto/easy_caesar/encrypt.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
with open("plain_text.txt") as f:
|
||||||
|
plain = f.readline()
|
||||||
|
|
||||||
|
|
||||||
|
def caesar_encrypt(text, shift = 3):
|
||||||
|
encrypted_text = ""
|
||||||
|
for char in text:
|
||||||
|
if char.isalpha():
|
||||||
|
ascii_offset = ord('A') if char.isupper() else ord('a')
|
||||||
|
encrypted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
|
||||||
|
encrypted_text += encrypted_char
|
||||||
|
else:
|
||||||
|
encrypted_text += char
|
||||||
|
return encrypted_text
|
||||||
|
|
||||||
|
cipher = caesar_encrypt(plain)
|
||||||
|
|
||||||
|
with open("cipher_text.txt","w") as f:
|
||||||
|
f.write(cipher)
|
||||||
Reference in New Issue
Block a user