简述:
仿射密码为单表加密的一种,字母系统中所有字母都藉一简单数学方程加密,对应至数值,或转回字母。
加密:
E(x) = (ax + b) (mod m)
,其中 a与b互质,m是编码系统中字母的个数(通常都是26)。
解密:
D(x) = a^{-1} (x - b) (mod m)
,其中 a^{-1} 是 a 在Z_{m}群(此处为26)的乘法逆元。
代码:
# 创建字母表
def creatLetterList():
letterList = []
for i in range(ord("a"), ord("z") + 1):
letterList.append(chr(i))
return letterList
# 判断互质
def judgeCoPrime(a, b):
# 求最大公因数
def maxCommonFactor(m, n):
result = 0
while m % n > 0:
result = m % n
m = n
n = result
return result
if maxCommonFactor(a, b) == 1:
return True
return False
# 求逆元
def getInverse(a, b