Lazarus

Programming => LCL => Topic started by: jshand2010 on October 31, 2010, 06:32:39 pm

Title: How to use dcpcrypt??
Post by: jshand2010 on October 31, 2010, 06:32:39 pm
i keep reading about how to use this module(s), but have never really understood how to code it into my software.

please can someone give me step by step instructions

many thanks for your help
Title: Re: How to use dcpcrypt??
Post by: faber on October 31, 2010, 06:42:12 pm
Demos and help on dcpcrypt home page: http://www.cityinthesky.co.uk/cryptography.html (http://www.cityinthesky.co.uk/cryptography.html)
Title: Re: How to use dcpcrypt??
Post by: sfeinst on October 31, 2010, 06:49:50 pm
I don't use the LCL for it, but here is how I code using the library:

I add the following to uses:
DCPrijndael, DCPSha256

Then when loading data from a file (leaving out what I do with the data:
var
   Crypt: TDCP_rijndael;
   FileData: TFileStream;
        Data: TStringStream;
begin
  FileData := TFileStream.Create(Filename, fmOpenRead);
  Crypt := TDCP_rijndael.Create(nil);
  try
    Crypt.InitStr(Self.Password, TDCP_sha256);   // self-Password is the password used to save the file
    Crypt.DecryptStream(FileData, Data, FileData.Size);
    Crypt.Burn;
    // I then use Data as if it was not encrypted
   ...
  finally
    FreeAndNil(Crypt);
    FreeAndNil(Data);
    FreeAndNil(FileData);
  end;


to save a file I do the following:

var
   Crypt: TDCP_rijndael;
   FileData: TFileStream;
   Data: TStringStream;
begin
  FileData := TFileStream.Create(Filename, fmCreate);
  Data := TStringStream.Create('');
  Crypt := TDCP_rijndael.Create(nil);
  try
    // I write data out to a stream
  ...
    // then using that stream, I encrypt it to a file
    Crypt.InitStr(Self.Password, TDCP_sha256);
    Crypt.EncryptStream(Data, FileData, Data.Size);
    Crypt.Burn;
  finally
    FreeAndNil(Crypt);
    FreeAndNil(Data);
    FreeAndNil(FileData);
  end;
Title: Re: How to use dcpcrypt??
Post by: jshand2010 on November 15, 2010, 03:33:35 am
I have tried using this code and it doesn't work.  i may have just missed the point that need attention.  please give me step by step instructions on how to set this up
Title: Re: How to use dcpcrypt??
Post by: sfeinst on November 16, 2010, 11:46:43 am
My steps would be very similar to the code I posted.  It might be easier instead if you post the code you are trying and the error you are getting.
Title: Re: How to use dcpcrypt??
Post by: Dibo on November 16, 2010, 12:13:07 pm
This is my code how I encrypt string:
Code: Pascal  [Select][+][-]
  1. uses DCPrijndael, DCPsha256;
  2.  
  3. var
  4.   S1, S2: String;
  5.   c: TDCP_rijndael;
  6. begin
  7.   c := TDCP_rijndael.Create(nil);
  8.   try
  9.     c.InitStr('MyCustomKey', TDCP_sha256);
  10.     S1 := c.EncryptString('First string');
  11.     c.Reset;
  12.     S2 := c.EncryptString('Second string');
  13.   finally
  14.     c.Free;
  15.   end;
  16. end;  
  17.  
And decrypt:
Code: Pascal  [Select][+][-]
  1. uses DCPrijndael, DCPsha256;
  2.  
  3. var
  4.   S1, S2: String;
  5.   c: TDCP_rijndael;
  6. begin
  7.   c := TDCP_rijndael.Create(nil);
  8.   try
  9.     c.InitStr('MyCustomKey', TDCP_sha256);
  10.     S1 := c.DecryptString(SomeEncryptedVariable);
  11.     c.Reset;
  12.     S2 := c.DecryptString(SomeEncryptedVariable);
  13.   finally
  14.     c.Free;
  15.   end;
  16. end;  
  17.  
One thing I noticed. If you want to encrypt / decrypt next string using the same object, you must first reset this object by "Reset" call. Otherwise, next string will be incorrectly encrypted / decrypted
Title: Re: How to use dcpcrypt??
Post by: sfeinst on November 16, 2010, 12:37:09 pm
Are you saying the code you posted fails or does work?  If it fails, what is wrong.  If it works, what is your question?
Title: Re: How to use dcpcrypt??
Post by: Dibo on November 16, 2010, 03:16:51 pm
It works. This is only another example how to use dcrypt (jshand2010 question in first post)
Title: Re: How to use dcpcrypt??
Post by: sfeinst on November 16, 2010, 03:31:57 pm
It works. This is only another example how to use dcrypt (jshand2010 question in first post)
Sorry, I mistook your post for the original poster's.
Title: Re: How to use dcpcrypt??
Post by: jshand2010 on November 26, 2010, 11:20:03 pm
you know when you spoke about adding clauses DCPr.... to the uses part??

you mentioned that i also need to go to project options etc....  at this stage.  i am unsure to where to put the paths to those clauses.

please help
Title: Re: How to use dcpcrypt??
Post by: sfeinst on November 28, 2010, 03:38:32 am
you know when you spoke about adding clauses DCPr.... to the uses part??

you mentioned that i also need to go to project options etc....  at this stage.  i am unsure to where to put the paths to those clauses.

please help

I put the compiled path in Other Unit Files (for me this is ../dcpcrypt-2.0.4.1/lib/i386-linux)
I put the source path in Other Sources (for me this is ../dcpcrypt-2.0.4.1;../dcpcrypt-2.0.4.1/Ciphers;../dcpcrypt-2.0.4.1/Hashes)

But the values will depend on where you have the code.
Title: Re: How to use dcpcrypt??
Post by: jshand2010 on December 19, 2010, 07:05:05 am
i really appreciate your help sfeinst.  thanks a million.

I got it all wording with the CBC method and works very well.  however after so many characters it doesn't seem to encrypt the whole text after about 20 characters.  can you show me how to encrypt everything that was typed into text boxes or memos?  thanks again
Title: Re: How to use dcpcrypt??
Post by: sfeinst on December 19, 2010, 11:43:31 pm
posting your code would help.  the steps I do were already posted, so if that is not working for you, I owuld need to see what you are attempting.
Title: Re: How to use dcpcrypt??
Post by: jshand2010 on January 01, 2011, 09:30:55 pm
I don't use the LCL for it, but here is how I code using the library:

I add the following to uses:
DCPrijndael, DCPSha256

Then when loading data from a file (leaving out what I do with the data:
var
   Crypt: TDCP_rijndael;
   FileData: TFileStream;
        Data: TStringStream;
begin
  FileData := TFileStream.Create(Filename, fmOpenRead);
  Crypt := TDCP_rijndael.Create(nil);
  try
    Crypt.InitStr(Self.Password, TDCP_sha256);   // self-Password is the password used to save the file
    Crypt.DecryptStream(FileData, Data, FileData.Size);
    Crypt.Burn;
    // I then use Data as if it was not encrypted
   ...
  finally
    FreeAndNil(Crypt);
    FreeAndNil(Data);
    FreeAndNil(FileData);
  end;


to save a file I do the following:

var
   Crypt: TDCP_rijndael;
   FileData: TFileStream;
   Data: TStringStream;
begin
  FileData := TFileStream.Create(Filename, fmCreate);
  Data := TStringStream.Create('');
  Crypt := TDCP_rijndael.Create(nil);
  try
    // I write data out to a stream
  ...
    // then using that stream, I encrypt it to a file
    Crypt.InitStr(Self.Password, TDCP_sha256);
    Crypt.EncryptStream(Data, FileData, Data.Size);
    Crypt.Burn;
  finally
    FreeAndNil(Crypt);
    FreeAndNil(Data);
    FreeAndNil(FileData);
  end;


i have no idea on how this code works or how to implement the data into arrays etc...
Title: Re: How to use dcpcrypt??
Post by: ezlage on September 02, 2011, 10:34:52 pm
This way works for me:

Code: [Select]
function Codifica(chave:string; texto:string; nivel:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} //Encode(key, text, level)
  var
    motor: TDCP_blowfish;
    i: integer;
    t: string;
  begin
    if nivel=0
      then nivel:=1
      else if nivel>2
        then nivel:=2;
    motor:=TDCP_blowfish.Create(nil);
    t:=texto;
    for i:=1 to nivel do begin
      motor.InitStr(chave,TDCP_sha256);
      t:=motor.EncryptString(t);
      motor.Burn;
    end;
    result:=t;
    motor.Free;
  end;

function Decodifica(chave:string; codigo:string; nivel:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} //Decode(key, text, level)
  var
    motor: TDCP_blowfish;
    i: integer;
    t: string;
  begin
    if nivel=0
      then nivel:=1
      else if nivel>2
        then nivel:=2;
    motor:=TDCP_blowfish.Create(nil);
    t:=codigo;
    for i:=1 to nivel do begin
      motor.InitStr(chave,TDCP_sha256);
      t:=motor.DecryptString(t);
      motor.Burn;
    end;
    result:=t;
    motor.Free;
  end;
Title: Re: How to use dcpcrypt??
Post by: Silvio Clécio on September 02, 2011, 11:37:49 pm
See a nice demo:

http://code.google.com/p/lazsolutions/source/browse/#svn%2Ftrunk%2FDemos%2FLSCrypt
TinyPortal © 2005-2018