简单替代密码
简单替换密码是最常用的密码,包括为每个密文文本字符替换每个纯文本字符的算法.在这个过程中,与凯撒密码算法相比,字母表是混乱的.
示例
简单替换密码的密钥通常由26个字母组成.一个示例键是 :
1
2
|
plain alphabet : abcdefghijklmnopqrstuvwxyz cipher alphabet: phqgiumeaylnofdxjkrcvstzwb |
使用上述密钥的示例加密是 :
1
2
|
plaintext : defend the east wall of the castle ciphertext: giuifg cei iprc tpnn du cei qprcni |
以下代码显示了一个实现简单替换密码的程序;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import random, sys LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def main(): message = '' if len (sys.argv) > 1 : with open (sys.argv[ 1 ], 'r' ) as f: message = f.read() else : message = raw_input ( "Enter your message: " ) mode = raw_input ( "E for Encrypt, D for Decrypt: " ) key = '' while checkKey(key) is False : key = raw_input ( "Enter 26 ALPHA key (leave blank for random key): " ) if key = = '': key = getRandomKey() if checkKey(key) is False : print ( 'There is an error in the key or symbol set.' ) translated = translateMessage(message, key, mode) print ( 'Using key: %s' % (key)) if len (sys.argv) > 1 : fileOut = 'enc.' + sys.argv[ 1 ] with open (fileOut, 'w' ) as f: f.write(translated) print ( 'Success! File written to: %s' % (fileOut)) else : print ( 'Result: ' + translated) # Store the key into list, sort it, convert back, compare to alphabet. def checkKey(key): keyString = ''.join( sorted ( list (key))) return keyString = = LETTERS def translateMessage(message, key, mode): translated = '' charsA = LETTERS charsB = key # If decrypt mode is detected, swap A and B if mode = = 'D' : charsA, charsB = charsB, charsA for symbol in message: if symbol.upper() in charsA: symIndex = charsA.find(symbol.upper()) if symbol.isupper(): translated + = charsB[symIndex].upper() else : translated + = charsB[symIndex].lower() else : translated + = symbol return translated def getRandomKey(): randomList = list (LETTERS) random.shuffle(randomList) return ''.join(randomList) if __name__ = = '__main__' : main() |
输出
您可以观察以下内容当你实现上面给出的代码时输出 :
简单替换密码测试
我们将重点介绍如何使用各种方法测试替换密码,这有助于生成随机字符串,如下面所示 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import random, string, substitution def main(): for i in range ( 1000 ): key = substitution.getRandomKey() message = random_string() print ( 'Test %s: String: "%s.."' % (i + 1 , message[: 50 ])) print ( "Key: " + key) encrypted = substitution.translateMessage(message, key, 'E' ) decrypted = substitution.translateMessage(encrypted, key, 'D' ) if decrypted ! = message: print ( 'ERROR: Decrypted: "%s" Key: %s' % (decrypted, key)) sys.exit() print ( 'Substutition test passed!' ) def random_string(size = 5000 , chars = string.ascii_letters + string.digits): return ''.join(random.choice(chars) for _ in range (size)) if __name__ = = '__main__' : main() |
输出
您可以随机观察输出生成的字符串有助于生成随机纯文本消息,如下所示 :
测试成功完成后,我们可以观察输出消息替换测试通过!.
因此,您可以系统地破解替换密码.
简单替换密码解密
您可以了解替换密码的简单实现,它根据简单替换密码技术中使用的逻辑显示加密和解密的消息.这可以被视为一种替代编码方法.
代码
您可以使用以下代码使用简单替换密码来执行解密;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import random chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + \ 'abcdefghijklmnopqrstuvwxyz' + \ '0123456789' + \ ':.;,?!@#$%&()+=-*/_<> []{}`~^"\'\\' def generate_key(): """Generate an key for our cipher""" shuffled = sorted (chars, key = lambda k: random.random()) return dict ( zip (chars, shuffled)) def encrypt(key, plaintext): """Encrypt the string and return the ciphertext""" return ''.join(key[l] for l in plaintext) def decrypt(key, ciphertext): """Decrypt the string and return the plaintext""" flipped = {v: k for k, v in key.items()} return ''.join(flipped[l] for l in ciphertext) def show_result(plaintext): """Generate a resulting cipher with elements shown""" key = generate_key() encrypted = encrypt(key, plaintext) decrypted = decrypt(key, encrypted) print 'Key: %s' % key print 'Plaintext: %s' % plaintext print 'Encrypted: %s' % encrypted print 'Decrypted: %s' % decrypted show_result( 'Hello World. This is demo of substitution cipher' ) |
以上就是python密码学简单替代密码解密及测试教程的详细内容,更多关于python替代密码解密测试的资料请关注服务器之家其它相关文章!
原文链接:https://www.it1352.com/OnLineTutorial/cryptography_with_python/cryptography_with_python_simple_substitution_cipher.html