Class - Range Check error
Jeg har lavet et program til at en/decryptere med. Jeg er så ved at forsøge at ligge funktionerne til at en/decryptere i en class for sig selv i en fil så koden kan genbruges...Men Når jeg så bruger den nye class med funktionerne i et program løber jeg ind i problemer. Hvis jeg en/decryptere mere en 3 bogstaver crasher programmet med en "Range Check Error". Det skal dertil tilføjes at funktionerne virker upåklageligt hvis man kopiere dem og alt tilbehør ind i det nye program.
Hvordan får jeg det til at virke med calss??
Koden lavet i en class:
------------------------------------------
unit lcCrypter;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Math;
Type
Tcrypter = class
public
function encrypt(code, password : string) : string;
function decrypt(code, password : string) : string;
end;
var
codeCount : integer;
counter: integer;
code : string;
const characters : array[1..62] of char = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
implementation
// Limited Char function
function lChar (num : integer) : char;
begin
if num > 0 then
begin
if num > length(characters) then
begin
repeat
num := num - (length(characters));
until num <= length(characters);
result := characters[num];
end
else
begin
result := characters[num];
end;
end
else
begin
if num < 0 then
begin
repeat
num := num + (length(characters));
until num > 0;
result := characters[num];
end
else
begin
result := characters[num];
end;
end;
end;
// Limited Ord Function
function lOrd(chara : char) : integer;
begin
result := pos(chara,Characters) ;
end;
function inArray (chara : char) : boolean;
begin
if pos(chara,Characters) > 0 then
begin
Result := true;
end
else
begin
Result := false;
end;
end;
// PUBLIC EnCrypt function
function Tcrypter.encrypt(code, password : string) : string;
var
i, charNum, codeCount, dnum, codeNum: integer;
currentChar : char;
output : string;
begin
codeCount := 1;
if length(code) > 0 then
if length(password) > 0 then
begin
for i := 1 to length(code) do
begin
charNum := 0;
dNum := 0;
charNum := lord(code[i]);
if inArray(code[i]) then
begin
if codeCount = length(password)-1 then
begin
codeCount := 1;
end
else
begin
codeCount := codeCount+1;
end;
*********
*********
RANGE CHECK ERROR PÅ NÆSTE LINIE
*********
*********
codeNum := 10360+round(sin(1/codeCount)*100)+ord(password[codeCount-1]);
dnum := charNum+codeNum;
output := output + lchar(dnum);
end
else
begin
output := output + code[i];
end;
end;
end;
result := output;
end;
// PUBLIC DeCrypt function
function Tcrypter.decrypt(code, password : string) : string;
var
i, charNum, codeCount, dnum, codeNum: integer;
currentChar : char;
output : string;
begin
codeCount := 1;
if length(code) > 0 then
if length(password) > 0 then
begin
for i := 1 to length(code) do
begin
charNum := 0;
dNum := 0;
charNum := lord(code[i]);
if inArray(code[i]) then
begin
if codeCount = length(password)-1 then
begin
codeCount := 1;
end
else
begin
codeCount := codeCount+1;
end;
*********
*********
RANGE CHECK ERROR PÅ NÆSTE LINIE
*********
*********
codeNum := 10360+round(sin(1/codeCount)*100)+ord(password[codeCount-1]);
dnum := charNum-codeNum;
output := output + lchar(dnum);
end
else
begin
output := output + code[i];
end;
end;
end;
result := output;
end;
end.
--------------------------------------
Koden i det nye program (bare et test)
-----------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, lcCrypter, StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
crypt : Tcrypter;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
crypt := Tcrypter.Create;
form1.Caption := crypt.encrypt('hello world!!!','myPass');
crypt.free;
end;
end.
-------------------------------------------------
Program med fuktionerne integeret
(Det originale program hvor funktionerne er lavet til)
-------------------------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus,Math, shellAPI, Registry;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
Edit3: TEdit;
MainMenu1: TMainMenu;
File1: TMenuItem;
About1: TMenuItem;
codeText: TEdit;
gb1: TGroupBox;
Label4: TLabel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button3: TButton;
EncryptFile1: TMenuItem;
DecryptFile1: TMenuItem;
CreatedByMikkelOlsenwwwmikkelARTdk1: TMenuItem;
Exit1: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure EncryptFile1Click(Sender: TObject);
procedure DecryptFile1Click(Sender: TObject);
procedure CreatedByMikkelOlsenwwwmikkelARTdk1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
codeCount, counter : integer;
code : string;
reg : Tregistry;
const characters : array[1..62] of char = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
implementation
{$R *.dfm}
// Limited Char function
procedure OpenURL(Url: string);
var
ts: string;
begin
with TRegistry.Create do
try
rootkey := HKEY_CLASSES_ROOT;
OpenKey('\htmlfile\shell\open\command', False);
try
ts := ReadString('');
except
ts := '';
end;
CloseKey;
finally
Free;
end;
if ts = '' then Exit;
// remove quotes and commandline parameters
ts := Copy(ts, Pos('"', ts) + 1, Length(ts));
ts := Copy(ts, 1, Pos('"', ts) - 1);
ShellExecute(0, 'open', PChar(ts), PChar(url), nil, SW_SHOW);
end;
function lChar (num : integer) : char;
begin
if num > 0 then
begin
if num > length(characters) then
begin
repeat
num := num - (length(characters));
until num <= length(characters);
result := characters[num];
end
else
begin
result := characters[num];
end;
end
else
begin
if num < 0 then
begin
repeat
num := num + (length(characters));
until num > 0;
result := characters[num];
end
else
begin
result := characters[num];
end;
end;
end;
// Function End
// Limited Ord Function
function lOrd(chara : char) : integer;
begin
result := pos(chara,Characters) ;
end;
function inArray (chara : char) : boolean;
begin
if pos(chara,Characters) > 0 then
begin
Result := true;
end
else
begin
Result := false;
end;
end;
function encrypt(code, password : string) : string;
var
i, charNum, codeCount, dnum, codeNum: integer;
currentChar : char;
output : string;
begin
codeCount := 1;
if length(code) > 0 then
if length(password) > 0 then
begin
for i := 1 to length(code) do
begin
charNum := 0;
dNum := 0;
charNum := lord(code[i]);
if inArray(code[i]) then
begin
if codeCount = length(password)-1 then
begin
codeCount := 1;
end
else
begin
codeCount := codeCount+1;
end;
codeNum := 10360+round(sin(1/codeCount)*100)+ord(password[codeCount-1]);
dnum := charNum+codeNum;
output := output + lchar(dnum);
end
else
begin
output := output + code[i];
end;
end;
end;
result := output;
end;
function decrypt(code, password : string) : string;
var
i, charNum, codeCount, dnum, codeNum: integer;
currentChar : char;
output : string;
begin
codeCount := 1;
if length(code) > 0 then
if length(password) > 0 then
begin
for i := 1 to length(code) do
begin
charNum := 0;
dNum := 0;
charNum := lord(code[i]);
if inArray(code[i]) then
begin
if codeCount = length(password)-1 then
begin
codeCount := 1;
end
else
begin
codeCount := codeCount+1;
end;
codeNum := 10360+round(sin(1/codeCount)*100)+ord(password[codeCount-1]);
dnum := charNum-codeNum;
output := output + lchar(dnum);
end
else
begin
output := output + code[i];
end;
end;
end;
result := output;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Form1.Edit3.Text := '';
edit3.text := encrypt(edit1.text,codeText.text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Edit3.Text := '';
edit3.text := decrypt(edit2.text,codeText.text);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Form1.Close;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Form1.Close;
end;
procedure TForm1.EncryptFile1Click(Sender: TObject);
begin
edit3.text := encrypt(edit1.text,codeText.text);
end;
procedure TForm1.DecryptFile1Click(Sender: TObject);
begin
edit3.text := decrypt(edit2.text,codeText.text);
end;
procedure TForm1.CreatedByMikkelOlsenwwwmikkelARTdk1Click(Sender: TObject);
begin
OpenURL('http://www.----------.dk');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
form1.Caption := '------- - Limit Encrypter';
reg := Tregistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKey('SOFTWARE\213\ccEnc_stV\',false);
if reg.ValueExists('code') = true then
begin
codeText.Text := decrypt(reg.ReadString('code'),'56e1b4ff6aec1699');
end
else
begin
codeText.Text := 'myPersonalCode';
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
reg := Tregistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
reg.OpenKey('SOFTWARE\213\ccEnc_stV\',true);
if codeText.Text <> '' then
begin
reg.WriteString('code',encrypt(codeText.Text,'56e1b4ff6aec1699'));
end;
reg.CloseKey;
finally
reg.Free;
end;
end;
end.