1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| import random
def judgeCoPrime(a, b): def maxCommonFactor(m, n): result = 0 while m % n > 0: result = m % n m = n n = result return result if maxCommonFactor(a, b) == 1: return True return False
def getInverse(a, b): def extGcd(a_, b_, arr): if b_ == 0: arr[0] = 1 arr[1] = 0 return a_ g = extGcd(b_, a_ % b_, arr) t = arr[0] arr[0] = arr[1] arr[1] = t - int(a_ / b_) * arr[1] return g arr = [0,1,] gcd = extGcd(a, b, arr) if gcd == 1: return (arr[0] % b + b) % b else: return -1
def init(length, interval): listV = [] listV_b = [] bagCapacity = 1000 for i in range(length): listV.append(sum(listV) + random.randrange(1, interval)) listV_b.append(0) bagCapacityTmp = bagCapacity for i in range(len(listV)-1, -1, -1): if bagCapacityTmp >= listV[i]: bagCapacityTmp -= listV[i] listV_b[i] = 1 return listV
def creatPKey(listV): while True: k = int(input("输入私钥k(大于%d):" % (sum(listV)))) if k <= sum(listV): print("输入的k值不合法,请重新设置") continue while True: t = int(input("输入私钥t(与k互素):")) if not judgeCoPrime(k, t): print("输入的t与k值不互素,请重新输入") continue break break inverse_t = getInverse(t, k) return k, t, inverse_t
def creatSKey(listV, t, k): sKeyList = [] for i in listV: sKeyList.append(i * t % k) return sKeyList
def encrypt(massage, sKeyList): massageList = [] ciphertextList = [] while len(massage) % len(sKeyList) != 0: massage = "0" + massage for i in range(int(len(massage) / len(sKeyList))): start = (i * len(sKeyList)) end = ((i + 1) * len(sKeyList)) massageList.append(massage[start : end]) for i in massageList: multiplyList = list(map(lambda x, y: int(x) * int(y), list(i), sKeyList)) ciphertextList.append(sum(multiplyList)) return ciphertextList
def decrypt(massage, sKeyList, k, inverse_t): pliantextList = [] reductListV = [] for i in sKeyList: reductListV.append(int(i) * inverse_t % k) bagCapacityList = [] for i in massage: bagCapacityList.append(int(i) * inverse_t % k) for bagCap in bagCapacityList: pliantextTmp = [] for i in range(len(reductListV)): pliantextTmp.append(0) for i in range(len(reductListV) - 1, -1, -1): if bagCap >= reductListV[i]: bagCap -= reductListV[i] pliantextTmp[i] = 1 pliantextList += pliantextTmp start, end = 0, 0 for i in range(len(pliantextList)): if pliantextList[i] != 0: break end = i + 1 del pliantextList[start : end] pliantextList = map(str, pliantextList) pliantext = "".join(pliantextList) return pliantext
if __name__ == "__main__": length = int(input("输入超递增向量元素个数:")) interval = int(input("输入随机增量:")) listV = init(length, interval) print("初始向量:", listV) k, t, inverse_t = creatPKey(listV) print("\n私钥验证成功,分别为 <k:%d>, <t:%d>,<t逆元:%d>" %(k, t, inverse_t)) sKeyList = creatSKey(listV, t, k) print("公钥向量为:", sKeyList, "\n") while True: choice = input("1、加密 2、解密\n请选择:") if choice == "1": massage = input("输入明文(01序列):") ciphertextList = encrypt(massage, sKeyList) print("加密结果:", ciphertextList) elif choice == "2": ciphertextList = list(map(int, list(input("输入密文:").split(",")))) sKeyList = list(map(int, list(input("输入公钥向量:").split(",")))) k = int(input("输入密钥k:")) inverse_t = int(input("输入密钥t逆:")) pliantext = decrypt(ciphertextList, sKeyList, k, inverse_t) print("解密结果:", pliantext)
|