以下是一个功能性解密和加密程序的一部分代码。
while checkvar < maxvar: # is set to < as maxvar is 1 to high for the index of var
#output.append("%02d" % number)
i =ord(var[checkvar]) - 64 # Gets postional value of i
i = ("%02d" % i)
if (checkvar + 1) < maxvar:
j =ord(var[(checkvar + 1)]) - 64 # Gets postional value of i
j = ("%02d" % j)
i = str(i) + str(j) #'Adds' the string i and j to create a new i
li.append(int(i))
checkvar = checkvar + 2
print li
你可以看到,变量i和j首先被视为字符串来添加一个0在任何个位数前面(作为字符串)。 然后将这些变量组合成四位数字(仍然是字符串)。 程序后来使用创建的数字在pow()
函数中,因为int
会删除任何前导零。
我的问题是:是否有可能强制Python保留int
的前导零?我已经在网上搜索过。
编辑
为了帮助其他人,我已经包含了程序的加密部分。 这就是问题所在。 在上述代码中创建的变量通过pow()
函数传递。 由于它无法处理字符串,因此必须将变量转换为int
,其中前导零丢失。
#a = li[]
b=int(17)#pulic = e
c=int(2773)#=n
lenli=int(len(li))
enchecker = int(0)
#encrpted list
enlist = []
while enchecker < lenli:
en = pow(li[enchecker],b,c)#encrpyt the message can only handle int
enlist.append(int(en))
enchecker = enchecker + 1
print enlist
i = "%02d" %i
,这里是正确的做法... 为什么它对你不起作用? - inspectorG4dget