Entwickler-Ecke

Algorithmen, Optimierung und Assembler - Probleme mit "return" in der funktion (glaube ich)


Wii360 - Di 28.04.09 17:38
Titel: Probleme mit "return" in der funktion (glaube ich)
Hallo liebe Profis!
Also ich bekomme immer wenn ich versuche die funktion mit buchstaben(bei zahlen gehts) als passwort zu auszuführen immer den fehler:
"Invailed Pointer Oparation"

Diese funktion meine ich:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
function  TForm1.EntcryptKey(KeyFile:TStringList; Password:String):TStringList;
  var
  PWDbuf:array of integer;
  i,i2,i3:integer;
  Letter:array of integer;
  LetterCount:integer;
  Buf:String;
  s:string;
begin
i3:=0;
LetterCount:=0;
SetLength(PWDbuf,length(Password));
//Passwort in Zahlen umwandeln
Password:=CharLower(PAnsiChar(Password));
for i:=1 to length(Password) do
  begin
  for i2:=1 to 40 do
    begin
    if Password[i]=Alp[i2] then
      PWDbuf[i]:=i2;
    end;
  end;
//SecurKey entschlüsseln
for i:=0 to KeyFile.Count-1 do
  begin
  for i2:=1 to length(KeyFile[i]) do
    begin
    if KeyFile[i][i2]='"' then
      begin
      LetterCount:=LetterCount+1;
      SetLength(letter,LetterCount);
      Letter[LetterCount-1]:=strtoint(s);
      s:='';
      end
    else
      s:=s+KeyFile[i][i2];
    end;

  for i2:=0 to LetterCount-1 do
    begin
    i3:=i3+1;
    if i3>length(Password)-1 then
      i3:=1;
    Buf:=Buf+chr(Letter[i2]-PWDbuf[i3]-i3);
    end;
  KeyFile[i]:=Buf;
  Buf:='';
  SetLength(Letter,0);
  LetterCount:=0;
  end;
result:=KeyFile;
end;



Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
Alp[1]:='a';
Alp[2]:='b';
Alp[3]:='c';
Alp[4]:='d';
Alp[5]:='e';
Alp[6]:='f';
Alp[7]:='g';
Alp[8]:='h';
Alp[9]:='i';
Alp[10]:='j';
Alp[11]:='k';
Alp[12]:='l';
Alp[13]:='m';
Alp[14]:='n';
Alp[15]:='o';
Alp[16]:='p';
Alp[17]:='q';
Alp[18]:='r';
Alp[19]:='s';
Alp[20]:='t';
Alp[21]:='u';
Alp[22]:='v';
Alp[23]:='w';
Alp[24]:='x';
Alp[25]:='y';
Alp[26]:='z';
Alp[27]:='ä';
Alp[28]:='ö';
Alp[29]:='ü';
Alp[30]:='_';
Alp[31]:='0';
Alp[32]:='1';
Alp[33]:='2';
Alp[34]:='3';
Alp[35]:='4';
Alp[36]:='5';
Alp[37]:='6';
Alp[38]:='7';
Alp[39]:='8';
Alp[40]:='9';
Alp[41]:='.';
Alp[42]:=':';
Alp[43]:=';';


Und Paradoxerweise gehts bei crypt nur mit buchtaben dort kommt der fehler "[...] EAccessViolation [...]"

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
function  TForm1.CryptKey(KeyFile:TStringList; Password:String):TStringList;
var
  PWDbuf:array of integer;
  i,i2,i3:integer;
  Letter:char;
  Buf,Buf2:String;
  b:boolean;
begin
i3:=0;
b:=false;
KeyFile.Add(Password);
SetLength(PWDbuf,length(Password));
//Passwort in Zahlen umwandeln
Password:=CharLower(PAnsiChar(Password));
for i:=1 to length(Password) do
  begin
  for i2:=1 to 40 do
    begin
    if Password[i]=Alp[i2] then
      begin
      PWDbuf[i]:=i2;
      b:=true
      end;
    end;
  if b=false then
    begin
    MessageDlg('Bitte gib ein gültiges Passwort ein! Dieses darf keine sonderzeichen außer "_" enthalten.',mtError,[mbok],0);
    abort;
    end;
  b:=false;
  end;
//20.000 Zeichen langer Schlüssel erstellen
for i:=1 to 400 do
  begin
  for i2:=1 to 40 do
    begin
    randomize;
    Buf2:=Buf2+inttostr(random(1000)+i div 3+i2 div 5)+'"';
    end;
  KeyFile.Add(Buf2);  
  Buf2:='';
  end;
//SecurKey verschlüsseln
for i:=0 to KeyFile.Count-1 do
  begin
  for i2:=1 to Length(KeyFile[i]) do
    begin
    i3:=i3+1;
    if i3>length(Password)-1 then
      i3:=1;
    Letter:=KeyFile[i][i2];
    Buf:=Buf+inttostr(ord(Letter)+PWDbuf[i3]+i3)+'"';
    end;
  KeyFile[i]:=Buf;
  Buf:='';
  end;
result:=KeyFile;
end;


Achso! Hier der komplette quelltext:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    function  EntcryptKey(KeyFile:TStringList; Password:String):TStringList;
    function  CryptKey(KeyFile:TStringList; Password:String):TStringList;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Alp:array[1..43]of char;

implementation

{$R *.dfm}

function  TForm1.CryptKey(KeyFile:TStringList; Password:String):TStringList;
var
  PWDbuf:array of integer;
  i,i2,i3:integer;
  Letter:char;
  Buf,Buf2:String;
  b:boolean;
begin
i3:=0;
b:=false;
KeyFile.Add(Password);
SetLength(PWDbuf,length(Password));
//Passwort in Zahlen umwandeln
Password:=CharLower(PAnsiChar(Password));
for i:=1 to length(Password) do
  begin
  for i2:=1 to 40 do
    begin
    if Password[i]=Alp[i2] then
      begin
      PWDbuf[i]:=i2;
      b:=true
      end;
    end;
  if b=false then
    begin
    MessageDlg('Bitte gib ein gültiges Passwort ein! Dieses darf keine sonderzeichen außer "_" enthalten.',mtError,[mbok],0);
    abort;
    end;
  b:=false;
  end;
//20.000 Zeichen langer Schlüssel erstellen
for i:=1 to 400 do
  begin
  for i2:=1 to 40 do
    begin
    randomize;
    Buf2:=Buf2+inttostr(random(1000)+i div 3+i2 div 5)+'"';
    end;
  KeyFile.Add(Buf2);  
  Buf2:='';
  end;
//SecurKey verschlüsseln
for i:=0 to KeyFile.Count-1 do
  begin
  for i2:=1 to Length(KeyFile[i]) do
    begin
    i3:=i3+1;
    if i3>length(Password)-1 then
      i3:=1;
    Letter:=KeyFile[i][i2];
    Buf:=Buf+inttostr(ord(Letter)+PWDbuf[i3]+i3)+'"';
    end;
  KeyFile[i]:=Buf;
  Buf:='';
  end;
result:=KeyFile;
end;

function  TForm1.EntcryptKey(KeyFile:TStringList; Password:String):TStringList;
  var
  PWDbuf:array of integer;
  i,i2,i3:integer;
  Letter:array of integer;
  LetterCount:integer;
  Buf:String;
  s:string;
begin
i3:=0;
LetterCount:=0;
SetLength(PWDbuf,length(Password));
//Passwort in Zahlen umwandeln
Password:=CharLower(PAnsiChar(Password));
for i:=1 to length(Password) do
  begin
  for i2:=1 to 40 do
    begin
    if Password[i]=Alp[i2] then
      PWDbuf[i]:=i2;
    end;
  end;
//SecurKey entschlüsseln
for i:=0 to KeyFile.Count-1 do
  begin
  for i2:=1 to length(KeyFile[i]) do
    begin
    if KeyFile[i][i2]='"' then
      begin
      LetterCount:=LetterCount+1;
      SetLength(letter,LetterCount);
      Letter[LetterCount-1]:=strtoint(s);
      s:='';
      end
    else
      s:=s+KeyFile[i][i2];
    end;

  for i2:=0 to LetterCount-1 do
    begin
    i3:=i3+1;
    if i3>length(Password)-1 then
      i3:=1;
    Buf:=Buf+chr(Letter[i2]-PWDbuf[i3]-i3);
    end;
  KeyFile[i]:=Buf;
  Buf:='';
  SetLength(Letter,0);
  LetterCount:=0;
  end;
result:=KeyFile;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Alp[1]:='a';
Alp[2]:='b';
Alp[3]:='c';
Alp[4]:='d';
Alp[5]:='e';
Alp[6]:='f';
Alp[7]:='g';
Alp[8]:='h';
Alp[9]:='i';
Alp[10]:='j';
Alp[11]:='k';
Alp[12]:='l';
Alp[13]:='m';
Alp[14]:='n';
Alp[15]:='o';
Alp[16]:='p';
Alp[17]:='q';
Alp[18]:='r';
Alp[19]:='s';
Alp[20]:='t';
Alp[21]:='u';
Alp[22]:='v';
Alp[23]:='w';
Alp[24]:='x';
Alp[25]:='y';
Alp[26]:='z';
Alp[27]:='ä';
Alp[28]:='ö';
Alp[29]:='ü';
Alp[30]:='_';
Alp[31]:='0';
Alp[32]:='1';
Alp[33]:='2';
Alp[34]:='3';
Alp[35]:='4';
Alp[36]:='5';
Alp[37]:='6';
Alp[38]:='7';
Alp[39]:='8';
Alp[40]:='9';
Alp[41]:='.';
Alp[42]:=':';
Alp[43]:=';';
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  a:TStringList;
begin
a:=TStringList.Create;
a.LoadFromFile('secur.key');
a:=EntcryptKey(a,InputBox('PasswortEingabe','Bitte geben sie das gewünschte Passwort ein',''));
a.SaveToFile('_secur.key');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  a:tstringlist;
begin
a:=TStringList.Create;
a:=CryptKey(a,InputBox('PasswortEingabe','Bitte geben sie das gewünschte Passwort ein',''));
a.SaveToFile('secur.key');
end;

end.


Regan - Di 28.04.09 18:33

user profile iconWii360 hat folgendes geschrieben Zum zitierten Posting springen:
Hallo liebe Profis!

Hallo!

ich habe mal die wichtigsten Stellen behalten:
user profile iconWii360 hat folgendes geschrieben Zum zitierten Posting springen:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var
  PWDbuf:array of integer;//Ist ein dynamisches Array
{...}
SetLength(PWDbuf,length(Password));//Die Länge des dynamischen Arrays wird gesetzt

for i:=1 to length(Password) do //Zählerschleife ist auf 1, das dynamische Array beginnt aber bei 0
  begin
  for i2:=1 to 40 do
    begin
    if Password[i]=Alp[i2] then
      PWDbuf[i-1]:=i2;//Deshalb müssen wir immer eins abziehen
    end;
  end;
{...}



Ich denke, dass das funktionieren sollte.

MfG
Regan


Wii360 - Mi 29.04.09 14:53

Danke, also jetzt funktioniert alles!

MFG Wii360