以下是
Python 实现 凯撒密码 加密和
解密的示例代码:
加密:
python解密def caesar_encrypt(plain_text, shift):"""凯撒密码 加密:param plain_text: 明文:param shift: 移位数:return: 密文"""cipher_text = ''for char in plain_text:if char.isalpha():cipher_text += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))else:cipher_text += charreturn cipher_text
:
python使用def caesar_decrypt(cipher_text, shift):"""凯撒密码 解密:param cipher_text: 密文:param shift: 移位数:return: 明文"""plain_text = ''for char in cipher_text:if char.isalpha():plain_text += chr((ord(char) - ord('a') - shift + 26) % 26 + ord('a'))else:plain_text += charreturn plain_text
示例:
pythonplain_text = 'hello world'shift = 3cipher_text = caesar_encrypt(plain_text, shift)print(cipher_text) # khoor zruogdecrypted_text = caesar_decrypt(cipher_text, shift)print(decrypted_text) # hello world
注:以上代码中,只考虑了小写字母的情况。如果需要
加密或
解密大写字母或其他字符,请自行修改代码。
到此这篇凯撒密码加密 算法python(凯撒密码加密解密python)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/pythonbc/27503.html