none
number of indices is less than the number of dimensions of the indexed array

    Question

  • hello, i tried to implement DES with ECB mode but without using lib framework and i have an error in this line 

    Public Sub New(ByVal boxNum As Integer)
        table = TABLES(boxNum)
      End Sub
    

    or maybe there is another alternative to implement

    this is the complete class of sbox

    '''************************************************************************************
    ''' The SBox class implements all 8 substitution boxes for the DES encryption algorithm *
    '''************************************************************************************
    
    Public Class SBox
    
      Public Shared TABLES(,) As SByte = {{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, _
                         0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, _
                         4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, _
                         15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}, _
                        {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, _
                         3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, _
                         0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, _
                         13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}, _
                        {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, _
                         13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, _
                         13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, _
                         1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}, _
                        {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, _
                         13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, _
                         10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, _
                         3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}, _
                        {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, _
                         14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, _
                         4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, _
                         11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}, _
                        {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, _
                         10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, _
                         9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, _
                         4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}, _
                        {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, _
                         13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, _
                         1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, _
                         6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}, _
                        {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, _
                         1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, _
                         7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, _
                         2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}}
    
    
      '''*************
      ''' Private data *
      '''*************
    
      Private table() As SByte 'the substitution table for this s-box
    
      Public Sub New(ByVal boxNum As Integer)
        table = TABLES(boxNum)
      End Sub
    
      '''********************************************************************
      ''' The substitue function: given a 6-bit value, returns a 4-bit value. *
      '''********************************************************************
    
      Public Overridable Function substitute(ByVal input As SByte) As SByte
        'get row number for s-box table
        Dim row As SByte = CSByte((input And &H20) Or ((input * 16) And &H10))
    
        'get column number
        Dim col As SByte = CSByte((input >> 1) And &HF)
    
        Return table(row Or col)
      End Function
    
    End Class
    

    Monday, July 19, 2010 8:02 AM

Answers

  • You declared TABLES as an array with two dimensions the first element being TABLES(0,0)

    Public Shared TABLES(,) As SByte

    But then you refer to TABLES as a single dimension array that is not allowed .

      Public Sub New(ByVal boxNum As Integer)
        table = TABLES(boxNum)
      End Sub

     As John has said use a type of collection that allows elements that are arrays or lists such as an Arraylist or list as john used .

     


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    • Marked as answer by Liliane Teng Sunday, July 25, 2010 5:08 AM
    Wednesday, July 21, 2010 3:31 AM
  • Hi vrainz,

    Your tables have 64 values.

    The way you are trying to setup the array, it would be as if you could do a 64 dimension array.

    Maximum number of dimensions allowed for an array is 32.

    Anyway, I have set up an array of LISTs

    Each LIST is a table. I hope you are with me so far.

    The PRIVATE value table in the below code is a 1 dimensional array of type SByte.

     

    Tell me if you have any problem with this code please.

     

    Do you really want a logical bitwise OR between the row and column

    Or Return Tables(row).Item(col) ?

     

    Public Class SBox
    
      Public Shared TABLES(7) As List(Of SByte)
    
      Private Sub SetupTables()
    
        TABLES(0) = New List(Of SByte)
        TABLES(0).AddRange(New SByte() _
        {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, _
         0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, _
         4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, _
         15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13})
    
        TABLES(1) = New List(Of SByte)
        TABLES(1).AddRange(New SByte() _
        {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, _
         3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, _
         0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, _
         13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9})
    
        TABLES(2) = New List(Of SByte)
        TABLES(2).AddRange(New SByte() _
        {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, _
         13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, _
         13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, _
         1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12})
    
        TABLES(3) = New List(Of SByte)
        TABLES(3).AddRange(New SByte() _
        {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, _
        13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, _
        10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, _
        3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14})
    
        TABLES(4) = New List(Of SByte)
        TABLES(4).AddRange(New SByte() _
        {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, _
         14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, _
         4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, _
         11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3})
    
        TABLES(5) = New List(Of SByte)
        TABLES(5).AddRange(New SByte() _
         {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, _
         10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, _
         9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, _
         4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13})
    
        TABLES(6) = New List(Of SByte)
        TABLES(6).AddRange(New SByte() _
         {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, _
          13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, _
          1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, _
          6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12})
    
        TABLES(7) = New List(Of SByte)
        TABLES(7).AddRange(New SByte() _
          {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, _
          1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, _
          7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, _
          2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11})
    
      End Sub
    
      '''*************
      ''' Private data *
      '''*************
    
      Private table() As SByte 'the substitution table for this s-box
    
      Public Sub New(ByVal tableNumber As Integer)
        Call SetupTables()
        table = TABLES(tableNumber).ToArray
      End Sub
    
      '''********************************************************************
      ''' The substitue function: given a 6-bit value, returns a 4-bit value. *
      '''********************************************************************
    
      Public Overridable Function substitute(ByVal input As SByte) As SByte
        'get row number for s-box table
        Dim row As SByte = CSByte((input And &H20) Or ((input * 16) And &H10))
    
        'get column number
        Dim col As SByte = CSByte((input >> 1) And &HF)
    
        Return table(row Or col)
      End Function
    
    End Class

    Regards, John
    • Marked as answer by Liliane Teng Sunday, July 25, 2010 5:08 AM
    Monday, July 19, 2010 3:08 PM

All replies

  • Hi vrainz,

    Your tables have 64 values.

    The way you are trying to setup the array, it would be as if you could do a 64 dimension array.

    Maximum number of dimensions allowed for an array is 32.

    Anyway, I have set up an array of LISTs

    Each LIST is a table. I hope you are with me so far.

    The PRIVATE value table in the below code is a 1 dimensional array of type SByte.

     

    Tell me if you have any problem with this code please.

     

    Do you really want a logical bitwise OR between the row and column

    Or Return Tables(row).Item(col) ?

     

    Public Class SBox
    
      Public Shared TABLES(7) As List(Of SByte)
    
      Private Sub SetupTables()
    
        TABLES(0) = New List(Of SByte)
        TABLES(0).AddRange(New SByte() _
        {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, _
         0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, _
         4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, _
         15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13})
    
        TABLES(1) = New List(Of SByte)
        TABLES(1).AddRange(New SByte() _
        {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, _
         3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, _
         0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, _
         13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9})
    
        TABLES(2) = New List(Of SByte)
        TABLES(2).AddRange(New SByte() _
        {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, _
         13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, _
         13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, _
         1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12})
    
        TABLES(3) = New List(Of SByte)
        TABLES(3).AddRange(New SByte() _
        {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, _
        13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, _
        10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, _
        3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14})
    
        TABLES(4) = New List(Of SByte)
        TABLES(4).AddRange(New SByte() _
        {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, _
         14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, _
         4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, _
         11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3})
    
        TABLES(5) = New List(Of SByte)
        TABLES(5).AddRange(New SByte() _
         {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, _
         10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, _
         9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, _
         4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13})
    
        TABLES(6) = New List(Of SByte)
        TABLES(6).AddRange(New SByte() _
         {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, _
          13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, _
          1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, _
          6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12})
    
        TABLES(7) = New List(Of SByte)
        TABLES(7).AddRange(New SByte() _
          {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, _
          1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, _
          7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, _
          2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11})
    
      End Sub
    
      '''*************
      ''' Private data *
      '''*************
    
      Private table() As SByte 'the substitution table for this s-box
    
      Public Sub New(ByVal tableNumber As Integer)
        Call SetupTables()
        table = TABLES(tableNumber).ToArray
      End Sub
    
      '''********************************************************************
      ''' The substitue function: given a 6-bit value, returns a 4-bit value. *
      '''********************************************************************
    
      Public Overridable Function substitute(ByVal input As SByte) As SByte
        'get row number for s-box table
        Dim row As SByte = CSByte((input And &H20) Or ((input * 16) And &H10))
    
        'get column number
        Dim col As SByte = CSByte((input >> 1) And &HF)
    
        Return table(row Or col)
      End Function
    
    End Class

    Regards, John
    • Marked as answer by Liliane Teng Sunday, July 25, 2010 5:08 AM
    Monday, July 19, 2010 3:08 PM
  • thx alot John...

    i took this original file from java :: http://www.mediafire.com/?ogf3dasg88w9i3j

    and then i try to convert it to vb.net :: http://www.mediafire.com/?3pcnw7pp020d8hj

    for the moment there is no error in class sbox, but since i have not finished implementing it i dont know where the error

    now i have the problem in calling the encrypt method,.

    Tuesday, July 20, 2010 4:40 AM
  • thx alot John...

    i took this original file from java :: http://www.mediafire.com/?ogf3dasg88w9i3j

    and then i try to convert it to vb.net :: http://www.mediafire.com/?3pcnw7pp020d8hj

    for the moment there is no error in class sbox, but since i have not finished implementing it i dont know where the error

    now i have the problem in calling the encrypt method,.


    Did you try the code John has provided for you or did he spent his time to help you for nothing?
    Success
    Cor
    Tuesday, July 20, 2010 8:23 AM
  • thx alot John...

    i took this original file from java :: http://www.mediafire.com/?ogf3dasg88w9i3j

    and then i try to convert it to vb.net :: http://www.mediafire.com/?3pcnw7pp020d8hj

    for the moment there is no error in class sbox, but since i have not finished implementing it i dont know where the error

    now i have the problem in calling the encrypt method,.

    Hi vrainz,

    Please mark my earlier post as helpful or / and as answer if you feel it answers or helps with your original question here.

     

    If you still have problems, then please start a new question but you can reference this thread with.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c4e43cbd-6233-4fc4-88b3-7abc23f00445

    if you wish to do so, in the new question.


    Regards, John
    Tuesday, July 20, 2010 11:56 PM
  • You declared TABLES as an array with two dimensions the first element being TABLES(0,0)

    Public Shared TABLES(,) As SByte

    But then you refer to TABLES as a single dimension array that is not allowed .

      Public Sub New(ByVal boxNum As Integer)
        table = TABLES(boxNum)
      End Sub

     As John has said use a type of collection that allows elements that are arrays or lists such as an Arraylist or list as john used .

     


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    • Marked as answer by Liliane Teng Sunday, July 25, 2010 5:08 AM
    Wednesday, July 21, 2010 3:31 AM
  • thx for all of you.
    I guess for an array of problems in this class had settled.
    there is no error anymore for this class
    once again thx for your help

    regards
    Wednesday, July 21, 2010 9:39 AM