用python写一个加密和破解的代码
以下是一个使用Python实现加密和解密的示例代码:
# 加密函数
def encrypt(plain_text, shift):
cipher_text = ""
for ch in plain_text:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipher_text += finalLetter
else:
cipher_text += ch
return cipher_text
# 解密函数
def decrypt(cipher_text, shift):
plain_text = ""中文写代码软件
for ch in cipher_text:
if ch.isalpha():
stayInAlphabet = ord(ch) - shift
if stayInAlphabet < ord('a'):
stayInAlphabet += 26
finalLetter = chr(stayInAlphabet)
plain_text += finalLetter
else:
plain_text += ch
return plain_text
plain_text = "This is a secret message"
shift = 3
cipher_text = encrypt(plain_text, shift)
print(cipher_text) # Wklv lv d vhfuhw phvvdjh
decrypted_text = decrypt(cipher_text, shift)
print(decrypted_text) # This is a secret message
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论