# 输入矩阵并判断是否存在逆矩阵
def inputMatrix():
while True:
# 输入一行、作为行列式的阶数和行列式的第一行
rank = list(input("").split())
matrix = [[0] * len(rank) for i in range(len(rank))]
matrix[0] = rank
# 输入行列式剩余数据
for i in range(1, len(matrix)):
matrix = list(input("").split())
# 判断每一行输入是否合法
if len(matrix) != len(matrix):
print("输入有误,重新输入。")
continue
# 转换字符型为整型
for i in range(len(matrix)):
matrix = list(map(lambda x: int(x), matrix))
# 判断是否存在逆矩阵
if not judgeInverse(matrix):
print("矩阵不存在逆矩阵,重新输入。")
continue
return matrix
# 生成消息分组
def createMassageList(massage, matrix):
matrixRank = len(matrix)
massageList = []
# 扩充消息序列并创建分组
while len(massage) % matrixRank != 0:
massage += " "
for i in range(1, len(massage) + 1, matrixRank):
massageList.append(massage[i-1:i + matrixRank - 1])
return massageList
# 字母序列转化为数字
def letterToDigit(massageList):
massageDigitList = [] # 替换后的数字列表
letterList = [] # 字母列表
for i in range(ord("a"), ord("z") + 1):
letterList.append(chr(i))
for i in range(10):
letterList.append(str(i))
# 添加空格,解决分组填充问题
letterList.append(" ")
# 替换字母为数字
for massage in massageList:
listTmp = []
for i in range(len(massage)):
listTmp.append(letterList.index(massage))
massageDigitList.append(listTmp)
return massageDigitList
# 数字序列转化为字母
def digitToLetter(massageList):
massageLetterList = [] # 还原后的字母列表
letterList = []
for i in range(ord("a"), ord("z") + 1):
letterList.append(chr(i))
for i in range(10):
letterList.append(str(i))
letterList.append(" ")
# 替换数字为字母
for massage in massageList:
massageLetterList.append(letterList[massage % 37])
return massageLetterList
# 加密
def encrypt(massage, matrix):
ciphertextList = [] # 加密结果列表
massageList = createMassageList(massage, matrix)
massageDigitList = letterToDigit(massageList)
# 矩阵相乘
for massageDigit in massageDigitList:
for i in range(len(massageDigit)):
sum = 0
for j in range(len(massageDigit)):
sum += massageDigit[j] * matrix[j][i % len(matrix)]
ciphertextList.append(sum % 37)
return ciphertextList
# 解密
def decrypt(massage, matrix):
plaintextList = [] # 解密结果列表
matrix_inverse = createMatrixInverse(matrix)
massageList = createMassageList(massage, matrix)
# 矩阵相乘
for msg in massageList:
for i in range(len(msg)):
sum = 0
for j in range(len(msg)):
sum += msg[j] * matrix_inverse[j][i % len(matrix)]
plaintextList.append(sum % 37)
# 浮点型转换为整型(采用四舍五入——round())
plaintextList = list(map(lambda x: int(round(x)), plaintextList))
plaintextList = digitToLetter(plaintextList) # 数字转换为字母
plaintext = ""
for item in plaintextList:
plaintext += item
return plaintext