# 生成密钥 defcreateKey(keyLength): # 方法生成 # keyList = [i for i in range(1, keyLength + 1)] # shuffle(keyList) # 生成随机排序 # 手工生成 keyList = [] whilelen(keyList) < keyLength: for i inrange(1, keyLength + 1): key = randrange(1, keyLength + 1) if key notin keyList: keyList.append(key) return keyList
# 加密 defencrypt(massage, keyList): ciphertext = "" # 创建明文分组 massageList = inputMassage(massage, keyLength) for item in massageList: # 存储改变字母位置后的临时列表 itemList = [0for i inrange(len(keyList))] for i inrange(len(keyList)): itemList[i] = list(item)[keyList[i] - 1] ciphertext += itemList[i] return ciphertext
# 解密 defdecrypt(massage, keyList): plaintext = "" plaintextList = inputMassage(massage, keyLength) for item in plaintextList: # 存储改变字母位置后的临时列表 itemList = [0for i inrange(len(keyList))] for i inrange(len(keyList)): itemList[keyList[i] - 1] = list(item)[i] for i in itemList: plaintext += str(i) return plaintext