Recent

Author Topic: DCPCrypt crypt/decrypt string problem  (Read 4372 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
DCPCrypt crypt/decrypt string problem
« on: May 23, 2015, 05:16:09 pm »
Boys, for a project he's making a cryptography library with 4 functions

2 for encryption / decryption of files
2 for encryption / decryption of strings

File no problem, they do not work with strings. About me explain why?
Code: [Select]
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.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: DCPCrypt crypt/decrypt string problem
« Reply #1 on: May 23, 2015, 05:19:30 pm »
when decrypting first decrypt the aes then the blowfish.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: DCPCrypt crypt/decrypt string problem
« Reply #2 on: May 23, 2015, 05:47:01 pm »
perfect. thank you  :)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: DCPCrypt crypt/decrypt string problem
« Reply #3 on: May 27, 2015, 10:45:21 am »
Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018