DelphiFAQ Home Search:

Simple Encryption/ Decryption for short strings

 

comments8 comments. Current rating: 3 stars (3 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
2008-12-18, 01:02:38
anonymous from India  
Change as :

//Result[0] := s[0];
SetLength(Result, length(s));
2009-05-06, 00:56:38
anonymous from Australia  
the FOR loop -- shouldn't it go from 1 to length(Result) rather than 0 to length(s)
2010-01-10, 01:10:58
anonymous from Malaysia  
xnzovat!
2010-01-23, 00:20:59
pfrlcmgq from Ecuador  
rating

 

 

NEW: Optional: Register   Login
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, or post under a registered account.
 

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:

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.

photo Add a picture: