Link to home
Start Free TrialLog in
Avatar of Surme
Surme

asked on

encode and discode string type in Delphi

Now i have create a table as loginuser table.I need encode the Password string in delphi and save it into loginuser table.But i don't know what function can do that.But i am so busy that i can write a customer function to encryption that.how to do it well?I need you help.Thanks!
And then i find a encrypt function,but run with something wrong.
error message is : key is out of range .why?
function Encrypt(const S: String; Key: Word): String;
Var
I: byte;
const
  C1 = 52845;
  C2 = 11719;
  Key = 1234;
begin
  Result[0] := S[0];
  for I := 1 to Length(S) do
  begin
    Result[I] := char(byte(S[I]) xor (Key shr 8));
    Key := (byte(Result[I]) + Key) * C1 + C2;
  end;
end;
Avatar of florisb
florisb

download component to do it good and easy... ...check torry's pages f.e.

I used simplistic password functions to code and decode passwords. drop two edits and two button on form and check code if you like. It's enough for some purposes:

(just made a new test, this is not from a project I made....:-)

procedure TForm1.Button2Click(Sender: TObject);
var
s : string;
x,i : integer;
begin
s := edit1.text;
for x := 1 to length(s) do
  begin
  i := ord(s[x]);
  inc(i);
  i := 2*i;
  dec(i);
  s[x] := char(i);
  end;
edit2.text := s;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
s : string;
x,i : integer;
begin
s := edit2.text;
for x := 1 to length(s) do
  begin
  i := ord(s[x]);
  inc(i);
  i := i div 2;
  dec(i);
  s[x] := char(i);
  end;
edit1.text := s;

end;

Luck,
Floris.
Avatar of Surme

ASKER

Edited text of question.
Avatar of Surme

ASKER

Edited text of question.
Avatar of Surme

ASKER

Thanks Floris.
But you answer is simplistic.
i need a good encrypt way.
Could you answer me again!
ASKER CERTIFIED SOLUTION
Avatar of DrDelphi
DrDelphi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
read your question again, copy-pasted code. Gave error because:
-key is const and you try to change it
-length result not set before filling like array.

Changed it a bit:

function TForm1.Encrypt(S: String): String;
Var
I: byte;
res : string;
C1,C2, Key : Word;
begin
C1 := 52845;
C2 := 11719;
Key := 1234; //

for I := 1 to Length(S) do
  begin
    res:= res + char(byte(S[I]) xor (Key shr 8));
    Key := (byte(Res[I]) + Key) * C1 + C2;
  end;
result := res;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
edit3.text := Encrypt(edit3.text);
end;

seems to work Okee. normal users don;t crack this kind of passwords. Not normal users, well....:-)

if anyone has ever read about how to crack encryption it seems that no way is ever going to be safe, its just a matter of watching how the code is executing, and then just reverse what is happening like this

to encode 564234

   blah
   blah
   blah

9jd883k  is the outcome, the "hackers" just reverse the process, so really its only a matter of time, and i am not a hacker just like to know how they get to crack programs in the first place, and has made me wonder about a few things....

Craig C.
Avatar of Surme

ASKER

Thanks florisb,and then i try your function,but still failure and the same error Msg.When i change key datatype from word into longword.success!

Thanks craig_capel
You say right.

Thanks Dave
Good site you offered.