Thema: Delphi DEC 5.1 wie benutzen?

Einzelnen Beitrag anzeigen

delphin06

Registriert seit: 18. Jun 2006
154 Beiträge
 
#46

Re: DEC 5.1 wie benutzen?

  Alt 22. Feb 2008, 16:20
Ich benutze so in etwa die function die ich so ziemlich am Anfang mal gepostet habe nur halt für n Memorysream umgeschrieben.

Und den Code:
Delphi-Quellcode:
SetDefaultCipherClass(TCipher_Rijndael);
  SetDefaultHashClass(THash_SHA1);
  IdentityBase := $84485225;
  RegisterDECClasses([TCipher_Rijndael, THash_SHA1]);
Rufe ich beim Start in der Formcreate() auf. Da ich nur diese eine Verschlüsselung benutze sollte es da keine Probleme geben oder?


Also ich benutze diese beiden Funktionen jeweils zum entschlüsseln bzw. verschlüsseln:
Delphi-Quellcode:
procedure EncodeMStream(const ADestFilename: String; const APassword: Binary;
                       ACipher: TDECCipherClass = nil; AMode: TCipherMode = cmCTSx;
                       AHash: TDECHashClass = nil);
var
  Dest: TStream;

  procedure Write(const Value; Size: Integer);
  begin
    Dest.WriteBuffer(Value, Size);
  end;

  procedure WriteByte(Value: Byte);
  begin
    Write(Value, SizeOf(Value));
  end;

  procedure WriteLong(Value: LongWord);
  begin
    Value := SwapLong(Value);
    Write(Value, SizeOf(Value));
  end;

  procedure WriteBinary(const Value: Binary);
  begin
    WriteByte(Length(Value));
    Write(Value[1], Length(Value));
  end;

var
  Sourcestr: TMemoryStream;
  Seed: Binary;
begin
  ACipher := ValidCipher(ACipher);
  AHash := ValidHash(AHash);
  Seed := RandomBinary(16);
  Sourcestr:=TMemorystream.Create();
  try
    sourcestr.WriteComponent(Form1.listview1);
    SourceStr.Position := 0;
    Dest := TFileStream.Create(ADestFilename, fmCreate);
    try
      with ACipher.Create do
        try
          Mode := AMode;
          Init(AHash.KDFx(APassword, Seed, Context.KeySize));

          WriteLong(Identity);
          WriteByte(Byte(Mode));
          WriteLong(AHash.Identity);
          WriteBinary(Seed);
          WriteLong(Sourcestr.Size);
          EncodeStream(Sourcestr, Dest, Sourcestr.Size);
          WriteBinary(CalcMAC);
        finally
          Free;
        end;
    finally
      Dest.Free;
    end;
      //ProtectStream(Source);
  finally
    Sourcestr.Free;
  end;
  //DeleteFile(AFileName);
end;

procedure TForm1.DecodeMStream(const ASourceFileName: String; const APassword: Binary);
var
    Source: TStream;

  procedure Read(var Value; Size: Integer);
  begin
    Source.ReadBuffer(Value, Size);
  end;

  function ReadByte: Byte;
  begin
    Read(Result, SizeOf(Result));
  end;

  function ReadLong: LongWord;
  begin
    Read(Result, SizeOf(Result));
    Result := SwapLong(Result);
  end;

  function ReadBinary: Binary;
  begin
    SetLength(Result, ReadByte);
    Read(Result[1], Length(Result));
  end;

var
    Dest: TMemoryStream;
    error:Boolean;
begin
  error:=false;
  Source := TFileStream.Create(ASourceFileName, fmOpenRead or fmShareDenyNone);
  try
    try
      Dest := TMemorystream.Create;
      try
        try
          with CipherByIdentity(ReadLong).Create do
          try
            Mode := TCipherMode(ReadByte);
            Init(HashByIdentity(ReadLong).KDFx(APassword, ReadBinary, Context.KeySize));
            DecodeStream(Source, Dest, ReadLong);
            if ReadBinary <> CalcMAC then
              //raise EDECException.Create('Invalid decryption');
              begin
                showmessage('Fehler: falsches Passwort');
                error:=true;
              end;
          finally
            Free;
          end;
        except
          //ProtectStream(Dest);
          raise;
        end;
        finally
          dest.Position := 0;
          if error = false then
            dest.ReadComponent(Form1.ListView1);
          Dest.Free;
        end;
      except
      //DeleteFile(ChangeFileExt(AFileName, ''));
      raise;
    end;
  finally
    Source.Free;
  end;
  if error = true then
    begin
      form1.close;
      PasswordDlg.Close;
    end
  else
    begin
      form1.Enabled:=true;
      form1.Visible:=true;
    end;
end;
Und dann halt die Klassenregestrierungen im formcreate().

Gruß
  Mit Zitat antworten Zitat