Algorithm Math Delphi

Title: Text Encryption Implementation
Question: These simple routines encrypt and decrypt text and return it as a set of ascii (printable) text characters. The only requirement is that the input characters must be a member of the Const CHARSET.
Answer:
{-----------------------------------------------------------------------------
Unit Name: unitBasicEncrypt
Documentation Date: 19/02/02 23:48:39
Version: 1.1
Compiler directives:
**$DEFINE TINY
removes unessacary strings. output will be empty or incomplete
on errors
Purpose:
--- Description: --
Encrypts / decryptsthe String with a key integer. Input characters must be in
Charset and output Result will only contain CharSet
Notes:
Dependancies:
History:
27-Feb-2002 01:35 : Modified and optimized.
Letters is now called Charset.
Speed has been improved (badly programmed!)
Copyright 2001 by Stewart Moss
All rights reserved.
-----------------------------------------------------------------------------}
unit unitBasicEncrypt;
interface
uses
Classes, Sysutils;
function BasicEncrypt(msg: string; key: integer): string;
function basicDecrypt(msg: string; key: integer): string;
implementation
const
Charset: string =
' .,/?;:''"~ABCDEFGHIJKLMNOP-=_+][{}QRSTUVWXYZ0987654321abcdefghij`!@#$%^&*()klmnopqrstuvwxyz';
{-----------------------------------------------------------------------------
Procedure: BasicEncrypt
Arguments: msg: string; key: Integer
Result: string
Description:
encrypts the msg with key. input characters must be in Charset and
output Result will only contain CharSet
Copyright 2001 by Stewart Moss
All rights reserved.
-----------------------------------------------------------------------------}
function BasicEncrypt(msg: string; key: Integer): string;
var
Loop,
encPos: Integer;
begin
// Basic Encrypt
result := '';
for Loop := 1 to length(msg) do
begin
encPos := pos(msg[loop], Charset);
if encPos = 0 then
begin
result := '';
{$IFNDEF TINY}
raise Exception.Create('Password has invalid characters' + msg[Loop]);
{$ENDIF}
{$IFDEF TINY}
exit;
{$ENDIF}
end;
EncPos := EncPos + key;
if EncPos Length(Charset) then EncPos := EncPos - Length(Charset);
if Encpos 0 then EncPos := EncPos + Length(Charset);
result := Result + Charset[encPos];
end;
end;
{-----------------------------------------------------------------------------
Procedure: BasicEncrypt
Arguments: msg: string; key: Integer
Result: string
Description:
encrypts the msg with key. input characters must be in Charset and
output Result will only contain CharSet
Copyright 2001 by Stewart Moss
All rights reserved.
-----------------------------------------------------------------------------}
function BasicDecrypt(msg: string; key: integer): string;
var
Loop,
encPos: Integer;
begin
// Basic Decrypt
result := '';
for Loop := 1 to length(msg) do
begin
encPos := Pos(msg[loop],Charset);
if encPos = 0 then
begin
result := '';
{$IFNDEF TINY}
raise Exception.Create('Password has invalid characters' + msg[Loop]);
{$ENDIF}
{$IFDEF TINY}
exit;
{$ENDIF}
end;
EncPos := EncPos - key;
if EncPos Length(Charset) then EncPos := EncPos - Length(Charset);
if Encpos 0 then EncPos := EncPos + Length(Charset);
result := Result + Charset[encPos];
end;
end;
end.