Link to home
Start Free TrialLog in
Avatar of RAGAB2000
RAGAB2000

asked on

how can i encrypt data to store in SQL Server and decrypt it again?

dear's
the case is,
- I use delphi 6
- I SQL server 7 (I can use SQL 2000 in needed)
i want to store a data of a certen coulmn incrypted in any table and rebrows it again (decrypted)in my program. any help.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's correct. You can use the OnGetText and the OnSetText events of the TField object to do the encryption / decryption.

If you XOR each byte of the data your encrypting -regardless of the data type-, then convert the ascii code of that byte to the hexadecimal representaion, you will end up having an ASCII result for any kind of data including binary data. You can then save this data in a text field.

Here is two simple encrypt / decrypt functions to be used with text data.

It could be simply modified to accept any type of input.

//========================================================
function Encrypt(sInput, sKey: String): string;
var
     i, j: integer;
     b: byte;
     sOutput: string;
begin
     sOutput := '';
     for i:= 1 to Length(sInput) do begin
          b := Ord(sInput[i]);
          for j := 1 to Length(sKey) do b := b XOR Ord(sKey[j]);
          sOutput :=  sOutput + IntToHex(b, 2);
     end;
     result :=  sOutput;
end;
//========================================================
function Decrypt(sInput, sKey: String): string;
var
     i, j: integer;
     b: byte;
     sOutput: string;
begin
     sOutput := '';
     for i:= 1 to (Length(sInput) div 2) do begin
          b := StrToInt('$0' + sInput[i*2-1] + sInput[i*2]);
          for j := Length(sKey) downto 1 do b := b XOR Ord(sKey[j]);
          sOutput :=  sOutput + Chr(b);
     end;
     result :=  sOutput;
end;
//========================================================
Avatar of RAGAB2000
RAGAB2000

ASKER

i want to ask about the parameter sKey in the function encrypt and decrypt
I try to implement code in the OnSetText and OnGetText events of the TField object but i fail to do it.
can you help me to do it.
can help me
When it comes to encryption, we use an "encryption key" which is like a password. When someone needs to decrypt your data, it should take him/her more than knowing your encryption algorithm. s/he have to have the key as well. That's what the sKey parameter is for.

However, let me warn you that this encryption scheme I displayed to you -which is very easy to implement- is also the easiest to crack.

I think I can provide you with more help provided that I know more information about what you are trying to do.

- Are you accessing and displaying your database through data-bound controls or using SQL statements?

- What type of data you are encrypting?

Regards,
Emad
Dear Emadat,
- I am accessing my date (insert,update and delete)using Query control and some time useing table control.
i am not use data bound.
- integer data but stored in char() data type
- i made some changes in your code upove to make some complcation.
//========================================================
function Encrypt(sInput, sKey: String): string;
var
    i, j: integer;
    b: byte;
    sOutput: string;
begin
    sOutput := '';
    for i:= 1 to Length(sInput) do begin
         b := Ord(sInput[i])+i;
         for j := 1 to Length(sKey) do b := b XOR Ord(sKey[j]);
         sOutput :=  sOutput + IntToHex(b, 2);
    end;
    result :=  sOutput;
end;
//========================================================
function Decrypt(sInput, sKey: String): string;
var
    i, j: integer;
    b: byte;
    sOutput: string;
begin
    sOutput := '';
    for i:= 1 to (Length(sInput) div 2) do begin
         b := StrToInt('$0' + sInput[i*2-1] + sInput[i*2]);
         for j := Length(sKey) downto 1 do b := b XOR Ord(sKey[j]);
b:=b-i;
         sOutput :=  sOutput + Chr(b);
    end;
    result :=  sOutput;
end;
//========================================================
 
I did not get a chance to test the changes you made but you can test it by encrypting a string and then decrypting the result. You should get the original one.

About how to use the encryption and decryption; assume the scenario when you add or update data like that

-> strSQL := 'INSERT INTO table_name VALUES(id, ''' + Encrypt(your_data, your_key) + ''')';
-> Execute your query

To display your data:
-> strSQL := 'SELECT _fields_ FROM table_name WHERE -criteria-';
-> Execute your query
-> Label1.caption := Decrypt(__Field_Value__, your_key) ;

Hope that I could be of any help to you

Emad
dear emadat,
I test the encryption and decryption will and work good,
the case is,
I need to use OnSetText and OnGetText events becasue i display the data in a DBGrid.
i want to make a encryption and decryption once when i insert and update or delete.
if possible.
Thanks
RAGAB2000:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to split points between kretzschmar (20) and emadat (30)
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup Volunteer