Solved

Translation help: Java to VB .Net

Posted on 2011-03-06
10
812 Views
Last Modified: 2013-11-27
Java code:
private String decrypt(String str_hex, String str_key1, String str_key2) {
 
        // 1. Convert hexadecimal string to binary string
        char[] chr_hex = str_hex.toCharArray();
        String str_bin = "";
        for(int i = 0; i < 32; i++) {
            String temp1 = Integer.toBinaryString(Integer.parseInt(Character.toString(chr_hex[i]), 16));
            while (temp1.length() < 4) {
                temp1 = "0" + temp1;
            }
            str_bin += temp1;
        }
        char[] chr_bin = str_bin.toCharArray();
 
        // 2. Generate switch and XOR keys
        int key1 = Integer.parseInt(str_key1);
        int key2 = Integer.parseInt(str_key2);
        int[] key = new int[384];
        for (int i = 0; i < 384; i++) {
            key1 = (key1 * 11 + 77213) % 81371;
            key2 = (key2 * 17 + 92717) % 192811;
            key[i] = (key1 + key2) % 128;
        }
 
        // 3. Switch bits positions
        for (int i = 256; i >= 0; i--) {
            char temp3 = chr_bin[key[i]];
            chr_bin[key[i]] = chr_bin[i%128];
            chr_bin[i%128] = temp3;
        }
 
        // 4. XOR entire binary string
        for (int i = 0; i < 128; i++) {
            chr_bin[i] = (char)(chr_bin[i] ^ key[i+256] & 1);
        }
 
        // 5. Convert binary string back to hexadecimal
        str_bin = new String(chr_bin);
        str_hex = "";
        for(int i = 0; i < 128; i+=4) {
            str_hex += Integer.toHexString(Integer.parseInt(str_bin.substring(i, i+4), 2));
        }
 
        // 6. Return counted string
        return str_hex;
    }

Open in new window


My translation to vb with error:
    Private Function decrypt(ByVal str_hex As String, ByVal str_key1 As String, ByVal str_key2 As String) As String

        ' 1. Convert hexadecimal string to binary string
        Dim chr_hex() As Char = str_hex.ToCharArray()
        Dim str_bin As String = ""
        For i As Integer = 0 To 31
            Dim temp1 As String = Convert.ToInt32(Char.ToString(chr_hex(i)), 16)
            Do While temp1.Length < 4
                temp1 = "0" & temp1
            Loop
            str_bin += temp1
        Next i
        Dim chr_bin() As Char = str_bin.ToCharArray()

        ' 2. Generate switch and XOR keys
        Dim key1 As Integer = Convert.ToInt32(str_key1)
        Dim key2 As Integer = Convert.ToInt32(str_key2)
        Dim key(383) As Integer
        For i As Integer = 0 To 383
            key1 = (key1 * 11 + 77213) Mod 81371
            key2 = (key2 * 17 + 92717) Mod 192811
            key(i) = (key1 + key2) Mod 128
        Next i

        ' 3. Switch bits positions
        For i As Integer = 256 To 0 Step -1
            Dim temp3 As Char = chr_bin(key(i))
            chr_bin(key(i)) = chr_bin(i Mod 128)
            chr_bin(i Mod 128) = temp3
        Next i

        ' 4. XOR entire binary string
        For i As Integer = 0 To 127
            chr_bin(i) = CChar(chr_bin(i) Xor key(i + 256) And 1)       'getting this error: Operator 'Xor' is not defined for types 'Char' and 'Integer'.
        Next i

        ' 5. Convert binary string back to hexadecimal
        str_bin = New String(chr_bin)
        str_hex = ""
        For i As Integer = 0 To 127 Step 4
            str_hex += Convert.ToInt32(str_bin.Substring(i, 4), 2)
        Next i

        ' 6. Return counted string
        Return str_hex

    End Function

Open in new window


So far in my translation I am getting this error:
Operator 'Xor' is not defined for types 'Char' and 'Integer'.


To test it, when you find out whats worng you can call it with: decrypt("adae225909bb6c445fe27aa919386454", "34233", "51162") and if you get "e54017d16ca68e8a9127a38731cd7173" as result It is translated right.

I don't know if my translation is "very" wrong so please, just help me get this to be translated properly.

Thank you.
0
Comment
Question by:JoachimPetersen
  • 5
  • 4
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35047264
Try casting the Char to Integer first:
chr_bin(i) = CChar(Convert.ToInt32(chr_bin(i)) Xor key(i + 256) And 1)

Open in new window

0
 

Author Comment

by:JoachimPetersen
ID: 35047328
That gives me another error:
'Integer' values cannot be converted to 'Char'. Use 'Microsoft.VisualBasic.ChrW' to interpret a numeric value as a Unicode character or first convert it to 'String' to produce a digit.

When i change is to:
chr_bin(i) = CChar(ChrW(Convert.ToInt32(chr_bin(i)) Xor key(i + 256) And 1))

I get this error:
Conversion from string "" to type 'Double' is not valid.

for line 41. see line below
str_hex += Convert.ToInt32(str_bin.Substring(i, 4), 2)
0
 

Author Comment

by:JoachimPetersen
ID: 35047337
oh. forgot to write in the comment above that when I get: Conversion from string "" to type 'Double' is not valid. , is when I run the function.
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 35048169
Hi!

Please check this:

Private Function decrypt(ByVal str_hex As String, ByVal str_key1 As String, ByVal str_key2 As String) As String
        ' 1. Convert hexadecimal string to binary string
        Dim chr_hex() As Char = str_hex.ToCharArray()
        Dim str_bin As String = ""
        For i As Integer = 0 To 31
            Dim temp1 As String = Convert.ToString(Convert.ToInt32(Char.ToString(chr_hex(i)), 16), 2) ' Integer in Binary 
            Do While temp1.Length < 4
                temp1 = "0" & temp1
            Loop
            str_bin += temp1
        Next i
        Dim chr_bin() As Char = str_bin.ToCharArray()

        ' 2. Generate switch and XOR keys
        Dim key1 As Integer = Convert.ToInt32(str_key1)
        Dim key2 As Integer = Convert.ToInt32(str_key2)
        Dim key(383) As Integer
        For i As Integer = 0 To 383
            key1 = (key1 * 11 + 77213) Mod 81371
            key2 = (key2 * 17 + 92717) Mod 192811
            key(i) = (key1 + key2) Mod 128
        Next i

        ' 3. Switch bits positions
        For i As Integer = 256 To 0 Step -1
            Dim temp3 As Char = chr_bin(key(i))
            chr_bin(key(i)) = chr_bin(i Mod 128)
            chr_bin(i Mod 128) = temp3
        Next i

        ' 4. XOR entire binary string
        For i As Integer = 0 To 127
            chr_bin(i) = CChar(ChrW(Convert.ToString(Asc(chr_bin(i)), 2) Xor key(i + 256) And 1))
        Next i

        ' 5. Convert binary string back to hexadecimal
        str_bin = New String(chr_bin)
        str_hex = ""
        For i As Integer = 0 To 127 Step 4
            str_hex += Convert.ToString(Convert.ToInt32(str_bin.Substring(i, 4), 2), 16)
        Next i

        ' 6. Return counted string
        Return str_hex
    End Function

Open in new window

0
 

Author Comment

by:JoachimPetersen
ID: 35048382
When I am running you code, I am getting this error at line 33.
Argument 'CharCode' must be within the range of -32768 to 65535.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 35058281
Hi!

Please change line 33 like this:
chr_bin(i) = CChar(Convert.ToString(Convert.ToString(Asc(chr_bin(i)), 2) Xor Convert.ToString(key(i + 256), 2) And Convert.ToString(1, 2), 10))

Open in new window


What i did is:
Asc(chr_bin(i)) = get integer value
Convert.ToString(Asc(chr_bin(i)), 2)  = get the binary of above (first) integer value
Convert.ToString(key(i + 256), 2) = get the binary of ( second )  integer value
Convert.ToString(1, 2) = get the binary of third operand
Convert.ToString(<first operand> Xor <second operand> And <third operand>, 10) = all in binary and convert bitwise operated binary into decimal equivalent.
CChar(<decimal equivalent>) = return the new character

Is this helpful ???
0
 

Author Comment

by:JoachimPetersen
ID: 35059161
When editing line 33, it does not return any error but the result is not the same as when I run javascript with same parameters:
decrypt("adae225909bb6c445fe27aa919386454", "34233", "51162")

Vb result: ffffffffffffffffffffffffffffffff
Java result: e54017d16ca68e8a9127a38731cd7173
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 35059757
Is this possible for you to check the values for the STEPS mentioned in the code(not all values, but some values). Because when i converted the code from online converter it reported syntax error on line 6 and 33 of the code. I believe there should be no error on line 33 NOW but still line 6 can contain an error because it it was converted and i just sort it down to make it errorless.

So, if you will compare values in steps as mentioned in the code, it would be more easy to trace the cause.
0
 
LVL 19

Accepted Solution

by:
Shahan Ayyub earned 500 total points
ID: 35060762
Hi!

Check this code:
 Private Function decrypt(ByVal str_hex As String, ByVal str_key1 As String, ByVal str_key2 As String) As String
        ' 1. Convert hexadecimal string to binary string
        Dim dic As New Dictionary(Of String, Integer)()
        dic.Add("a", 10) : dic.Add("b", 11) : dic.Add("c", 12) : dic.Add("d", 13) : dic.Add("e", 14) : dic.Add("f", 15)
        Dim chr_hex() As Char = str_hex.ToCharArray()
        Dim str_bin As String = ""
        For i As Integer = 0 To 31
            Dim temp1 As String = String.Empty
            If (dic.ContainsKey(chr_hex(i).ToString)) Then
                temp1 = Convert.ToString(dic(chr_hex(i)), 2)
            Else
                temp1 = Convert.ToString(Val(chr_hex(i)), 2)
            End If
            Do While temp1.Length < 4
                temp1 = "0" & temp1
            Loop
            str_bin += temp1
        Next i
        Dim chr_bin() As Char = str_bin.ToCharArray()

        ' 2. Generate switch and XOR keys
        Dim key1 As Integer = Convert.ToInt32(str_key1)
        Dim key2 As Integer = Convert.ToInt32(str_key2)
        Dim key(383) As Integer
        For i As Integer = 0 To 383
            key1 = (key1 * 11 + 77213) Mod 81371
            key2 = (key2 * 17 + 92717) Mod 192811
            key(i) = (key1 + key2) Mod 128
        Next i

        ' 3. Switch bits positions
        For i As Integer = 256 To 0 Step -1
            Dim temp3 As Char = chr_bin(key(i))
            chr_bin(key(i)) = chr_bin(i Mod 128)
            chr_bin(i Mod 128) = temp3
        Next i

        ' 4. XOR entire binary string
        For i As Integer = 0 To 127
            chr_bin(i) = CChar(Convert.ToString(Convert.ToString(Val(chr_bin(i)), 2) Xor Convert.ToString(key(i + 256), 2) And Convert.ToString(1, 2), 16))
        Next i

        ' 5. Convert binary string back to hexadecimal
        str_bin = New String(chr_bin)
        str_hex = ""
        For i As Integer = 0 To 127 Step 4
            str_hex += Convert.ToString(Convert.ToInt32(str_bin.Substring(i, 4), 2), 16)
        Next i

        ' 6. Return counted string
        Return str_hex
    End Function

Open in new window


For input:
decrypt("adae225909bb6c445fe27aa919386454", "34233", "51162")

It gives me output:
e54017d16ca68e8a9127a38731cd7173
0
 

Author Closing Comment

by:JoachimPetersen
ID: 35061663
God, thank you, and all others who tried,
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now