凯撒加密

一种简单的加密方法,即将每个字母替换为其字母表 n 位之后的字母。

加密

 1 # Caesar Cipher
 2 # http://inventwithpython.com/hacking (BSD Licensed)
 3 
 4 import pyperclip
 5 
 6 # the string to be encrypted/decrypted
 7 message = 'This is my secret message.'
 8 
 9 # the encryption/decryption key
10 key = 13
11 
12 # tells the program to encrypt or decrypt
13 mode = 'encrypt' # set to 'encrypt' or 'decrypt'
14 
15 # every possible symbol that can be encrypted
16 LETTERS = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
17 
18 # stores the encrypted/decrypted form of the message
19 translated = ''
20 
21 # capitalize the string in message
22 #message = message.upper()
23 
24 # run the encryption/decryption code on each symbol in the message string
25 for symbol in message:
26     if symbol in LETTERS:
27         # get the encrypted (or decrypted) number for this symbol
28         num = LETTERS.find(symbol) # get the number of the symbol
29         if mode == 'encrypt':
30             num = num + key
31         elif mode == 'decrypt':
32             num = num - key
33 
34         # handle the wrap-around if num is larger than the length of
35         # LETTERS or less than 0
36         if num >= len(LETTERS):
37             num = num - len(LETTERS)
38         elif num < 0:
39             num = num + len(LETTERS)
40 
41         # add encrypted/decrypted number's symbol at the end of translated
42         translated = translated + LETTERS[num]
43 
44     else:
45         # just add the symbol without encrypting/decrypting
46         translated = translated + symbol
47 
48 # print the encrypted/decrypted string to the screen
49 print(translated)
50 
51 # copy the encrypted/decrypted string to the clipboard
52 pyperclip.copy(translated)

破解即为穷举密钥

 1 # Caesar Cipher Hacker
 2 # http://inventwithpython.com/hacking (BSD Licensed)
 3 
 4 message = 'GUVF VF ZL FRPERG ZRFFNTR.'
 5 LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 6 
 7 # loop through every possible key
 8 for key in range(len(LETTERS)):
 9 
10     # It is important to set translated to the blank string so that the
11     # previous iteration's value for translated is cleared.
12     translated = ''
13 
14     # The rest of the program is the same as the original Caesar program:
15 
16     # run the encryption/decryption code on each symbol in the message
17     for symbol in message:
18         if symbol in LETTERS:
19             num = LETTERS.find(symbol) # get the number of the symbol
20             num = num - key
21 
22             # handle the wrap-around if num is 26 or larger or less than 0
23             if num < 0:
24                 num = num + len(LETTERS)
25 
26             # add number's symbol at the end of translated
27             translated = translated + LETTERS[num]
28 
29         else:
30             # just add the symbol without encrypting/decrypting
31             translated = translated + symbol
32 
33     # display the current key being tested, along with its decryption
34     print('Key #%s: %s' % (key, translated))

 

上一篇:[Swift]LeetCode744. 寻找比目标字母大的最小字母 | Find Smallest Letter Greater Than Target


下一篇:Redis入门指南 第3章 Redis的5种主要数据类型及相应的命令(四) 集合类型