Hi all,
I am trying to create a procedure that encrypts my passwords using blowfish and then using another procedure decrypts....
I wrote the following sample program, but for some reason I can't get it decrypted using the decrypt procedure... it works using the DoItAll button which encrypts and decrypts in one run...(but that is not the way)
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
BlowFish;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
mysalt : string;
end;
const
chars = 'qwertyuiopasdfghjklzxcvbnm`1234567890-=[];~!@#$%^&*()_+{}:"<>?QWERTYUIOPASDFGHJKLZXCVBNM';
key = '53285GFDJSLKFJSDLJ*#fefjsdljlk3284-3284-3285632:"<?>{}BXCV#@$GFDgf#@$%#@^H"L"Lgfd#@%#@%K#%#@?:_ÉÁŐÚőúéáüóöäü+ß´0ÖÄÜ';
var
Form1: TForm1;
procedure Encrypt(const input:string ; out storesalt,crypted : string);
procedure Decrypt(const input,salt: string; out password : string);
implementation
{$R *.lfm}
{ TForm1 }
procedure Decrypt(const input,salt: string; out password : string);
var
de: TBlowFishDeCryptStream;
s1: TStringStream;
temp : string;
begin
// decrypt
{ 4 }
s1 := TStringStream.Create(input);
{ 5 }
de := TBlowFishDeCryptStream.Create(salt+key+salt,s1);
{ 6 }
temp := de.ReadAnsiString;
password:=temp;
de.Free;
s1.Free;
end;
procedure Encrypt(const input:string ; out storesalt,crypted : string);
var
en: TBlowFishEncryptStream;
s1: TStringStream;
value,temp,salt: String;
i : integer;
begin
// we make some random salt here
randomize;
for i:=0 to length(chars) do
begin
salt:=salt+chars[random(length(chars))];
end;
// encrypt
{ 1 }
value := input;
{ 2 }
s1 := TStringStream.Create('');
en := TBlowFishEncryptStream.Create(salt+key+salt,s1);
{ 3 }
en.WriteAnsiString(value);
en.Free;
crypted:=s1.DataString;
storesalt:=salt;
s1.Free
end;{Encrypt}
procedure TForm1.Button1Click(Sender: TObject);
const
chars = 'qwertyuiopasdfghjklzxcvbnm`1234567890-=[];~!@#$%^&*()_+{}:"<>?QWERTYUIOPASDFGHJKLZXCVBNM';
key = '53285GFDJSLKFJSDLJ*#fefjsdljlk3284-3284-3285632:"<?>{}BXCV#@$GFDgf#@$%#@^H"L"Lgfd#@%#@%K#%#@?:_ÉÁŐÚőúéáüóöäü+ß´0ÖÄÜ';
var
en: TBlowFishEncryptStream;
de: TBlowFishDeCryptStream;
s1,s2: TStringStream;
value,temp: String;
salt : string;
i : integer;
begin
// we make some random salt here
randomize;
for i:=0 to length(chars) do
begin
salt:=salt+chars[random(length(chars))];
end;
Edit2.Text:=salt;
// encrypt
{ 1 }
value := Edit1.Text;
{ 2 }
s1 := TStringStream.Create('');
en := TBlowFishEncryptStream.Create(salt+key+salt,s1);
{ 3 }
en.WriteAnsiString(value);
en.Free;
Edit3.Text:=s1.DataString;
// decrypt
{ 4 }
s2 := TStringStream.Create(s1.DataString);
s1.Free;
{ 5 }
de := TBlowFishDeCryptStream.Create(salt+key+salt,s2);
{ 6 }
temp := de.ReadAnsiString;
Edit4.Text:=temp;
de.Free;
s2.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var salt,crypted : string;
begin
Encrypt(Edit1.Text,mysalt,crypted);
Edit3.Text:=crypted;
Edit2.Text:=mysalt;
end;
procedure TForm1.Button3Click(Sender: TObject);
var password : string;
begin
Decrypt(Edit3.Text,mysalt,password);
Edit4.Text:=password;
end;
end.
I was trying to solve this yesterday but could not do it... can someone please tell me what is possibly wrong with my decrypt procedure, it does not seem to work...
Greetings
Robert