0

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

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125

1 Answers1

1

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?

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array – Victor Jacquens Nov 21 '18 at 08:38
  • `Y2=np.zeros((m,1),dtype=np.int32)` – hpaulj Nov 21 '18 at 10:45
  • This is how I declared Y2. What improvment do you suggest me to do? – Victor Jacquens Nov 21 '18 at 14:38
  • Explain why you chose to initial `Y2` with that shape? What's different in my 2 test examples? – hpaulj Nov 21 '18 at 15:41
  • So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it [link](https://lh6.googleusercontent.com/YxZD9n9GLPcv-DFKjG5-eQ4JBUMntkSUUMQF9z3jbnVuEJaXYHL1LJixZKLS9L0ZHui37SkV5cAbRNUQPfr0=w1920-h944) – Victor Jacquens Nov 21 '18 at 16:53
  • I don't need to know what the Maple code is supposed to do. We are dealing with a simple `numpy` case here. If you are going to get anywhere with Python/numpy coding you need to understand it's `shape` and `indexing` approaches. The difference between `np.zeros((m,1), int)` and `np.zeros(m, int)` matters in this situation. Do you know how to test Python code in an interactive session? I can't code `numpy` without window open where I can test bits and pieces of the code to verify that they work. I tried to illustrate that in my answer. – hpaulj Nov 21 '18 at 17:42