DelphiFAQ Home Search:

Simple Encryption/ Decryption for short strings

 

comments4 comments. Current rating: 5 stars (2 votes). Leave comments and/ or rate it.

The following routines Encrypt(), Decrypt() are suitable for passwords and similar situations.

const
  c1 = 52845;
  c2 = 22719;

function Encrypt (const s: string; Key: Word) : string;
var
  i : byte;
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;


function Decrypt (const s: string; Key: Word) : string;
var
  i : byte;
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 (s[i]) + Key) * c1 + c2 
    end 
end; 


Comments:

2006-06-21, 20:26:06
allan.ford@santos.com from Australia  
rating
I had to do a monor modification as per below:

const
c1 = 52845;
c2 = 22719;

function Encrypt (const s: string; Key: Word) : string;
var
i : byte;
ResultStr : string;
begin
Result:=s;
{Result[0] := s[0]; }
for i := 0 to (length (s)) do
begin
Result[i] := Char (byte (s[i]) xor (Key shr 8));
Key := (byte (Result[i]) + Key) * c1 + c2
end
end;

function Decrypt (const s: string; Key: Word) : string;
var
i : byte;
begin
{Result[0] := s[0];}
Result:=s;
for i := 0 to (length (s)) do
begin
Result[i] := Char (byte (s[i]) xor (Key shr 8));
Key := (byte (s[i]) + Key) * c1 + c2
end
end;
2006-10-05, 08:33:05
anonymous from United Kingdom  
rating
2008-01-08, 05:08:45
anonymous  
Thanks Allan this was useful !
2008-06-06, 07:17:28
anonymous from Sri Lanka  
Thanks This Was Useful

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.