1

It was not me who wrote this code, it was the previous programmer. However, I noticed he didn't provide a decryption algorithm, rendering the encryption useless.

How can I decrypt this?

function Encrypt(jstr: String): String;
var
  I: Integer;
  A: Real;
begin
  if Length(jstr) = 0 Then begin
    Result := '';
    Exit;
  end;
  A := 0;
    for I := 0 To Length(jstr) do
      A := A + (Ord(jstr[I]) * Pos(jstr[I],jstr)) / 33;
  Result := FormatFloat('0000000000.0000000000',A);
  if Pos(',',Result) > 0 then begin
    Insert('.',Result,Pos(',',Result));
    Delete(Result,Pos(',',Result),1);
  end;
end;

Thanks!

Johan
  • 74,508
  • 24
  • 191
  • 319
John Rosenberg
  • 607
  • 3
  • 11
  • 18
  • 2
    As a hint: You don't need to put every line into . Just add 4 spaces in front of each line and you'll have the highlighted code formatting – Tim Meyer Nov 16 '11 at 07:41
  • Good luck it is not encryption without key. – Luka Rahne Nov 16 '11 at 07:45
  • 1
    @ralu that's not the issue, the entire code for the so called encryption is in the Q, there is no key – David Heffernan Nov 16 '11 at 07:46
  • It appears that the comma replacement is to force the decimal character to a period. If you're still using this function, I would make use of the DecimalSeparator global variable in the replacement portion instead of coding in the comma. – Marcus Adams Nov 16 '11 at 14:20
  • 1
    "Floating point encryption techniques" might even create different output on different CPUs, thanks possible differences in rounding errors in their floating point engines. This is a crappy way to write a hash. I wouldn't reuse this code, ever. – Warren P Nov 16 '11 at 21:43
  • It won't even work in modern Delphis, since it uses [0] index of a string, which is only defined in ShortStrings in newer Delphi versions, unless {$H-} is used. – HeartWare Feb 15 '13 at 14:47

2 Answers2

20

It looks like a one way hash and hence is not reversible. For example, is the string is very big the result is still a string representation of a float.

Steve
  • 1,769
  • 2
  • 22
  • 33
16

That function cannot be reversed. Since it takes input of arbitrary length and returns output of finite length, simple information theory tells you the futility of attempting to write a general inverse. Even for shorter input strings it seems to me that different input strings can result in the same encrypted string.

Even as a hash this function seems very brittle to me due to the bizarre use of floating point code. If I were you I would replace this function with something more fit for purpose.

Finally, I recommend that you undertake a review of all code produced by this developer. The low quality of this code and algorithm suggests to me that everything that this developer touched is liable to have defects.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490