共計 1249 個字符,預(yù)計需要花費 4 分鐘才能閱讀完成。
Python 中的簡單加密算法可以使用替換或移位等操作來實現(xiàn)。下面是一個使用替換操作的簡單加密算法示例:
def encrypt(message):
# 定義替換規(guī)則
substitution = {'a': 'b',
'b': 'c',
'c': 'd',
'd': 'e',
'e': 'f',
'f': 'g',
'g': 'h',
'h': 'i',
'i': 'j',
'j': 'k',
'k': 'l',
'l': 'm',
'm': 'n',
'n': 'o',
'o': 'p',
'p': 'q',
'q': 'r',
'r': 's',
's': 't',
't': 'u',
'u': 'v',
'v': 'w',
'w': 'x',
'x': 'y',
'y': 'z',
'z': 'a'
}
encrypted_message = ''
for char in message:
if char.lower() in substitution:
# 將字符替換為對應(yīng)的加密字符
encrypted_message += substitution[char.lower()]
else:
encrypted_message += char # 如果字符不在替換規(guī)則中,則保持原樣
return encrypted_message
message = input(' 請輸入需要加密的消息:')
encrypted_message = encrypt(message)
print(' 加密后的消息:', encrypted_message)
在這個示例中,使用了一個簡單的替換規(guī)則將字母替換為其后一位字母,即’a’替換為’b’,‘b’替換為’c’,依此類推。輸入的消息中出現(xiàn)的字母都會被替換為對應(yīng)的加密字符,其他字符保持不變。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完