python凯撒密码实现_密码:凯撒密码及其Python实现

python凯撒密码实现

Before we start let’s some basic terminology...

在开始之前,让我们先介绍一些基本术语...

The art and science to achieve security by encoding messages to make them unreadable are known as Cryptography. That’s what the whole article is going about.

通过对消息进行编码以使其不可读来实现安全性的技术和科学称为密码术 。 这就是整篇文章的内容。

The technique to decode an unreadable code to readable again without knowing how they were initially converted from readable to unreadable is Cryptanalysis. That’s what we’ll do in our later articles.

密码分析是一种将不可读代码再次解码为可读的技术,而无需知道它们最初是如何从可读转换为不可读的。 这就是我们在以后的文章中要做的。

Thus, Cryptology = Cryptography + Cryptanalysis.

因此, 密码学=密码术+密码分析

Cryptography is used since ages performed by manual techniques but the basic framework has always remained less or more the same, definitely, there were a lot of improvements. While this article is all theory but no need be disheartened we’ll cover them too.

自从使用手动技术执行密码以来,就开始使用密码术,但是基本框架始终或多或少保持不变,当然,已经有了很多改进。 虽然本文只是理论而已,但不必灰心,我们也将介绍它们。

We have two types of text:

我们有两种类型的文本:

  1. Plain (or clear) text: Which is an actual message that both sender and receiver can understand also by anyone else who gets an access to that message.

    纯文本(或纯文本):这是发送者和接收者都可以访问的其他人也可以理解的实际消息。

  2. Cipher text: When any plain text is codified using a suitable scheme and the resulting message is a cipher text.

    密文:使用合适的方案将任何纯文本编码后,得到的消息就是密文。

There are two ways by which we can primarily change plain text to cipher text by Substitution and Transposition.

我们可以通过两种方式主要通过替换换位将纯文本更改为密文。

1)替代技术 (1) Substitution Techniques)

凯撒密码 (Caesar Cipher)

This Scheme was first proposed by Julius Caesar, cryptography is used since that time.

该方案最初由Julius Caesar提出,从那时开始使用加密技术。

In this Substitution cipher technique, each character of the plaintext message will be replaced by another character, symbol or number.

在这种替换密码技术中,纯文本消息的每个字符将被另一个字符,符号或数字代替。

Caesar cipher is another example of a substitution cipher where it replaces each alphabet from the message to an alphabet 3 places down the line.

凯撒密码是替换密码的另一个示例,其中它将消息中的每个字母替换为下一行的3个字母。

Caesar Cipher

Python编码 (Python Encoding)

string = input("Enter a string\n")
string= str.upper(string)
for x in string:
if(x==' '):
print(' ',end='')
elif(ord(x)-ord('A')+3 >= 26 ):
print(chr(ord(x)-26+3), end='')
else:
print (chr(ord(x)+3), end='')

Python解码 (Python Decoding)

string = input('Enter Decode text: ')
string = str.upper(string)
for x in string:
if(x==' '):
print(' ',end='')
elif(ord(x)-ord('A')-3<0):
print(chr(ord(x)-3+26), end='')
else:
print(chr(ord(x)-3), end='')

Just to make an attacker’s life more difficult we generalized the Caesar Cipher by not necessarily change original alphabet by a third place down the line but instead it can be any place down the line.

只是为了使攻击者的生活更加困难,我们对Caesar Cipher进行了概括,其方式不一定是将原始字母下移第三位,而是可以将其下移到任何位置。

凯撒密码的修改版 (Modified Version of Caesar Cipher )

Just to make an attacker’s life more difficult we generalized the Caesar Cipher by not necessarily change original alphabet by a third place down the line but instead it can be any place down the line.

只是为了使攻击者的生活更加困难,我们对Caesar Cipher进行了概括,其方式不一定是将原始字母下移第三位,而是可以将其下移到任何位置。

In modified Version, and alphabet can be changed with any other alphabet but once the replacement scheme is decided then it would be constant and will use for all other alphabets in that message.

在修改后的版本中,字母可以与任何其他字母一起更改,但是一旦决定了替换方案,它将保持不变,并将用于该消息中的所有其他字母。

Since English has 26 alphabets then there are 25 possible replacement schemes (replacement of an alphabet with itself is senseless).

由于英语有26个字母,因此有25种可能的替换方案(用自身替换字母是没有意义的)。

Example:

例:

RWLUDMNQNUY RB JFNBXVN

RWLUDMNQNUY RB JFNBXVN

To change above Cipher Text into the plain text we need to use brute-force (trying all available options) thus we got 25 results.

要将上方的密文更改为纯文本,我们需要使用蛮力(尝试所有可用的选项),因此我们得到了25个结果。

    1.	QVKTCLMPMTX QA IEMAWUM
2.	PUJSBKLOLSW PZ HDLZVTL
3.	OTIRAJKNKRV OY GCKYUSK
4.	NSHQZIJMJQU NX FBJXTRJ
5.	MRGPYHILIPT MW EAIWSQI
6.	LQFOXGHKHOS LV DZHVRPH
7.	KPENWFGJGNR KU CYGUQOG
8.	JODMVEFIFMQ JT BXFTPNF
9.	INCLUDEHELP IS AWESOME
10.	HMBKTCDGDKO HR ZVDRNLD
11.	GLAJSBCFCJN GQ YUCQMKC
12.	FKZIRABEBIM FP XTBPLJB
13.	EJYHQZADAHL EO WSAOKIA
14.	DIXGPYZCZGK DN VRZNJHZ
15.	CHWFOXYBYFJ CM UQYMIGY
16.	BGVENWXAXEI BL TPXLHFX
17.	AFUDMVWZWDH AK SOWKGEW
18.	ZETCLUVYVCG ZJ RNVJFDV
19.	YDSBKTUXUBF YI QMUIECU
20.	XCRAJSTWTAE XH PLTHDBT
21.	WBQZIRSVSZD WG OKSGCAS
22.	VAPYHQRURYC VF NJRFBZR
23.	UZOXGPQTQXB UE MIQEAYQ
24.	TYNWFOPSPWA TD LHPDZXP
25.	SXMVENOROVZ SC KGOCYWO

Here we tried all possible outcomes and the 9th one was our message.

在这里,我们尝试了所有可能的结果和 9一个是我们的消息。

凯撒编码的修改版 (A modified version of Caesar Encoding)

string = input('Enter Input: ')
key = int(input('Enter a KEY (1-25): '))
string= str.upper(string)
for x in string:
if(x==' '):
print(' ',end='')
elif(ord(x)-ord('A')+key >= 26 ):
print(chr(ord(x)-26+key), end='')
else:
print (chr(ord(x)+key), end='')

凯撒解码的修改版本 (A modified version of Caesar Decoding)

string = input('Enter Decode text: ')
string = str.upper(string)
for key in range(1,26):
for x in string:
if(x==' '):
print(' ',end='')
elif(ord(x)-ord('A')-key<0):
print(chr(ord(x)-key+26), end='')
else:
print(chr(ord(x)-key), end='')
print(' ')

翻译自: https://www.includehelp.com/cryptography/cryptography-caesar-cipher-and-its-python-implementations.aspx

python凯撒密码实现

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542704.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qtextedit 默认文案_QT-纯代码控件-QSplitter(分裂器)

版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循CC 4.0 by-sa版权协议&#xff0c;转载请附上原文出处链接和本声明。本文链接&#xff1a;https://blog.csdn.net/qq_41488943/article/details/96431379使用Qplitter实现页面的三布局分布1.新建一个无ui界面的工程&…

TYVJ P1030 乳草的入侵 Label:跳马问题

背景 USACO OCT09 6TH描述 Farmer John一直努力让他的草地充满鲜美多汁的而又健康的牧草。可惜天不从人愿&#xff0c;他在植物大战人类中败下阵来。邪恶的乳草已经在他的农场的西北部份佔领了一片立足之地。草地像往常一样&#xff0c;被分割成一个高度為Y(1 < y < 100)…

kotlin中既继承又实现_Kotlin程序| 解决继承中的主要冲突的示例

kotlin中既继承又实现继承中的主要冲突 (Overriding Conflicts in Inheritance) It may appear, we inherit more than one implementation of the same method. 看来&#xff0c;我们继承了同一方法的多个实现。 Need to implement all the methods which we have inherited f…

python雷达图详解_Python简单雷达图绘制

import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams[font.family] SimHei matplotlib.rcParams[font.sans-serif] [SimHei] lables np.array([综合,KDA,发育,推进,生存,输出]) nAttr 6 date np.array([7, 5, 6, 9, 8, 7]) angles…

浏览器兼容问题 透明度 position:fixed bootstrap

浏览器兼容问题&#xff1a;主要是ie8以下&#xff1a; 用bootstrap框架结合jq写页面&#xff0c;因为bootstrap有好多media和html5所以要在引入样式后引入两个js <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --><!-- WARNI…

s查找mysql服务_MySQL菜鸟实录(一):MySQL服务安装实战

CentOS 7基本信息系统版本&#xff1a; CentOS 7.3 64bit系统配置&#xff1a; 4vCPUs | 8GB磁盘空间&#xff1a;[rootecs-ce5a-0001 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/vda1 40G 17G 22G 44% /devtmpfs 3.9G 0 3.9G 0% /devtmpfs 3.9G 0 3.9G 0% /dev…

实验一 线性表的顺序存储与实现_【自考】数据结构中的线性表,期末不挂科指南,第2篇

线性表这篇博客写的是线性表相关的内容&#xff0c;包括如下部分&#xff0c;先看下有木有期待啥是线性表线性表的顺序存储线性表的基本运算在顺序表上的实现线性表的链式存储线性表的基本运算在单链表上的实现循环链表与双向循环链表Over&#xff0c;内容还蛮多的&#xff01;…

二叉树打印叶子节点,非递归_使用递归打印链接列表中的备用节点

二叉树打印叶子节点,非递归Solution: 解&#xff1a; Input: A singly linked list whose address of the first node is stored in a pointer, say head 输入&#xff1a;一个单链表 &#xff0c;其第一个节点的地址存储在指针中&#xff0c;例如head Output: The alternati…

TYVJ P1012 火柴棒等式 Label:枚举

背景 NOIP2008年提高组第二题描述 给你n根火柴棍&#xff0c;你可以拼出多少个形如“ABC”的等式&#xff1f;等式中的A、B、C是用火柴棍拼出的整数&#xff08;若该数非零&#xff0c;则最高位不能是0&#xff09;。用火柴棍拼数字0-9的拼法如图所示&#xff1a;注意&#xff…

java math max_Java Math类静态double max(double d1,double d2)示例

java math max数学类静态double max(double d1&#xff0c;double d2) (Math Class static double max(double d1,double d2) ) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to return the maximum one of both the give…

python怎么开发软件_怎么使用python进行软件开发

一、下载pyinstaller 我使用的版本为PyInstaller-2.1&#xff0c;支持python版本2.3-2.7&#xff0c;点击这里下载。 二、安装pyinstaller 下载完成后&#xff0c;解压即可。我的解压目录为D:\Python27\PyInstaller-2.1\ 三、使用pyinstaller打包.py成.exe应用程序 1.注意使用前…

28、清华大学脑机接口实验组SSVEP数据集:通过视觉触发BCI[飞一般的赶脚!]

前言&#xff1a; 哈喽&#xff0c;最近对清华大学脑机接口的数据进行了尝试&#xff0c;输入到了DL模型中&#xff0c;以下是本人对于清华BCI数据的个人见解。 数据地址&#xff1a; 清华大学脑机接口研究组 (tsinghua.edu.cn) 打开网站可以看到有很多个数据&#xff0c;官…

python Pexpect

http://www.cnblogs.com/dkblog/archive/2013/03/20/2970738.htmlhttp://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/index.htmlhttp://www.cnblogs.com/dkblog/archive/2013/03/20/2970738.htmlpython Pexpect Pexpect 是一个用来启动子程序并对其进行自动控制的纯 P…

python 幂运算 整数_在Python中检查一个数字是否是另一个数字的幂

python 幂运算 整数To solve this problem simply, we will use the log() function from the math module. The math module provides us various mathematical operations and here we will use the log() function from this module. In Python working of log() function, …

3dmax镜像后模型线条乱了_3dMax入门教程来啦!小白赶紧收藏!

3D Studio Max&#xff0c;常简称为3d Max或3ds MAX&#xff0c;是Discreet公司开发的&#xff08;后被Autodesk公司合并&#xff09;基于PC系统的三维动画渲染和制作软件&#xff0c; 3dmax软件主要功能有建模&#xff0c;动画&#xff0c;渲染&#xff0c;特效等&#xff0c;…

java中哲学家就餐死锁_哲学家就餐问题与死锁总结

死锁的四个条件&#xff1a;(1) 互斥条件&#xff1a;一个资源每次只能被一个进程使用。(2) 请求与保持条件&#xff1a;一个进程因请求资源而阻塞时&#xff0c;对已获得的资源保持不放。(3) 不剥夺条件:进程已获得的资源&#xff0c;在末使用完之前&#xff0c;不能强行剥夺。…

linux扫描工具之nmap

Linux下有很多强大网络扫描工具&#xff0c;网络扫描工具可以分为&#xff1a;主机扫描、主机服务扫描、路由扫描等,nmap支持批量主机扫描和主机服务扫描。检测安装&#xff1a;[rootbier ~]# rpm -qa nmap nmap-5.51-4.el6.x86_64如果没有安装就安装一下nmap的安装直接使用&am…

如何将多个一维列表转化为二维列表_数据分析2_如何处理一维、二维数据

吞一块大饼&#xff0c;还不如切成小块吃得香常见的数据集&#xff0c;要么是数列&#xff0c;要么是表格&#xff1b;因此&#xff0c;数据分析最首要的是&#xff0c;处理一维、二维数据。主要知识点可参考如图。如需要&#xff0c;可点击以下百度网盘链接下载数据分析基础知…

关于java中锁的面试题_Java面试题-Java中的锁

1. 如何实现乐观锁(CAS)&#xff1f;如何避免ABA问题&#xff1f;答&#xff1a;1)读取内存值的方式实现了乐观锁(比如&#xff1a;SVN系统)&#xff0c;方法&#xff1a;第一&#xff0c;比较内存值和期望值&#xff1b;第二&#xff0c;替换内存值为要替换值。2)带参数版本来…

NSUserDefaults

2019独角兽企业重金招聘Python工程师标准>>> NSUserDefaults 转载于:https://my.oschina.net/18829297883/blog/737931