Home  >  Article  >  Backend Development  >  Example of Caesar cipher algorithm implemented in Python

Example of Caesar cipher algorithm implemented in Python

不言
不言Original
2018-05-05 15:32:338889browse

This article mainly introduces the Caesar cipher algorithm implemented in Python, briefly introduces the concept and principle of the Caesar cipher, and analyzes the relevant definitions and usage techniques of the Caesar cipher algorithm implemented in Python in the form of examples. Friends in need can refer to the following

The example in this article describes the Caesar cipher algorithm implemented in Python. Share it with everyone for your reference, the details are as follows:

Introduction

The Caesar cipher is a very old encryption method. According to legend, Caesar used In order to ensure that their orders are not known to the enemy when they are marching and fighting on the earth, they use this special method to communicate to ensure the safety of information transmission. His principle is very simple. In the final analysis, it is the replacement of letters between letters. Let's look at a simple example: "baidu" is encrypted with Caesar cipher and the string becomes "edlgx". What is its principle? Shift each letter in "baidu" backward by 3 digits in alphabetical order, and the result is the ciphertext we just saw.

Second code

# -*- coding:utf-8 -*-
import os
#==================================================================#
#     凯撒密码(caesar)是最早的代换密码,对称密码的一种        #
#  算法:将每个字母用字母表中它之后的第k个字母(称作位移值)替代      #
#==================================================================#
def encryption():
  str_raw = raw_input("请输入明文:")
  k = int(raw_input("请输入位移值:"))
  str_change = str_raw.lower()
  str_list = list(str_change)
  str_list_encry = str_list
  i = 0
  while i < len(str_list):
    if ord(str_list[i]) < 123-k:
      str_list_encry[i] = chr(ord(str_list[i]) + k)
    else:
      str_list_encry[i] = chr(ord(str_list[i]) + k - 26)
    i = i+1
  print ("加密结果为:"+"".join(str_list_encry))
def decryption():
  str_raw = raw_input("请输入密文:")
  k = int(raw_input("请输入位移值:"))
  str_change = str_raw.lower()
  str_list = list(str_change)
  str_list_decry = str_list
  i = 0
  while i < len(str_list):
    if ord(str_list[i]) >= 97+k:
      str_list_decry[i] = chr(ord(str_list[i]) - k)
    else:
      str_list_decry[i] = chr(ord(str_list[i]) + 26 - k)
    i = i+1
  print ("解密结果为:"+"".join(str_list_decry))
while True:
  print (u"1. 加密")
  print (u"2. 解密")
  choice = raw_input("请选择:")
  if choice == "1":
    encryption()
  elif choice == "2":
    decryption()
  else:
    print (u"您的输入有误!")

Three running results

Related recommendations:

Python implementation example of finding all subsets of a set

Python implements LR classic algorithm

The above is the detailed content of Example of Caesar cipher algorithm implemented in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn