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;
SurmeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

florisbCommented:
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.
0
SurmeAuthor Commented:
Edited text of question.
0
SurmeAuthor Commented:
Edited text of question.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

SurmeAuthor Commented:
Thanks Floris.
But you answer is simplistic.
i need a good encrypt way.
Could you answer me again!
0
DrDelphiCommented:
Surme,
   You are not going to want to use an alogorithm (no matter how good) that is posted here, solely by virtue of the fact that half the world will know how to hack your application. I can suggest a very good site for encryption algorithms, however. Take a look at my buddy Earl's page:

http://www.efg2.com/lab/library/Delphi/MathFunctions/Cryptography.htm


There are several routines there for you to choose one that you feel comfortable with and noboday has to know which one you went with (if any).


Good luck!

-Dave
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
florisbCommented:
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....:-)

0
craig_capelCommented:
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.
0
SurmeAuthor Commented:
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.

   
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.