コンテンツにスキップ

08. 暗号文

問題

与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.

英小文字ならば(219 - 文字コード)の文字に置換 その他の文字はそのまま出力 この関数を用い,英語のメッセージを暗号化・復号化せよ.

回答例

ここをクリックして回答を見る
def cipher(text):
result = ""
for c in text:
if c.islower():
result += chr(219 - ord(c))
else:
result += c
return result
# 暗号化
encrypted = cipher("this is test")
print(encrypted)
# 復号化
decrypted = cipher(encrypted)
print(decrypted)