Does someone know how to do encryption and decryption with keys?
Mailing lists / fpc-pascal / 2012-06
encryption and decryption
11 messages · 6 people · –
- Ssilvioprog 4
- Lleledumbo 2
- RRainer Stratmann 2
- JJorge Aldo G. de F. Junior 1
- MMichael Van Canneyt 1
- RReinier Olislagers 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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? _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
2012/6/22 Jorge Aldo G. de F. Junior :
Show quoted text 6 lines
Hellow guys,
How to use TBlowFishEnCryptStream? Is there any example for mortals? :)
silvioprog wrote 12 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.
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.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
I saw, but I did not understand this example. :/
No but I guess the doc is clear enough. To encrypt: 1. Create TBlowFishEnCryptStream…
2012/6/23 leledumbo :
Show quoted text 56 lines
2012/6/23 silvioprog :
Show quoted text 59 lines
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.
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.leledumbo wrotekey := 'testkey'; value := 'this is a string';
How is it working?
By xoring 'testkey' with 'this is a string' byte by byte?
Rainer Stratmann wrote 6 lines
I hope not.
http://en.wikipedia.org/wiki/Blowfish_%28cipher%29
