encryption and decryption

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

encryption and decryption

Rainer Stratmann
Does someone know how to do encryption and decryption with keys?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

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 <[hidden email]>:
> Does someone know how to do encryption and decryption with keys?
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

silvioprog
2012/6/22 Jorge Aldo G. de F. Junior <[hidden email]>:
> Theres a blowfish encryption system in freepascal
>
> http://www.freepascal.org/docs-html/fcl/blowfish/index.html
>
> 2012/6/22 Rainer Stratmann <[hidden email]>:
>> Does someone know how to do encryption and decryption with keys?

Hellow guys,

How to use TBlowFishEnCryptStream? Is there any example for mortals? :)

--
Silvio Clécio
My public projects - github.com/silvioprog
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

Michael Van Canneyt


On Fri, 22 Jun 2012, silvioprog wrote:

> 2012/6/22 Jorge Aldo G. de F. Junior <[hidden email]>:
>> Theres a blowfish encryption system in freepascal
>>
>> http://www.freepascal.org/docs-html/fcl/blowfish/index.html
>>
>> 2012/6/22 Rainer Stratmann <[hidden email]>:
>>> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

leledumbo
Administrator
In reply to this post by silvioprog
> 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.
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

silvioprog
In reply to this post by Michael Van Canneyt
2012/6/23 Michael Van Canneyt <[hidden email]>:
[...]
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

silvioprog
In reply to this post by leledumbo
2012/6/23 leledumbo <[hidden email]>:

>> 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 archive at Nabble.com.

I'll test this sample now ... :)

--
Silvio Clécio
My public projects - github.com/silvioprog
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

silvioprog
2012/6/23 silvioprog <[hidden email]>:

> 2012/6/23 leledumbo <[hidden email]>:
>>> 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 archive at Nabble.com.
>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

leledumbo
Administrator
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.
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

Rainer Stratmann
Am Sunday 24 June 2012 16:59:36 schrieb leledumbo:
>   key := 'testkey';
>   value := 'this is a string';

How is it working?
By xoring 'testkey' with 'this is a string' byte by byte?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: encryption and decryption

Reinier Olislagers
On 24-6-2012 17:30, Rainer Stratmann wrote:
> Am Sunday 24 June 2012 16:59:36 schrieb leledumbo:
>>   key := 'testkey';
>>   value := 'this is a string';
>
> How is it working?
> By xoring 'testkey' with 'this is a string' byte by byte?
I hope not.
http://en.wikipedia.org/wiki/Blowfish_%28cipher%29
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal