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凯撒密码实现