?
Solved

Bad encryption

Posted on 2002-07-18
2
Medium Priority
?
552 Views
Last Modified: 2010-04-04
Dear Experts, please help !
     Please have a look at the program, and tell me what is wrong.

Thanks from apin.

ps : I'm using D5 Enterprise and Windows ME

Function Encrypt(S : String ; Sign : Integer ;  Key : Word ) : String ;
Const
  C1  = 52845 ;
  C2  = 22719 ;

Var
  I : BYTE ;
  x : string ;
  k : Integer ;

Begin
  x := S ;
  IF Length(s) = 0 Then Begin
     Result := '' ;
     Exit  ;  
  End ;
  For I := 1 To Length(S) Do Begin
     x[I] := Char(Byte(x[I]) xor ( Key Shr 8 )) ;
     Case Sign OF
       1 : Key := ( Byte(x[I]) + Key ) * C1 + C2 ;  // encrypt
       2 : Key := ( Byte(s[I]) + Key ) * C1 + C2 ;  // Decrypt
     End ; // Case
  End ;  // For
  Result := x ;
End ;


procedure TForm1.Button1Click(Sender: TObject);
Var
  S1, S2 : String ;
begin
  // bad
  S1 := Encrypt('00000',1,12345) ; // encrypt
  S2 := Encrypt(S1,2,12345) ;      // decrypt
  ShowMessage( S1 +  ' ' + S2 ) ;  // Not OK !!!

  // good, when I change the Key value >= 20000
  S1 := Encrypt('00000',1,20000) ;
  S2 := Encrypt(S1,2,20000) ;      
  ShowMessage( S1 +  ' ' + S2 ) ;  // OK

  S1 := Encrypt('100000',1,12345) ;
  S2 := Encrypt(S1,2,12345) ;      
  ShowMessage( S1 +  ' ' + S2 ) ;   // OK

  S1 := Encrypt('Good day',1,222) ;
  S2 := Encrypt(S1,2,222) ;      
  ShowMessage( S1 +  ' ' + S2 ) ;   // OK
end;

0
Comment
Question by:pc_melsa
[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
2 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 800 total points
ID: 7162964
pc_melsa,

Firstly, thanks for this easy-to-inspect sample. I love when problem
is reduced to code sample like this. Helping you is *much* easier this
way.

OK, on to your question...
Well, actually you don't really have a problem. To quickly see what I mean,
just replace line:

  ShowMessage( S1 +  ' ' + S2 ) ;  // Not OK !!!

with:

  ShowMessage( 'Encrypted:'+S1); ShowMessage('Decrypted:'+S2);

See?


Your function is correctly encrypting string, but it happened that, for
this particular S/Sign combination, the first letter (ie. '0', or Chr(48)) in
your string S is encrypted as Chr(0).

Why? Simple:

x='00000'   ->  Byte(x[1]) = 48

Key=12345   ->  Key Shr 8 = 48

So,

x[1]= Byte(x[1]) xor  (Key Shr 8) = 48 xor 48 = 0


As you probably know, Chr(0) is interpreted by wast majority of string
functions as termination character.
So, ShowMessage actually sees S1 as empty string, although it really isn't,
as you can easily see by placing a break line at ShowMessage(), and inspecting
S1. As a conseqence, string S1+' '+S2 is also saw as empty string.
If you're not quite clear on this, tell me and I'll try to explain it further.

You have to worry about all this only if you really want to print your
encoded string (I don't see why would you  want this, though, because your
encoded string will almost certanly contain nonprintable characters, and you'll
end up with unpredictable results most of the time...).

But...if you'd like to solve this anyway, one way is to simply replace Chr(0)
in encrypted string with some printable character.

For example, you could add this function:

function DisplayableString(s: String): String;
var i: Integer; // Replace termination char with 'X'
begin
  Result:=s;
  for i := 1 To length(s) do
      if Result[i]=#0 then Result[i]:='X'
end;

And replace line:

  ShowMessage( S1 +  ' ' + S2 ) ;  // Not OK !!!

with:

  ShowMessage( DisplayableString(S1)+  ' ' + S2 ) ;  // OK after all...

0
 

Author Comment

by:pc_melsa
ID: 7163659
Thanks for your clear explanation, May God Bless you !
0

Featured Post

Industry Leaders: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

649 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