python凯撒密码实现_密码:凯撒密码及其Python实现
python凱撒密碼實現(xiàn)
Before we start let’s some basic terminology...
在開始之前,讓我們先介紹一些基本術(shù)語...
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.
通過對消息進(jìn)行編碼以使其不可讀來實現(xiàn)安全性的技術(shù)和科學(xué)稱為密碼術(shù) 。 這就是整篇文章的內(nèi)容。
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.
密碼分析是一種將不可讀代碼再次解碼為可讀的技術(shù),而無需知道它們最初是如何從可讀轉(zhuǎn)換為不可讀的。 這就是我們在以后的文章中要做的。
Thus, Cryptology = Cryptography + Cryptanalysis.
因此, 密碼學(xué)=密碼術(shù)+密碼分析 。
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.
自從使用手動技術(shù)執(zhí)行密碼以來,就開始使用密碼術(shù),但是基本框架始終或多或少保持不變,當(dāng)然,已經(jīng)有了很多改進(jìn)。 雖然本文只是理論而已,但不必灰心,我們也將介紹它們。
We have two types of text:
我們有兩種類型的文本:
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.
純文本(或純文本):這是發(fā)送者和接收者都可以訪問的其他人也可以理解的實際消息。
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)替代技術(shù) (1) Substitution Techniques)
凱撒密碼 (Caesar Cipher)
This Scheme was first proposed by Julius Caesar, cryptography is used since that time.
該方案最初由Julius Caesar提出,從那時開始使用加密技術(shù)。
In this Substitution cipher technique, each character of the plaintext message will be replaced by another character, symbol or number.
在這種替換密碼技術(shù)中,純文本消息的每個字符將被另一個字符,符號或數(shù)字代替。
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個字母。
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進(jìn)行了概括,其方式不一定是將原始字母下移第三位,而是可以將其下移到任何位置。
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}凱撒密碼的修改版 (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進(jìn)行了概括,其方式不一定是將原始字母下移第三位,而是可以將其下移到任何位置。
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個結(jié)果。
1. QVKTCLMPMTX QA IEMAWUM2. PUJSBKLOLSW PZ HDLZVTL3. OTIRAJKNKRV OY GCKYUSK4. NSHQZIJMJQU NX FBJXTRJ5. MRGPYHILIPT MW EAIWSQI6. LQFOXGHKHOS LV DZHVRPH7. KPENWFGJGNR KU CYGUQOG8. JODMVEFIFMQ JT BXFTPNF9. INCLUDEHELP IS AWESOME10. HMBKTCDGDKO HR ZVDRNLD11. GLAJSBCFCJN GQ YUCQMKC12. FKZIRABEBIM FP XTBPLJB13. EJYHQZADAHL EO WSAOKIA14. DIXGPYZCZGK DN VRZNJHZ15. CHWFOXYBYFJ CM UQYMIGY16. BGVENWXAXEI BL TPXLHFX17. AFUDMVWZWDH AK SOWKGEW18. ZETCLUVYVCG ZJ RNVJFDV19. YDSBKTUXUBF YI QMUIECU20. XCRAJSTWTAE XH PLTHDBT21. WBQZIRSVSZD WG OKSGCAS22. VAPYHQRURYC VF NJRFBZR23. UZOXGPQTQXB UE MIQEAYQ24. TYNWFOPSPWA TD LHPDZXP25. SXMVENOROVZ SC KGOCYWOHere we tried all possible outcomes and the 9th one was our message.
在這里,我們嘗試了所有可能的結(jié)果和第 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凱撒密碼實現(xiàn)
總結(jié)
以上是生活随笔為你收集整理的python凯撒密码实现_密码:凯撒密码及其Python实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二叉树打印叶子节点,非递归_使用递归打印
- 下一篇: scala集合中添加元素_如何在Scal