Convert a VB6 program into Java

Hello everyone,

I have this small VB6 function needed to be converted properly into Java

This is the VB6 function

Public Function Crypt(ByVal Text As String) As String

Dim X As Integer

For X = 1 To Len(Text)

Crypt = Crypt & Chr(Asc(Mid$(Text, Len(Text) - X + 1, 1)) Xor &HA5)

Next X

End Function

It is just a function to invert and encrypt a string. And if you run Crypt(Crypt("10108"), it will give you "10108" back (because of the Xor operator)

My java equivalent is

publicstatic String crypt(String input)

{

int i = 0;

int len = input.length();

int a5 = 0xa5;// variable used in original VB code for encoding

String cryptOutput =new String("");

for (i = 0; i < input.length(); i++)

{

cryptOutput = cryptOutput + ((char)(((byte) input.charAt(len - i - 1)) ^ ((byte)a5)));

}

return cryptOutput;

}

My java function also work in a sense that crypt(crypt("10108") also gives back "10108", but the my crypt("10108") output is different from the VB6 code Crypt("10108"). And it is important for them to be the same... I've been trying without success in finding out why...

Could someone point out to my why there is a difference? Thank you in advance :)

Could someone

[1859 byte] By [BuggyVBa] at [2007-11-27 6:08:17]
# 1

Does this work for you?

public static String crypt(String input)

{

StringBuffer buf = new StringBuffer();

int len = input.length();

int mask = 0xa5; // variable used in original VB code for encoding

for (int i = 0; i < len; i++)

{

buf.append((char) (input.charAt(len - i - 1) ^ mask));

}

return buf.toString();

}

quittea at 2007-7-12 17:10:26 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for the reply quitte. But no, it still the same :(.... crypt("10108") in java returns different result to Crypt("10108") in VB6. Does it work for you?

And I'm having even stranger problem:

Say

Dim text As String

Text = Chr(253) & Chr(253) & Chr(1) & Chr(2) & Chr(Len("10108") / 256) & Chr(Len("10108") Mod 256) & "10108" & Chr(254) & Chr(254)

is different my java equivalent on printed on screen

String msg = new String("");

msg = msg + ((char)253) + ((char)253) + ((char)packetType) + ((char)packetSubType) + ((char)(packet.length() / 256)) + ((char)(packet.length() % 256)) + packet + ((char)254) + ((char)254);

The VB printout only contains 3 characters (even though there is a ...& "10108" & ....

part). Java printout is longer and I can see part of the original message.

I dont know what is going on here... Never that good with bits and bytes :(

BuggyVBa at 2007-7-12 17:10:26 > top of Java-index,Java Essentials,Java Programming...
# 3

> Does it work for you?

Yes, if I got your algorithm right:

public class Xxx {

public static void main(String[] args) {

String org = args[0];

String enc = crypt(org);

print(org);

print(enc);

}

private static void print(String str) {

System.out.println("");

for (int i = 0, len = str.length();i < len;i++) {

System.out.println(((int) str.charAt(i)) + ":" + Integer.toBinaryString(str.charAt(i)));

}

System.out.println("");

}

public static String crypt(String input)

{

StringBuffer buf = new StringBuffer();

int len = input.length();

int mask = 0x00a5;

for (int i = 0; i < len; i++)

{

buf.append((char) (input.charAt(len - i - 1) ^ mask));

}

return buf.toString();

}

}

Test run:

java Xxx hurz

Result:

104:1101000

117:1110101

114:1110010

122:1111010

223:11011111

215:11010111

208:11010000

205:11001101

What's the output of your VB code?

quittea at 2007-7-12 17:10:26 > top of Java-index,Java Essentials,Java Programming...
# 4

Probably you shouldn't cast to byte, but do this instead:cryptOutput = cryptOutput + (char) (input.charAt(len - i - 1) ^ a5);

This will likely not behave like the original with non-ASCII characters such as € or ?because Chr and Asc use one of the

Windows-character encodings, but the output should still be the same for ASCII characters.

Edit: ugh long lines

jsalonena at 2007-7-12 17:10:26 > top of Java-index,Java Essentials,Java Programming...