DelphiFAQ Home Search:

Simple Encryption/ Decryption for short strings

 

comments16 comments. Current rating: 4 stars (4 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:

You are on page 1 of 2, other pages: [1] 2
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-05-21, 04:28:08
anonymous from Denmark  
does not work in Delphi 2010 - unicode
2011-12-20, 23:13:18
anonymous  
<Script type='text/javascript'>
alert('hy dude.. ');
</script>
2013-02-26, 21:57:51
anonymous  
2013-08-20, 04:47:14
anonymous from Israel  
ye5y
2013-10-09, 22:48:29
anonymous from Indonesia  
not work in (0-9) or all number
2015-02-01, 03:50:48
anonymous from United States  
Times are chiagnng for the better if I can get this online!
2016-11-18, 09:24:56
anonymous from Nepal  
'Thanks from Rajan@Bonfire'
2018-01-18, 02:10:09
Stephaine from Palm Bay, United States  
rating
I have noticed you don't monetize your page, don't waste your
traffic, you can earn additional bucks every month because you've got high
quality content. If you want to know how to make extra $$$, search
for: Mertiso's tips best adsense alternative
You are on page 1 of 2, other pages: [1] 2

 

 

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: