#暴力破解仿射密码
def decrypt():
k1 = [1,3,5,7,9,11,15,17,19,21,23,25]
n = 1
ny = []
for i in k1:
while (i * n) % 26 != 1:
n += 1
ny.append(n)
f = open('cipher08.txt')
w = open('plain08.txt','w')
cipher = f.read()
p = []
for k1 in ny:
for k2 in range(1,27):
p.append('\n 逆元='+str(k1)+' k2='+str(k2)+' ')
# plain = ''.join('\n 逆元='+str(k1)+'k2='+str(k2)+' ')
for i in range(len(cipher)):
#小写字母
if cipher[i].islower():
t1 = ord(cipher[i])-97-k2
if t1 < 0:
t1 +=26
p.append(chr((k1 * t1)%26+97))
#大写字母
elif cipher[i].isupper():
t2 = ord(cipher[i])-65-k2
if t2 < 0:
t2 +=26
p.append(chr((k1 * t2)%26+65))
#其他
else :
p.append(cipher[i])
plain = ''.join(p)
w.write(plain)
print('解密完成!')
f.close()
w.close()
if __name__ == '__main__':
decrypt()
python暴力破解仿射密码
最新推荐文章于 2023-11-10 21:22:47 发布