I'm trying to code a Hill-RSA cryptography program that you can see a part of here:
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
When i execute this part, I get
"TypeError: only integer scalar arrays can be converted to a scalar index"
on the line
Texte_decode=Texte_decode+alphabet[Y2[k]+1]
Can anyone help me get rid of this error?
Thanks in advance
What debugging have you done? Did you review the nature of the elements of the problem line?
Texte_decode=Texte_decode+alphabet[Y2[k]+1]
k
comes from for k in range(0,m):
so that shouldn't be the problem. It's clearly an integer.
Your printed Y2
. It's initialed as a (m,1)
array. So Y2[k]
will be a (1,) array, right?
alphabet
is a list.
In an interactive shell let's try a test case:
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
Same error message!
If we start with a 1d array, and select an element, the indexing works:
In [71]: [1,2,3,4][np.arange(4)[1]]
Out[71]: 2
Now that we understand the problem, the solution should be obvious, right?