-
Notifications
You must be signed in to change notification settings - Fork 55
/
Affine.py
76 lines (66 loc) · 1.56 KB
/
Affine.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
#
# Affine cipher
#
# @author lellansin <lellansin@gmail.com>
# @website http://www.lellansin.com/tutorials/ciphers
#
WIDTH = 26 # 密表宽度
ASC_A = ord('A') # 密表起点 (使用字母表做密表)
#
# 加密
#
def encrypt(key, words):
return ''.join([shift(key, ch) for ch in words.upper()])
#
# 解密
#
def decrypt(key, words):
a, b = modInverse(key[0], WIDTH), -key[1]
return ''.join([unshift([a, b], ch) for ch in words.upper()])
#
# E(x) = (ax + b) mod m
#
def shift(key, ch):
if str.isalpha(ch):
offset = ord(ch) - ASC_A
return chr(((key[0] * offset + key[1]) % WIDTH) + ASC_A)
return ''
#
# D(x) = a^{-1}(x - b) mod m
#
def unshift(key, ch):
offset = ord(ch) - ASC_A
return chr(((key[0] * (offset + key[1])) % WIDTH) + ASC_A)
#
# 判断 a 与 b 是否互素 (Euclid's Algorithm)
#
def gcd(a, b):
while a != 0:
a, b = b % a, a
return b
#
# 求 a 在密表中的乘法逆
#
def modInverse(a, m):
if gcd(a, m) != 1:
print("Error: a 与 m 不互素")
quit()
u1, u2, u3 = 1, 0, a
v1, v2, v3 = 0, 1, m
while v3 != 0:
q = u3 // v3
v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
return u1 % m
if __name__ == '__main__':
# 本例推算见 http://en.wikipedia.org/wiki/Affine_cipher
# 明文
text = 'AFFINECIPHER'
# 密匙
key = [5, 8]
# 加密
ciphertext = encrypt(key, text)
# 密文
print (ciphertext)
# 解密
print (decrypt(key, ciphertext))