I have a problem by converting the following piece of Delphi code to Java. The code I posted in here is just a small part of my full code that I took to describe my problem.
type
TSecureArray = Array of AnsiChar;
const
CodePosIdx = 10;
function ReadLenFromArray(aArray:TSecureArray):integer;
var
HS:integer;
begin
Hs:=0;
HS:=Hs+(ord(aArray[3]))*$1000000;
HS:=Hs+(ord(aArray[4]))*$10000;
HS:=Hs+(ord(aArray[5]))*$100;
HS:=Hs+(ord(aArray[6]));
result:=HS;
end;
function Decrypt(Source: Ansistring): Ansistring;
var
srclen, aArrayLength: integer;
aArray: TSecureArray;
cryptedstring:AnsiString;
begin
aArrayLength:=Length(Source); // length of source-String in my test case is 1046
for i:=1 to aArrayLength do
aArray[i]:=Source[i];
srclen:=ReadLenFromArray(aArray); // function returns 858862149 in my test case
cryptedString:='';
for i:=1 to srclen do
cryptedstring:=cryptedstring+aArray[aArraylength-srclen-ord(aArray[CodePosIdx])+i];
end;
in Java I've implemented this piece of Delphi code as:
protected static int readLenFromArray(char secureArray[])
{
int arrayLength = secureArray.length;
int hs = 0;
if(2<arrayLength)
{
hs = hs + (int) secureArray[2] * 0x1000000;
}
if(3<arrayLength)
{
hs = hs + (int) secureArray[3] * 0x10000;
}
if(4<arrayLength)
{
hs = hs + (int) secureArray[4] * 0x100;
}
if(5<arrayLength)
{
hs = hs + (int) secureArray[5];
}
return hs;
}
protected static String deCrypt(String code)
{
int codePosIdx = 10;
char [] secureArray;
int srcLen;
int arrayLength;
arrayLength = source.length(); // 1046 in my test case
for (i=0; i<arrayLength; i++)
{
secureArray[i] = source.charAt(i);
}
srcLen = readLenFromArray(secureArray); // function returns 858862149 in my test case
StringBuilder tmpStr = new StringBuilder();
for(i=0; i<srcLen; i++)
{
tmpStr = tmpStr.append(secureArray[arrayLength - srcLen - 1 - (int) secureArray[codePosIdx - 1] + i]);
}
cryptCode = tmpStr.toString();
return cryptCode;
}
My problem is checking of condition in the for-Loop in Java.
In my testcase aArray[i + (int) secureArray[codePosIdx - 1]] is equal to aArray[-858861172] and in contrast to Delphi it causes an IndexOutOfBound Exception. I avoided it in Java readLineFromArray function through if-statement. But how could I solve this problem in my decrypt function, so that the functionality of function is preserved for all values of parameters.
Any advice truly approciated!
TSecureArraytype. Same u do forsecureArrayin Java. Also tell your Delphi version. I suspect you stumbled uponchar/PChar/Stringambiguity. In some Delphi versions they are mapped to MBCS typesAnsiChar/PAnsiChar/AnsiStringbut in other Delphi versions they are mapped to UTF-16 typesWideChar/PWideChar/UnicodeStringlike text data probably is in Java. Try to output and comparesizeof(aArray[1])in both Delphi and Java when you deal with unsafe bits-jockeying likeOrd/(int)CharwasAnsiCharandstringwasAnsiString. To use the Delphi code as a model for the Java code will lead to all kind of troubles along the way. Look for a specification of the functionality and code it in Java without even looking at the Delphi code.ReadLenFronArray()(858862149) reasonable? Anyway, as Johan wrote already, move away from string and char types. Cryptography does not operate on those types. BTW, of curiosity, is the Delphi code available on the net?