Italian community of Lazarus and Free Pascal

Programmazione => Componenti Aggiuntivi => Topic aperto da: xinyiman - Maggio 23, 2015, 05:16:25 pm

Titolo: DCPCrypt problema crypt/decrypt string
Inserito da: xinyiman - Maggio 23, 2015, 05:16:25 pm
Ragazzi, per un progetto stò realizzando una libreria crittografica con 4 funzioni

2 per la crittografia/decrittografia dei file
2 per la crittografia/decrittografia delle stringhe

Sui file nessun problema, con le stringhe non funzionano. Chi mi spiega come mai?

Codice: [Seleziona]
unit Unit_Crypt_Function;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DCPSha1, DCPcrypt2, DCPblockciphers, DCPblowfish, dcprijndael;

  type

      { TCyperFile }

      TCyperFile=class
           public
                 constructor Create;
                 destructor Free;
                 procedure CifraFile(Original:String;Encrypted:String;Password:String; Password2: string);
                 procedure DeCifraFile(Encrypted:String;Original:string;Password:String; Password2: string);
                 function CifraStringa(Original: String;Password:String; Password2: string): string;
                 function DeCifraStringa(Original: String;Password:String; Password2: string): string;
      end;

implementation

{ TCyperFile }

constructor TCyperFile.Create;
begin

end;

destructor TCyperFile.Free;
begin

end;

procedure TCyperFile.CifraFile(Original:String;Encrypted:String;Password:String; Password2: string);
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  OriginalStream:TFileStream;
  EncryptStream:TFileStream;
begin
  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);
  OriginalStream := TFileStream.Create(Original,fmOpenRead);
  EncryptStream := TFileStream.Create(Encrypted,fmCreate);
  Blowfish.EncryptStream(OriginalStream,EncryptStream,OriginalStream.Size);
  BlowFish.Burn;
  AES.EncryptStream(EncryptStream,EncryptStream,EncryptStream.Size);
  AES.Burn;
  EncryptStream.Free;
  OriginalStream.Free;
  Blowfish.Free;
  AES.Free;
end;

procedure TCyperFile.DeCifraFile(Encrypted:String;Original:string;Password:String; Password2: string);
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  DecryptStream:TMemoryStream;
  EncryptStream:TFileStream;
begin
  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);
  EncryptStream := TFileStream.Create(Encrypted,fmOpenRead);
  DecryptStream := TMemoryStream.Create;
  Blowfish.DecryptStream(EncryptStream,DecryptStream ,EncryptStream.Size);
  Blowfish.Burn;
  AES.DecryptStream(DecryptStream,DecryptStream,DecryptStream.Size);
  AES.Burn;
  EncryptStream.Free;
  DecryptStream.Position :=0;
  DecryptStream.SaveToFile(Original);
  DecryptStream.Free;
  AES.Free;
end;

function TCyperFile.CifraStringa(Original: String; Password: String; Password2: string): string;
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  ret: string;
begin
  ret:='';
  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);
  ret:=Blowfish.EncryptString(Original);
  BlowFish.Burn;
  ret:=AES.EncryptString(ret);
  AES.Burn;
  Blowfish.Free;
  AES.Free;
  result:=ret;
end;

function TCyperFile.DeCifraStringa(Original: String; Password: String; Password2: string): string;
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  ret: string;
begin
  ret:='';

  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);

  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);

  ret:=Blowfish.DecryptString(Original);
  BlowFish.Burn;

  ret:=AES.DecryptString(ret);
  AES.Burn;

  Blowfish.Free;
  AES.Free;
  result:=ret;
end;

end.
Titolo: Re:DCPCrypt problema crypt/decrypt string
Inserito da: xinyiman - Maggio 23, 2015, 05:47:32 pm
Risolto così

Codice: [Seleziona]
unit Unit_Crypt_Function;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DCPSha1, DCPcrypt2, DCPblockciphers, DCPblowfish, dcprijndael;

  type

      { TCyperFile }

      TCyperFile=class
           public
                 constructor Create;
                 destructor Free;
                 procedure CifraFile(Original:String;Encrypted:String;Password:String; Password2: string);
                 procedure DeCifraFile(Encrypted:String;Original:string;Password:String; Password2: string);
                 function CifraStringa(Original: String;Password:String; Password2: string): string;
                 function DeCifraStringa(Original: String;Password:String; Password2: string): string;
      end;

implementation

{ TCyperFile }

constructor TCyperFile.Create;
begin

end;

destructor TCyperFile.Free;
begin

end;

procedure TCyperFile.CifraFile(Original:String;Encrypted:String;Password:String; Password2: string);
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  OriginalStream:TFileStream;
  EncryptStream:TFileStream;
begin
  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);
  OriginalStream := TFileStream.Create(Original,fmOpenRead);
  EncryptStream := TFileStream.Create(Encrypted,fmCreate);
  Blowfish.EncryptStream(OriginalStream,EncryptStream,OriginalStream.Size);
  BlowFish.Burn;
  AES.EncryptStream(EncryptStream,EncryptStream,EncryptStream.Size);
  AES.Burn;
  EncryptStream.Free;
  OriginalStream.Free;
  Blowfish.Free;
  AES.Free;
end;

procedure TCyperFile.DeCifraFile(Encrypted:String;Original:string;Password:String; Password2: string);
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  DecryptStream:TMemoryStream;
  EncryptStream:TFileStream;
begin
  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);
  EncryptStream := TFileStream.Create(Encrypted,fmOpenRead);
  DecryptStream := TMemoryStream.Create;
  Blowfish.DecryptStream(EncryptStream,DecryptStream ,EncryptStream.Size);
  Blowfish.Burn;
  AES.DecryptStream(DecryptStream,DecryptStream,DecryptStream.Size);
  AES.Burn;
  EncryptStream.Free;
  DecryptStream.Position :=0;
  DecryptStream.SaveToFile(Original);
  DecryptStream.Free;
  AES.Free;
end;

function TCyperFile.CifraStringa(Original: String; Password: String; Password2: string): string;
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  ret: string;
begin
  ret:='';
  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);
  ret:=Blowfish.EncryptString(Original);
  BlowFish.Burn;
  ret:=AES.EncryptString(ret);
  AES.Burn;
  Blowfish.Free;
  AES.Free;
  result:=ret;
end;

function TCyperFile.DeCifraStringa(Original: String; Password: String; Password2: string): string;
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  ret: string;
begin
  ret:='';

  AES:=TDCP_rijndael.Create(nil);
  AES.InitStr(Password2,TDCP_Sha1);

  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password,TDCP_Sha1);

  ret:=AES.DecryptString(Original);
  AES.Burn;

  ret:=Blowfish.DecryptString(ret);
  BlowFish.Burn;

  Blowfish.Free;
  AES.Free;
  result:=ret;
end;

end.