Here is a nice challenge for someone:
I have this delphi unit source file that I cannot get converted in any fashion to PHP.
I first of all don't even know Delphi/Pascal and have a very hard time understanding any of this.
Basically, we are encrypting a URL. Here is the code and below the code is some information I know about some of the stuff in here:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
unit Encrypt;
interface
function EncryptText(URL: string): string;
function DecryptText(URL: string): string;
implementation
uses Math, SysUtils, Base64;
var
P, Q, N, PHI, E, D: Integer;
function GCD(e,PHI: Integer): Integer;
var
a: Integer;
begin
if e > PHI then
begin
while (e mod PHI) <> 0 do
begin
a:= e mod PHI;
e:= PHI;
PHI:= a;
end;
Result:= PHI;
end
else
begin
while (PHI mod e) <> 0 do
begin
a:= PHI mod e;
PHI:= e;
e:= a;
end;
Result:= e;
end;
end;
function tofindE(PHI,P,Q: Integer): Integer;
var
great, e: Integer;
begin
great:= 0;
e:= 2;
while great <> 1 do
begin
e:= e + 1;
great:= GCD(e,PHI);
PHI:= (P - 1) * (Q - 1);
end;
result:= e;
end;
function extend(E,PHI: Integer): Integer;
var
u1, u2, u3, v1, v2, v3, t1, t2, t3: Integer;
begin
u1:= 1;
u2:= 0;
u3:= PHI;
v1:= 0;
v2:= 1;
v3:= E;
while v3 <> 0 do
begin
q:= Floor(u3/v3);
t1:= u1 - q v1;
t2:= u2 - q v2;
t3:= u3 - q * v3;
u1:= v1;
u2:= v2;
u3:= v3;
v1:= t1;
v2:= t2;
v3:= t3;
end;
if u2 < 0 then Result:= u2 + PHI
else Result:= u2;
end;
function EncryptText(URL: string): string;
var
I: Integer;
S: string;
begin
Result:= '';
P:= 17;
Q:= 11;
N:= PQ;
PHI:= (P-1)(Q-1);
E:= tofindE(PHI,P,Q);
D:= extend(E,PHI);
SetLength(S, Length(URL));
for I:= 1 to Length(URL) do
begin
S:= Chr(Trunc(Power(Ord(URL), E)) mod N);
end;
Result:= B64Encode(S);
end;
function DecryptText(URL: string): string;
var
S, S2: string;
I, A : Integer;
G, F : Integer;
C : Integer;
begin
Result:= '';
S:= B64Decode(URL);
SetLength(S2, Length(S));
for A:= 1 to Length(S) do
begin
C:= Ord(S[A]);
if (D mod 2) = 0 then
begin
G:= 1;
for I:= 1 to D div 2 do
begin
F:= (CC) mod N;
G:= (FG) mod N;
end
end
else
begin
G:= C;
for I:= 1 to D div 2 do
begin
F:= (CC) mod N;
G:= (FG) mod N;
end;
end;
S2[A]:= Chr(G);
end;
Result:= S2;
end;
end.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Here is some stuff I already know about this:
the mod keyword is:
A-(A/😎*B and is always an absolute value.
if B is 0, then an error returns, thus the reason for <>0 above in the script.
Chad.