我正在尝试编写一个 Hill-RSA 加密程序,您可以在此处看到部分内容:
q2=31
alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",","," ",".",";","_"]
X=np.zeros((m,1),dtype=np.int32)
Y=np.zeros((m,1),dtype=np.int32)
Texte_decode="";
for i in range(1,(len(Texte_code)/m)+1):
for k in range(0,m):
j=0
while (Texte_code[k+m*(i-1)]<>alphabet[j+1]):
j=j+1
X[k]=j
X=X.transpose()
A2=np.zeros((m,m),dtype=np.int32)
for u in range(0,m):
for l in range(0,m):
A2[u,l]=A[u,l]
Y=X.dot(A2)
Y=Y.transpose()
pprint(Y)
Y2=np.zeros((m,1),dtype=np.int32)
for ind in range(0,m):
Y2[ind]=Y[ind]%q2
pprint(Y2)
for k in range(0,m):
Texte_decode=Texte_decode+alphabet[Y2[k]+1]
for i in range(len(Texte_decode),len(Texte_decode)-m+1,-1):
if Texte_decode[i]=="." and Texte_decode[i-1]==".":
Texte_decode=Texte_decode[1,i-1]
print Texte_decode
执行此部分时,我得到
“ TypeError:只有整数标量数组可以转换为标量索引”
在线
Texte_decode=Texte_decode+alphabet[Y2[k]+1]
有人可以帮我摆脱这个错误吗?
预先感谢
答案 0 :(得分:1)
您完成了哪些调试?您是否审查了问题专线元素的性质?
Texte_decode=Texte_decode+alphabet[Y2[k]+1]
k
来自for k in range(0,m):
,所以这不是问题。显然是整数。
您打印的Y2
。它初始化为(m,1)
数组。所以Y2[k]
将是(1,)数组,对吧?
alphabet
是一个列表。
在交互式外壳中,让我们尝试一个测试用例:
In [70]: [1,2,3,4][np.array([1])]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-70-4ad73b219fa3> in <module>()
----> 1 [1,2,3,4][np.array([1])]
TypeError: only integer scalar arrays can be converted to a scalar index
相同的错误消息!
如果我们从一维数组开始,然后选择一个元素,则索引将起作用:
In [71]: [1,2,3,4][np.arange(4)[1]]
Out[71]: 2
现在我们了解了问题,解决方案应该很明显,对吧?