?
Solved

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

Posted on 2003-02-21
12
Medium Priority
?
327 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
11 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

621 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