How can I encrypt a string without increase the its length?

delphi package - madRes, madTools, madStrings, ...
Post Reply
alexhaifa
Posts: 11
Joined: Tue Dec 13, 2005 10:51 am
Location: BRazil

How can I encrypt a string without increase the its length?

Post by alexhaifa »

Hi

I am studying Delphi and some library for it, I can see that your library has many wonderful functions.

I have a database in access with 100 rows and I want to encrypt it. But the problem is whether I encrypt it and the length of my string increase I can crash the information.

How could I encrypt a string without increase it. Thanks

Alexandre / Brazil
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

How long are your strings? Is their length always a multitude of 8? In that case encryption without length increase is possible. Otherwise it's not impossible, but you then need to invent your own encryption method. The standard encryption methods all only work on 8 byte long chunks.
alexhaifa
Posts: 11
Joined: Tue Dec 13, 2005 10:51 am
Location: BRazil

Post by alexhaifa »

See,

I have a database with some sentences like this below:

Str := 'I have a beautiful house on the beach and it is very big'

when you say 8 , what do you mean?

is it correct to do something like this?

Var
Str : String;
Begin
Str := 'I have a beautiful house on the beach and it is very big';
Encrypt(Str, 'key145');
Edit1.Text := Str // only to show the encrypted string
End
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Encrypted text will contain wild chars in it. It can't be properly posted into an edit box. If you want an output friendly encryption, you need to use first "Encrypt" and then "Encode". To revert this, you need to do "Decode" and then "Decrypt". The strings will become much longer that way, though.
alexhaifa
Posts: 11
Joined: Tue Dec 13, 2005 10:51 am
Location: BRazil

Post by alexhaifa »

Hi, unfortunately the function didn't work fine as much I have tested :(

See what I have when I decrypt:

'Prazer em conhecê-lo! E é também um prazer tê-loþ±ÔõïÖ'

The last ones caracteres are wrong :(

I don't know what I must use to encrypt my database. Do you know why this happen? I can send you a copy of my database, it is small and the code of my project, maybe you can help me.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Just show me the code you used for encryption and decryption.
alexhaifa
Posts: 11
Joined: Tue Dec 13, 2005 10:51 am
Location: BRazil

Post by alexhaifa »

Here is all my code where I encrypt my table.

To display it in dbedit I placed a code in ongettext the descrypt function


procedure TForm1.BitBtn1Click(Sender: TObject);
Var
TQ : TADOQuery;
St1, St2 : String;
begin
TQ := TADOQuery.Create(Self);
TQ.Connection := ADOConnection1;

QryPalavra.DisableControls;
QryPalavra.First;
Gauge1.MaxValue := QryPalavra.RecordCount;
Gauge1.Progress := 0;

Try
While not QryPalavra.eof do
Begin
With TQ do
Begin
St1 := QryPalavraCP_FRASE_PT.AsString;
St2 := QryPalavraCP_FRASE_US.AsString;

Encrypt(St1,'Key4563');
Encrypt(St2,'Key7497');

Close;
Sql.Clear;
Sql.Add('UPDATE CADPAL');
Sql.Add('SET CP_FRASE_PT=:PT, CP_FRASE_US=:US');
Sql.Add('WHERE CP_CODIGO=:CD');
Parameters.ParamByName('PT').Value := St1;
Parameters.ParamByName('US').Value := St2;
Parameters.ParamByName('CD').Value := QryPalavraCP_CODIGO.AsInteger;
ExecSQL;
Gauge1.Progress := Gauge1.Progress + 1;
End;
QryPalavra.Next;
Application.ProcessMessages;
End;
Finally
FreeAndNil(TQ)
End;
End;

procedure TForm1.QryPalavraCP_FRASE_PTGetText(Sender: TField; var Text: String; DisplayText: Boolean);
Var
St : String;
begin
St := Sender.AsString;
Decrypt(St,'Key4563');
Text := St
end;
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Try it with Encrypt + Encode and with Decode + Decrypt, as I suggested before.
alexhaifa
Posts: 11
Joined: Tue Dec 13, 2005 10:51 am
Location: BRazil

Post by alexhaifa »

ok,

This way, right?

While not QryPalavra.eof do
Begin
With TQ do
Begin
St1 := QryPalavraCP_FRASE_PT.AsString;
St2 := QryPalavraCP_FRASE_US.AsString;

Encrypt(St1,'Key4563');
Encrypt(St2,'Key7497');

St1 := Encode(St1);
St2 := Encode(St2);

Close;
Sql.Clear;
Sql.Add('UPDATE CADPAL');
Sql.Add('SET CP_FRASE_PT=:PT, CP_FRASE_US=:US');
Sql.Add('WHERE CP_CODIGO=:CD');
Parameters.ParamByName('PT').Value := St1;
Parameters.ParamByName('US').Value := St2;
Parameters.ParamByName('CD').Value := QryPalavraCP_CODIGO.AsInteger;
ExecSQL;
Gauge1.Progress := Gauge1.Progress + 1;
End;
alexhaifa
Posts: 11
Joined: Tue Dec 13, 2005 10:51 am
Location: BRazil

Post by alexhaifa »

Hi, It seems that worked very fine. I will check all my database.

Thanks by now
Post Reply