当前位置:网站首页 > Python编程 > 正文

凯撒密码加密算法python(凯撒密码加密解密python)

以下是

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 += char return 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 += char return plain_text 

使用

示例:

 python plain_text = 'hello world' shift = 3 cipher_text = caesar_encrypt(plain_text, shift) print(cipher_text) # khoor zruog  decrypted_text = caesar_decrypt(cipher_text, shift) print(decrypted_text) # hello world 

注:以上代码中,只考虑了小写字母的情况。如果需要

加密

解密

大写字母或其他字符,请自行修改代码。

到此这篇凯撒密码加密 算法python(凯撒密码加密解密python)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • python函数图像绘制程序(python绘制函数图像代码)2026-01-17 09:36:06
  • onnx模型部署 python(onnx模型部署到单片机)2026-01-17 09:36:06
  • python字典如何添加一个元素和修改一个元素(python字典如何添加值)2026-01-17 09:36:06
  • python的函数可以没有返回值(python函数没有返回值会返回什么)2026-01-17 09:36:06
  • python函数定义及调用(python定义的函数怎么调用)2026-01-17 09:36:06
  • 删除python虚拟环境(pipenv删除虚拟环境)2026-01-17 09:36:06
  • python3返回多个值(python 返回多个值)2026-01-17 09:36:06
  • python3 def函数(python怎么def函数)2026-01-17 09:36:06
  • python可以没有返回值吗(python中哪些函数没有返回值)2026-01-17 09:36:06
  • python中用于获取用户输入的命令(python中用于获取用户输出的命令)2026-01-17 09:36:06
  • 全屏图片