"TypeError: only integer scalar arrays can be converted to a scalar index" on Hill-RSA cryptography

"TypeError: only integer scalar arrays can be converted to a scalar index" on Hill-RSA cryptography

我正在尝试编写一个 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: only integer scalar arrays can be converted to a scalar index"

上线

Texte_decode=Texte_decode+alphabet[Y2[k]+1]

谁能帮我解决这个错误?

提前致谢

你做了哪些调试?您是否查看了问题行的元素的性质?

Texte_decode=Texte_decode+alphabet[Y2[k]+1]

k 来自 for k in range(0,m): 所以这不应该是问题所在。明明是整数。

您打印的 Y2。它被初始化为一个 (m,1) 数组。所以 Y2[k] 将是一个 (1,) 数组,对吗?

alphabet 是一个列表。

在交互式 shell 中,让我们尝试一个测试用例:

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

同样的错误信息!

如果我们从一维数组开始,select 一个元素,索引有效:

In [71]: [1,2,3,4][np.arange(4)[1]]
Out[71]: 2

既然我们了解了问题,解决方案应该很明显了吧?