朋友写的。Python实现的。用的是PyCharm编译。
代码如下
def split_list_average(send, n): # 用于分割指定长度字符串
for i in range(0, len(send), n):
yield send[i:i + n]
def check_chip(chipS, chipT, chipU, chipV): # 判断码片之间是否相互正交
def check(chipA, chipB):
if sum(list(map(lambda x, y: x * y, chipA, chipB))) == 0: return True # 判断其内积是否为0
if check(chipS, chipT) and check(chipS, chipU) and check(chipS, chipV) and check(chipT, chipU) and check(chipT, chipV) and check(chipU, chipV):return True
return False
def get_vector(chip, send): # 用于通过传输信息与码片信息得出发送的向量信息
vector = []
reverse = list(map(lambda x: x * (-1), chip))
for i in send:
i = int(i)
if i == 1:
vector.extend(chip)
elif i == 0 or i == -1:
vector.extend(reverse)
else:
print(“发送数据出现非法字符。”)
return
return vector
def send_message(chipS, chipT, chipU, chipV): # 输入四个站分别发送的信息,输入要传输的信息,进而生成发送的加密信息
if not check_chip(chipS, chipT, chipU, chipV):
print(“码片不符合要求”)
return
sendS = input(“请输入S站要发送数据,每个数字之间用空格隔开:”).split(" “)
sendT = input(“请输入T站要发送数据,每个数字之间用空格隔开:”).split(” “)
sendU = input(“请输入U站要发送数据,每个数字之间用空格隔开:”).split(” “)
sendV = input(“请输入V站要发送数据,每个数字之间用空格隔开:”).split(” “)
if len(sendS) == len(sendT) == len(sendU) == len(sendV):
print(”\n码片创建成功,请输入每个站发送信息\n")
vectorS = get_vector(chipS, sendS)
vectorT = get_vector(chipT, sendT)
vectorU = get_vector(chipU, sendU)
vectorV = get_vector(chipV, sendV)
if vectorS is not None and vectorT is not None and vectorU is not None and vectorV is not None:
send = list(map(lambda x, y, z, t: x + y + z + t, vectorS, vectorT, vectorU, vectorV))
print(“发送数据为:”)
print(send)
return send
return
def receive_message(chip, send): # 每个站根据自己的码片信息与接收信息进行正交从而解密接收信息
decrypt = []
if send is not None:
for i in split_list_average(send, len(chip)):
decrypt.append(int(sum(list(map(lambda x, y: x * y, i, chip))) / 8