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:
我們有兩種類型的文本:
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.
純文本(或純文本):這是發送者和接收者都可以訪問的其他人也可以理解的實際消息。
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個字母。

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凱撒密碼實現