Solved

Bad encryption

Posted on 2002-07-18
2
513 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
2 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 200 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now