?
Solved

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

Posted on 2003-02-21
12
Medium Priority
?
272 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:RAGAB2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 7992237
you can use the onsettext- and ongettext-events
of the needed tfields to en-/decrypt your fields

meikl ;-)
0
 
LVL 3

Assisted Solution

by:emadat
emadat earned 100 total points
ID: 7995204
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;
//========================================================
0
 
LVL 3

Expert Comment

by:emadat
ID: 7995297
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;
//========================================================
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RAGAB2000
ID: 8002482
i want to ask about the parameter sKey in the function encrypt and decrypt
0
 

Author Comment

by:RAGAB2000
ID: 8002638
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
0
 
LVL 3

Expert Comment

by:emadat
ID: 8004294
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
0
 

Author Comment

by:RAGAB2000
ID: 8008893
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;
//========================================================
 
0
 
LVL 3

Expert Comment

by:emadat
ID: 8009598
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
0
 

Author Comment

by:RAGAB2000
ID: 8009948
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
0
 

Expert Comment

by:CleanupPing
ID: 9317141
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.
0
 
LVL 10

Expert Comment

by:kacor
ID: 9947805
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question