import numpy as np
m = 'YOURPINNOISFOURONETWOSIX' #明文
a = np.matrix([[11,2,19],[5,23,25],[20,7,17]]) #密鑰LCTFXZUHR
num_m = []
temp = []
count = 1
for i in m: #將明文分為三個一組
temp.append(ord(i)-ord('A'))
if count % 3 == 0:
num_m.append(temp)
temp = []
count += 1
mat_m = [np.matrix(i).T for i in num_m] #將明文分組轉(zhuǎn)換為向量形式
mat_c = [a * i % 26 for i in mat_m] #得到密文分組的向量形式
num_c = []
temp = []
for i in mat_c: #將密文向量轉(zhuǎn)換為列表形式,且合并到一個列表
temp = i.tolist()
for j in range(3):
num_c.append(temp[j][0])
c = [chr(i+ord('A')) for i in num_c]
print(''.join(c)) #連接成字符串,輸出密文
更多建議: