M_Hashir
M_Hashir

Reputation: 3

How to solve this error in VB.net (Expression Expected)

recently I was making a program to encrypt a string. I had code for C# so I converted it into vb now I'm getting this error (Error 1 Expression expected.) in this line of code buffer(--keySize) = Byte.Parse(data(i--)) don't know how to solve it here is my code

Public Function Pkcs1Pad2(data As String, keySize As Integer) As BigInteger
    If (keySize < data.Length + 11) Then
        Return New BigInteger()
    End If
    Dim buffer(256) As Byte
    Dim i = data.Length - 1
    While (i >= 0 And keySize > 0)
        buffer(--keySize) = Byte.Parse(data(i--))
    End While
    Dim random = New Random
    buffer(--keySize) = 0
    While (keySize > 2)
        buffer(--keySize) = Byte.Parse(random.Next(1, 256))
    End While
    buffer(--keySize) = 2
    buffer(--keySize) = 0
    Array.Reverse(buffer)
    Return New BigInteger(buffer)
End Function

And here is original c# code

private static BigInteger Pkcs1Pad2(string data, int keySize)
    {
        if (keySize < data.Length + 11)
            return new BigInteger();

        var buffer = new byte[256];
        var i = data.Length - 1;

        while (i >= 0 && keySize > 0)
        {
            buffer[--keySize] = (byte)data[i--];
        }

        // Padding, I think
        var random = new Random();
        buffer[--keySize] = 0;
        while (keySize > 2)
        {
            buffer[--keySize] = (byte)random.Next(1, 256);
            //buffer[--keySize] = 5;
        }

        buffer[--keySize] = 2;
        buffer[--keySize] = 0;

        Array.Reverse(buffer);

        return new BigInteger(buffer);
    }

Upvotes: 0

Views: 306

Answers (3)

Syed Md. Kamruzzaman
Syed Md. Kamruzzaman

Reputation: 1089

Try this

 keySize -= 1

 buffer(keySize) = Byte.Parse(data(i))

 i -= 1

instead of

   buffer(--keySize) = Byte.Parse(data(i--))

Upvotes: 0

Caius Jard
Caius Jard

Reputation: 74710

Incidentally, the reason why it shows an error on that line but not others (which conceptually do have errors) is because that line uses c# suffix form of decrement

As others have noted, suffixing -- such as x-- in c# means "decrement x but return the value of x before it was decremented", and it has a corollary prefix form of --x meaning "decrement x then return the decremented value"

This means if x is 10:

z = array[x--] //array index 10 is now in z and x is 9
z = array[--x] //array index 9 is now in z and x is 9

Your converter didn't touch any of the decrements in the code, and sometimes the c# used --x and sometimes it used x--

This is a problem for you because --x is actually legal VB; prefixing a variable with a minus sign is the same as multiplying it by -1, so if x is 10, saying:

Dim z = -x

Puts -10 in z

Any number of minis signs can be used, but an even number is a non op (double negative is a positive); --x is 10 if x is 10, as is ----x, or ------x

x-- is not legal vb so you got an error from the compiler, but even after you fix up this line of code you still have the problem that your other lines of code are full of valid, but useless double negatives. In c# those -- decremented the variable. In vb they do not. It means a line like:

buffer(--keySize) = 2

Is syntactically valid vb but logically it does not do what you want - in c# if keysize is 4, it is decremented to 3 then array index three is given a value of 2. In vb, if keysize is 4, it's double negative'd to still be 4 then array index four is set to 2


Public Function Pkcs1Pad2(data As String, keySize As Integer) As BigInteger
    If (keySize < data.Length + 11) Then
        Return New BigInteger()
    End If
    Dim buffer(256) As Byte
    Dim i = data.Length - 1
    While (i >= 0 And keySize > 0)

        keySize -= 1 'VB "decrement x on previous line" = c# "decrement x then return x"
        buffer(keySize) = Convert.ToByte(data(i))
        i -= 1       'VB "decrement x on next line" = c# "use x then return decremented x"

    End While
    Dim random = New Random
    keySize -= 1 'VB "decrement x on previous line" = c# "decrement x then return x"
    buffer(keySize) = 0
    While (keySize > 2)

        keySize -= 1 'VB "decrement x on previous line" = c# "decrement x then return x"
        buffer(keySize) = Convert.ToByte(random.Next(1, 256))
    End While

    keySize -= 1 'VB "decrement x on previous line" = c# "decrement x then return x"
    buffer(keySize) = 2

    keySize -= 1 'VB "decrement x on previous line" = c# "decrement x then return x"
    buffer(keySize) = 0

    Array.Reverse(buffer)
    Return New BigInteger(buffer)
End Function

I make no warranty that this code does what it claims to do, only that it is logically equivalent to the C#. Personally I at least 2, if not 3, of those buffer assignments could have done just using fixed indexes - indexing by/decrementing keysize is rather pointless fingerwear

This answer incorporates advice jmcilhinney discusses in greater depth re byte conversion - give that answer an upvote too, if you upvote this

Upvotes: 1

jmcilhinney
jmcilhinney

Reputation: 54477

In C# you can increment or decrement a variable and use the value of that variable in one expression. You can also use the value before or after the increment or decrement. If you execute this code in C#:

var x1 = 10;
var x2 = 10;
var x3 = 10;
var x4 = 10;

Console.WriteLine(x1);
Console.WriteLine(x2);
Console.WriteLine(x3);
Console.WriteLine(x4);
Console.WriteLine();

Console.WriteLine(x1++);
Console.WriteLine(++x2);
Console.WriteLine(x3--);
Console.WriteLine(--x4);
Console.WriteLine();

Console.WriteLine(x1);
Console.WriteLine(x2);
Console.WriteLine(x3);
Console.WriteLine(x4);

then you'll see this output:

10
10
10
10

10
11
10
9

11
11
9
9

There is no equivalent to that in VB. VB requires that you perform the increment or decrement as a separate operation, before or after using the value. The equivalent VB code to the C# code above would be the following:

Dim x1 = 10
Dim x2 = 10
Dim x3 = 10
Dim x4 = 10

Console.WriteLine(x1)
Console.WriteLine(x2)
Console.WriteLine(x3)
Console.WriteLine(x4)
Console.WriteLine()

Console.WriteLine(x1)
x1 += 1

x2 += 1
Console.WriteLine(x2)

Console.WriteLine(x3)
x3 -= 1

x4 -= 1
Console.WriteLine(x4)

Console.WriteLine()

Console.WriteLine(x1)
Console.WriteLine(x2)
Console.WriteLine(x3)
Console.WriteLine(x4)

As you can see, the line that performs the increment or decrement must come before or after the line that uses the value in VB based on whether the operator is placed before or after the variable in C#.

That means that the most literal translation of this line:

buffer[--keySize] = (byte)data[i--];

would be this:

keySize -= 1
buffer(keySize) = DirectCast(data(i), Byte)
i -= 1

That said, given that data is a String and, therefore, data(i) is a Char, I think the code you need would actually be this:

keySize -= 1
buffer(keySize) = Convert.ToByte(data(i))
i -= 1

That will give you the ASCII/Unicode value of the character as a Byte. the Byte.Parse call that you originally have couldn't be right unless you're actually trying to convert the digits "0" to "9" into numeric Bytes, which that C# code is not doing.

Upvotes: 2

Related Questions