我正在尝试实现一个类似于Shamir使用Python的秘密共享的加密系统。从本质上讲,我有一些代码可以生成一个点列表,可用于在这些点形成的梯度的y截距处找到密码。密码是ASCII中的数字(每个ASCII字符使用两位数),因此是一个非常大的数字,密码更大。例如,密码ThisIsAPassword将生成一个点列表,如下所示:
x y
9556 66707086867915126140753213946756441607861037300900
4083 28502040182447127964404994111341362715565457349000
9684 67600608880657662915204624898507424633297513499300
9197 64201036847801292531159022293017356403707170463200
需要明确的是,这些点是在随机选择的斜率上生成的(这很好,因为重要的是y截距)。
在尝试制作程序以解码密码时会出现问题。使用正常的数学运算,Python无法准确地找到密码,因为数字的大小。这是我的代码:
def findYint(x,y):
slope = (y[1] - y[0]) / (x[1] - x[0])
yint = int(y[0] - slope * x[0])
return yint
def asciiToString(num):
chars = [num[i:i+3] for i in range(0, len(num), 3)]
return ''.join(chr(int(i)) for i in chars)
def main():
fi = open('pass.txt','r')
x,y = [], []
for i in fi:
row = i.split()
x.append(int(row[0]))
y.append(int(row[1]))
fi.close()
yint = findYint(x,y)
pword = asciiToString(str(yint))
print(pword)
main()
输出(密码为“ThisIsAPassword”):
͉)3 ǢΜĩũć»¢ǔ¼
通常,我的代码将使用较短的密码,例如“pass”或“word”,但是较大的数字可能没有以将其转换为ASCII所需的确切准确性进行计算。使用精确数学或其他方法的任何解决方案?
此外,以下是生成点的代码,以防万一它很重要:
import random
def encryptWord(word):
numlist = []
for i in range(len(word)):
numlist.append(str(ord(word[i])).zfill(3))
num = int("".join(numlist))
return num
def createPoints(pwd, pts):
yint = pwd
gradient = pwd*random.randint(10,100)
xvals = []
yvals = []
for i in range(pts):
n = random.randint(1000,10000)
xvals.append(n)
yvals.append(((n) * gradient) + pwd)
return xvals, yvals
def main():
pword = input("Enter a password to encrypt: ")
pword = encryptWord(pword)
numpoints = int(input("How many points to generate? "))
if numpoints < 2:
numpoints = 2
xpts, ypts = createPoints(pword, numpoints)
喵喵时光机
当年话下
郎朗坤
相关分类