Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Its probally very basic what im doing wrong but I dont see it. 1

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
Hi all,

I've tried to write a function for light encrytion for
encrypting personal information (think of addresses, cc numbers etc) in my database. Now here is the problem, Im not getting them back !!!

(Maybe the encryption is better than I thought lol)

Very weird but I dont know why.

Down below you'll find the procedure I've written.
Str is the string that's going to be encrypted/decrypted.
Encrypt boolean value tells function wether it should
encrypt or decrypt.

What I do is I take a single char get its position in the
ASCII table and add a random number, and then change it back
to a char in the ASCII table. Then I make the first char of
the string the number I used to encrypt it with. Thats how
I encrypt it.

To decrypt it, I get the first char, change the first char
and change it back to a number. Then I do the same as before
only I subtract it.

Code:
function ChangeChars(Str: String; Encrypt: Boolean): String;
var UseNum, i: Integer;
begin
Usenum := 0; i := 0; Result := '';

if Encrypt then
        begin
        Randomize;
        UseNum := Round(Random(100)) + 22;
        for i := 0 to Length(Str) do
                begin
                Result := Result + Chr(Ord(Str[i]) + UseNum);
                end;
        Result := Chr(UseNum) + Result;
        end
else
        begin
        UseNum := Ord(Str[1]);
        Delete(Str,0,1);
        for i := 0 to Length(Str) do
                begin
                Result := Result + Chr(Ord(Str[i]) - UseNum);
                end;
        end;
Result := Result;
end;

I hope you guys can help me out, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Also, the function I use to get the number used to encrypt
it with does always get me the right number.

Greetz, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
You're working with strings. The index of a first characher is 1 not 0.

function ChangeChars(Str: String; Encrypt: Boolean): String;
var UseNum, i: Integer;
begin
Usenum := 0; i := 0; Result := '';
if Encrypt then
begin
Randomize;
UseNum := Round(Random(100)) + 22;
for i := 1 to Length(Str) do
begin
Result := Result + Chr(Ord(Str[ i ]) + UseNum);
end;
Result := Chr(UseNum) + Result;
end
else
begin
UseNum := Ord(Str[1]);
Delete(Str,1,1);
for i := 1 to Length(Str) do
begin
Result := Result + Chr(Ord(Str[ i ]) - UseNum);
end;
end;
Result := Result;
end;

--- markus

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top