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.pswp.cn/news/542704.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542704.shtml
英文地址,請注明出處:http://en.pswp.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