文章目录
效果
代码
import pandas as pd
# target:明文标准化
# params:明文
# return:标准化明文
def trans_(m: str, padding_alpha: str)-> pd.core.series.Series:
m = m.lower().replace(' ','')
temp_dic = {}
index = 0
flag = 0
while(flag < len(m)):
if(flag+1 < len(m)):
if(m[flag] == m[flag+1]):
temp_dic[index] = m[flag] + padding_alpha
index += 1
flag += 1
else:
temp_dic[index] = m[flag:flag + 2]
index += 1
flag += 2
else:
temp_dic[index] = m[flag] + padding_alpha
index += 1
flag += 1
return pd.Series(temp_dic)
# target:密钥矩阵创建
# params:密钥产生字符串,禁用字母i/j
# return:字母->坐标 and 坐标->字母 字典列表
def create_km(s: str, drop_alpha: str)-> list:
lt = list(set(list(s)))
# 按照list(s)中的顺序排序(set打乱顺序需要恢复)
lt.sort(key = list(s).index)
alpha_lt = list(map(chr, list(range(97,97+26))))
alpha_lt.remove(drop_alpha)
for alpha in alpha_lt:
if alpha not in lt:
lt.append(alpha)
dic_alpha2index = {}
dic_index2alpha = {}
for row in range(5):
for col in range(5):
dic_index2alpha[(row, col)] = lt[row * 5 + col]
dic_alpha2index[lt[row * 5 + col]] = (row, col)
return [dic_alpha2index, dic_index2alpha]
# target:通过密钥矩阵进行代换
# params:密钥矩阵,坐标0,坐标1,不同行不同列代换方向,同行或同列代换方向
# return: 代换后的字符串
def str_replace(dic_index2alpha, index_0, index_1, direction, exchange_direction):
# index0 = (x1, y1)
# index1 = (x2, y2)
# 不同行不同列
if(index_0[0] != index_1[0] and index_0[1] != index_1[1]):
alpha0 = dic_index2alpha[(index_0[direction], index_1[(direction+1)%2])]
alpha1 = dic_index2alpha[(index_1[direction], index_0[(direction+1)%2])]
else:
step = 1 - exchange_direction*2
# 同行不同列
if(index_0[0] == index_1[0] and index_0[1] != index_1[1]):
alpha0 = dic_index2alpha[(index_0[0], (index_0[1]+step)%5)]
alpha1 = dic_index2alpha[(index_1[0], (index_1[1]+step)%5)]
# 同列不同行
else:
alpha0 = dic_index2alpha[((index_0[0]+step)%5, index_0[1])]
alpha1 = dic_index2alpha[((index_1[0]+step)%5, index_1[1])]
return alpha0+alpha1
# target:加密
# params:密钥矩阵,明文,不同行列替换方式,同行或列替换方式
# return:密文
def encrypt(key_matrix: list, m_t: pd.core.series.Series, direction: int, exchange_direction: int):
func = lambda s: str_replace(key_matrix[1] ,key_matrix[0][s[0]], key_matrix[0][s[1]], direction, exchange_direction)
return m_t.map(func)
# target:解密
# params:密钥矩阵,密文,不同行列替换方式,同行或列替换方式
# return:明文
def decrypt(key_matrix: list, c: pd.core.series.Series, direction: int, exchange_direction: int):
func = lambda s: str_replace(key_matrix[1] ,key_matrix[0][s[0]], key_matrix[0][s[1]], direction, 1-exchange_direction)
return c.map(func)
# target:将series转为字符串
# params:series
# return:str
def tran_series2str(seri):
ret=''
for s in seri:
ret +=s
return ret
# 以矩阵形式查看密钥矩阵
# 密钥矩阵
# 无
def show_key_matrix(key_matrix):
for row in range(5):
s = ''
for col in range(5):
s += key_matrix[1][(row, col)] + ' '
print(s)
if __name__ == "__main__":
# 密钥生成字符串
s = 'hello'
# 禁用i或j
drop_alpha = 'j'
# padding用于填充的字母
padding_alpha = 'x'
# 0按照行替换,1按照列替换
direction = 0
# 0以后一个位置代替当前位置,1以前一个位置代替当前位置
exchange_direction = 0
# 明文
M = 'how are you tony'
# 标准化明文
m_t = trans_(M, padding_alpha)
# 密钥矩阵生成
key_matrix = create_km(s, drop_alpha)
# 标准化密文
c_t = encrypt(key_matrix, m_t, direction, exchange_direction)
# 密文字符串
c_str = tran_series2str(c_t)
# 标准化明文
m = decrypt(key_matrix, c_t, direction, exchange_direction)
# 明文字符串
m_str = tran_series2str(m)