Einzelnen Beitrag anzeigen

Kytrix

Registriert seit: 26. Nov 2007
54 Beiträge
 
#1

Decrypten von Daten in TStringList fehlerhaft

  Alt 25. Aug 2012, 16:38
Hallo ich verschlüssele einen String den ich dann in eine TStringList schreibe welche wiederum als datei gespeichert wird. Wennn ich nun aus dieser datei wieder entschlüsseln möchte so bekomme ich nicht das richtige ergebnis.

An der ver-/entschlüsselung kann es nicht liegen, denn:

Delphi-Quellcode:
  Test:='hallo';
  temp.Add(Encrypt(Test,w)); //temp=Tstringlist;
  test:= Encrypt(Test,w);
  temp.Add(Decrypt(test,w)) ;
klappt.

kann es sein das zusätzliche zeichen aus der datei in die Stringlist geschrieben/gelesen werden die die ver-/entschlüsselung "verfälschen"?

Delphi-Quellcode:
procedure TfMain.BtOpenEncryptedClick(Sender: TObject);//decrypt from file
Var w:Word;
begin
  w:= 16518;

  OpenDialog1.Execute;
  Info.LoadFromFile(OpenDialog1.FileName);

  EdShowInfo.Text:=Decrypt(Info.Text,w);
end;

procedure TfMain.SaveEncrypted(Sender: TObject);//save encrypted to file
Var InformationText: TStringList;
  w:Word;
  Test:String;
begin
  w:= 16518;
  InformationText:=TstringList.Create();
  Test:=Encrypt('TEST',w);
  InformationText.Add(Test);
  InformationText.SaveToFile(ExtractFilePath(ParamStr(0))+'reg_info.dat');
  MessageDlg('The registration information file "info.dat" was successfully created in:'+sLineBreak+sLineBreak+
              '"'+ExtractFilePath(ParamStr(0))+'"',mtInformation,[mbOK],0);
end;

function Encrypt (const s: string; Key: Word) : string;
var
  i : byte;
  c1,c2: Integer;
begin
  c1:=823748325;
  c2:=1343424;
  Result:=s;
  SetLength(Result, length(s));
  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 TfMain.Decrypt (const s: string; Key: Word) : string;
var
  i : byte;
  c1,c2: Integer;
begin
  c1:=823748325;
  c2:=1343424;
  begin
    SetLength(Result, length(s));
    Result:=s;
    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;
end;

procedure TfMain.FormCreate(Sender: TObject);
begin
  Info:=TStringList.Create;
end;
  Mit Zitat antworten Zitat