Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-06

encryption and decryption

11 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Rainer Stratmann

Does someone know how to do encryption and decryption with keys?

Michael Van Canneyt
to silvioprog
silvioprog wrote 12 lines
2012/6/22 Jorge Aldo G. de F. Junior : > Theres a blowfish encryption system in freepascal > > http://www.freepascal.org/docs-html/fcl/blowfish/index.html > > 2012/6/22 Rainer Stratmann : >> Does someone know how to do encryption and decryption with keys? Hellow guys, How to use TBlowFishEnCryptStream? Is there any example for mortals? :)

The testbf.pp program in fcl-base/examples shows how to use it.

It's very simple, really. Just create an encryption stream, and write the data you want to see encrypted to this stream. The output of the stream is the encrypted data.

Michael.

leledumbo
to silvioprog: 2012/6/22 Jorge Aldo G. de F. Junior : How to use TBlowFishEnCryptStream? Is there any…
How to use TBlowFishEnCryptStream? Is there any example for mortals?

No but I guess the doc is clear enough.

To encrypt:
1. Create TBlowFishEnCryptStream instance supplying a key and the stream to write encrypted data to
2. Write the data using its Write method
3. Flush or Free (Free implies Flush) to ensure all data are written

To decrypt:
1. Create TBlowFishDeCryptStream instance supplying a key and the stream containing previously encrypted data. If you want to use directly previous stream, ensure to Seek it to the first data. To be safe, create a new stream instead and put the data from previous stream.
2. Read the data using its Read method (you should now the data length, for some stream Size property can be used)

An example:

{$mode objfpc}{$H+}
uses classes,blowfish;
var
  en: TBlowFishEncryptStream;
  de: TBlowFishDeCryptStream;
  s1,s2: TStringStream;
  key,value,temp: String;
begin
  key := 'testkey';
  value := 'this is a string';
  s1 := TStringStream.Create('');
  en := TBlowFishEncryptStream.Create(key,s1);
    en.Write(value[1],Length(value));
  en.Free;
  WriteLn('encrypted: ' + s1.DataString);
  s2 := TStringStream.Create(s1.DataString);
  s1.Free;
  de := TBlowFishDeCryptStream.Create(key,s2);
  SetLength(temp,s2.Size);
  de.Read(temp[1],s2.Size);
  WriteLn('decrypted: ' + temp);
  de.Free;
  s2.Free;
end.
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/encryption-and-decryption-tp5710131p5710137.html Sent from the Free Pascal - General mailing list .
silvioprog
to Michael Van Canneyt: The testbf.pp program in fcl-base/examples shows how to use it. It's very simple, really.…

2012/6/23 Michael Van Canneyt :
[...]

Show quoted text 7 lines
The testbf.pp program in fcl-base/examples shows how to use it. It's very simple, really. Just create an encryption stream, and write the data you want to see encrypted to this stream. The output of the stream is the encrypted data. Michael.

I saw, but I did not understand this example. :/

Silvio Clécio My public projects - github.com/silvioprog
silvioprog
to leledumbo: No but I guess the doc is clear enough. To encrypt: 1. Create TBlowFishEnCryptStream…

2012/6/23 leledumbo :

Show quoted text 56 lines
> How to use TBlowFishEnCryptStream? Is there any example for mortals? No but I guess the doc is clear enough. To encrypt: 1. Create TBlowFishEnCryptStream instance supplying a key and the stream to write encrypted data to 2. Write the data using its Write method 3. Flush or Free (Free implies Flush) to ensure all data are written To decrypt: 1. Create TBlowFishDeCryptStream instance supplying a key and the stream containing previously encrypted data. If you want to use directly previous stream, ensure to Seek it to the first data. To be safe, create a new stream instead and put the data from previous stream. 2. Read the data using its Read method (you should now the data length, for some stream Size property can be used) An example: {$mode objfpc}{$H+} uses classes,blowfish; var  en: TBlowFishEncryptStream;  de: TBlowFishDeCryptStream;  s1,s2: TStringStream;  key,value,temp: String; begin  key := 'testkey';  value := 'this is a string';  s1 := TStringStream.Create('');  en := TBlowFishEncryptStream.Create(key,s1);    en.Write(value[1],Length(value));  en.Free;  WriteLn('encrypted: ' + s1.DataString);  s2 := TStringStream.Create(s1.DataString);  s1.Free;  de := TBlowFishDeCryptStream.Create(key,s2);  SetLength(temp,s2.Size);  de.Read(temp[1],s2.Size);  WriteLn('decrypted: ' + temp);  de.Free;  s2.Free; end. -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/encryption-and-decryption-tp5710131p5710137.html Sent from the Free Pascal - General mailing list 'll test this sample now ... :)
Silvio Clécio My public projects - github.com/silvioprog
silvioprog
to silvioprog

2012/6/23 silvioprog :

Show quoted text 59 lines
2012/6/23 leledumbo : >> How to use TBlowFishEnCryptStream? Is there any example for mortals? > > No but I guess the doc is clear enough. > > To encrypt: > 1. Create TBlowFishEnCryptStream instance supplying a key and the stream to > write encrypted data to > 2. Write the data using its Write method > 3. Flush or Free (Free implies Flush) to ensure all data are written > > To decrypt: > 1. Create TBlowFishDeCryptStream instance supplying a key and the stream > containing previously encrypted data. If you want to use directly previous > stream, ensure to Seek it to the first data. To be safe, create a new stream > instead and put the data from previous stream. > 2. Read the data using its Read method (you should now the data length, for > some stream Size property can be used) > > An example: > > {$mode objfpc}{$H+} > > uses classes,blowfish; > > var >  en: TBlowFishEncryptStream; >  de: TBlowFishDeCryptStream; >  s1,s2: TStringStream; >  key,value,temp: String; > begin >  key := 'testkey'; >  value := 'this is a string'; > >  s1 := TStringStream.Create(''); >  en := TBlowFishEncryptStream.Create(key,s1); >    en.Write(value[1],Length(value)); >  en.Free; > >  WriteLn('encrypted: ' + s1.DataString); > >  s2 := TStringStream.Create(s1.DataString); >  s1.Free; > >  de := TBlowFishDeCryptStream.Create(key,s2); > >  SetLength(temp,s2.Size); >  de.Read(temp[1],s2.Size); >  WriteLn('decrypted: ' + temp); >  de.Free; > >  s2.Free; > end. > > -- > View this message in context: http://free-pascal-general.1045716.n5.nabble.com/encryption-and-decryption-tp5710131p5710137.html > Sent from the Free Pascal - General mailing list . I'll test this sample now ... :)

It worked perfectly well, and is a great example, it explains in detail how to use the blowfish unit. :)

It would be interesting to distribute this example with the Free Pascal.

Silvio Clécio My public projects - github.com/silvioprog
leledumbo
to silvioprog

For examples, I think it's better to use the data type wrapper of the Read/Write, instead of using the low level methods:

{$mode objfpc}{$H+}
uses classes,blowfish;
var
  en: TBlowFishEncryptStream;
  de: TBlowFishDeCryptStream;
  s1,s2: TStringStream;
  key,value,temp: String;
begin
  key := 'testkey';
  value := 'this is a string';
  s1 := TStringStream.Create('');
  en := TBlowFishEncryptStream.Create(key,s1);
  en.WriteAnsiString(value);
  en.Free;
  WriteLn('encrypted: ' + s1.DataString);
  s2 := TStringStream.Create(s1.DataString);
  s1.Free;
  de := TBlowFishDeCryptStream.Create(key,s2);
  temp := de.ReadAnsiString;
  WriteLn('decrypted: ' + temp);
  de.Free;
  s2.Free;
end.
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/encryption-and-decryption-tp5710131p5710147.html Sent from the Free Pascal - General mailing list .
Rainer Stratmann
to leledumbo
leledumbo wrote
key := 'testkey'; value := 'this is a string';

How is it working?
By xoring 'testkey' with 'this is a string' byte by byte?