Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2011, 04:39
TmC TmC is offline
VIP
 
Join Date: Aug 2004
Posts: 328
Rept. Given: 1
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 22 Times in 16 Posts
TmC Reputation: 15
Delphi + DCPCrypt 2 (RC6) vs RAD Studio + DCPCrypt 2 (RC6) = KABOOM!

Hi...

Just a question:

I've coded an application with delphi, that generates files encrypted with RC6, just as a security measure so the user must generate file through the program and not by hand with notepad.

Later on...i moved to RAD Studio XE and used exactly the same files (no modifications at all), to further develop the program to version 2.

Well...hard to say, but RC6 fails. Files generated by version 1 (delphi 7) are unreadable from version 2 (rad studio xe). From what i see, difference is not so important...it happens very rarely... here it is an example:

Plain String before encryption:
###BDB###R#G#Gx#V#RG#RGx#RV#GG#GV#GxVx#GxVy#RGG#RGV#RGxVx#RGxVy#R#G#Gx#V#RG#RGx#RV#GG#GV#GxVx#GxVy#RGG#RGV#RGxVx#RGxVy###EDB######BOB###3###EOB###

Plain String after decryption:
###BDB#oŽVÌ'#$0090'NŒ™^Z'#$11'‹.M™“#RV#GG#GV#GxVx#GxVy#RGG#RGV#RGxVx#RGxVy#R#G#Gx#V#RG#RGx#RV#GG#GV#GxVx#GxVy#RGG#RGV#RGxVx#RGxVy###EDB######BOB###3###EOB###

As you can see...something breaks in the middle...you can try with various other strings...and it happens the same...even multiple times if the string is long enough.

One thought came to my mind: Delphi 7 is ASCII while RAD Studio XE is UNICODE. That causes many problems and a new version of DPCrypt 2 has been released to address UNICODE issues...

I have even problems encrypting and decrypting with the same library on RAD Studio XE by itself...it encrypts but after decryption the result is the same of above.

So the question is:
a) is it normal that going unicode, i receive different results. If I try on delphi 7 everything is fine...on rad studio and between different versions of delphi the entire thing go mess.
b) what can be the problem? dpcrypt 2, unicode, rad studio xe or other things?
It's a month that I'm bashing my head on the pc without a solution...tried to change everything but this is the most close to unencrypted i got...

Thanks in advance to everyone...

(P.S. I can share the code if necessary, nothing secret)
Reply With Quote
  #2  
Old 10-20-2011, 05:07
Git's Avatar
Git Git is offline
Old Git
 
Join Date: Mar 2002
Location: Torino
Posts: 1,115
Rept. Given: 220
Rept. Rcvd 265 Times in 157 Posts
Thanks Given: 108
Thanks Rcvd at 216 Times in 124 Posts
Git Reputation: 200-299 Git Reputation: 200-299 Git Reputation: 200-299
UNICODE is the first thing that came to my mind too.

Git
Reply With Quote
  #3  
Old 10-20-2011, 06:26
TmC TmC is offline
VIP
 
Join Date: Aug 2004
Posts: 328
Rept. Given: 1
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 22 Times in 16 Posts
TmC Reputation: 15
What drives me absolutely MAD is that it is QUITE correct...but not ALL correct.

The difference between the before and after is minimal...

###BDB###R#G#Gx#V#RG#RGx#RV#

###BDB#oŽVÌ'#$0090'NŒ™^Z'#$11'‹.M™“#RV#

only 16 digits...so 128 bits?

It's so nonsense...it seems like one round fails and the others don't.

I can't even think about a possible solutions...if the sha1 key was incorrect, the entire decryption should be incorrect but i find miself with partial correct decryption and partial incorrect decryption...and that's crazy...

I looked everywhere, but everything is set to take AnsiString...so it is correct. Are there other entities that changed from Ansi to Unicode?
Reply With Quote
  #4  
Old 10-20-2011, 07:08
zementmischer's Avatar
zementmischer zementmischer is offline
Don't mess with concrete
 
Join Date: Mar 2011
Location: Europe
Posts: 216
Rept. Given: 124
Rept. Rcvd 490 Times in 111 Posts
Thanks Given: 12
Thanks Rcvd at 99 Times in 33 Posts
zementmischer Reputation: 400-499 zementmischer Reputation: 400-499 zementmischer Reputation: 400-499 zementmischer Reputation: 400-499 zementmischer Reputation: 400-499
I've heard that the type 'Char' has been changed from 'AnsiChar' (8bit) to 'WideChar' (16bit) as well. Perhaps your RC6 algo uses this type.
__________________
Real programmers don't read manuals.
Reliance on a reference is a hallmark of the novice and the coward.
Reply With Quote
  #5  
Old 10-20-2011, 08:43
TmC TmC is offline
VIP
 
Join Date: Aug 2004
Posts: 328
Rept. Given: 1
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 22 Times in 16 Posts
TmC Reputation: 15
there is no "Char" use, but there is a function called "FillChar" that may be causing problems. I don't know how to replace and Embarcadero tips don't work.

I'm currently reviewing for the 100th time all the code and found out that the Decrypt routine does not use FillChar, so it should be safe. The problems may arise in the Initialisation of the Key or in the Creation of the Cipher (Cipher:= TDCP_rc6.Create(nil); Cipher.InitStr(KeyStr,TDCP_sha1) so I'm investigating this way...

UPDATE1: Cipher:= TDCP_rc6.Create(nil); does nothing else than filling KeyData (which is an array of DWord) with $FF and IV and CV (which are array of byte) again with $FF. So unless there is something else wrong, it shouldn't be the culprit...

Last edited by TmC; 10-20-2011 at 08:53.
Reply With Quote
  #6  
Old 10-20-2011, 10:28
TmC TmC is offline
VIP
 
Join Date: Aug 2004
Posts: 328
Rept. Given: 1
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 22 Times in 16 Posts
TmC Reputation: 15
UPDATE2: I think i got a hold on the whole damn thing... The problem is the following(yet to disclose how to fix it though...):

a) if I load the encrypted text from a file into a string and decrypt that string, the whole damn thing breaks.
b) if I load the encrypted text from a constant string hardcoded into rad studio xe, the whole damn thing breaks.
c) if I load the encrypted text from a Memo.Text and execute RC6Decrypt(Memo1.Text), THE WHOLE DAMN THING WORKS AND DECRYPTS SUCCESFULLY.

So...I am now trying to understand why loading it in a textbox makes it succeed while loading from a string it kills himself...

Worse...if i load the encrypted text into a string, and then send it to Memo, the thing FAILS, if i copy from the encrypted file the binary data and paste it directly into memo, the thing WORKS...

Does this make any sense?
Reply With Quote
  #7  
Old 10-20-2011, 15:19
sendersu sendersu is offline
VIP
 
Join Date: Oct 2010
Posts: 1,067
Rept. Given: 332
Rept. Rcvd 223 Times in 115 Posts
Thanks Given: 235
Thanks Rcvd at 513 Times in 288 Posts
sendersu Reputation: 200-299 sendersu Reputation: 200-299 sendersu Reputation: 200-299
TmC
attach your sample Delphi project here please
the chances will raise once people will take a look over
Reply With Quote
  #8  
Old 10-20-2011, 15:44
Kerlingen Kerlingen is offline
VIP
 
Join Date: Feb 2011
Posts: 324
Rept. Given: 0
Rept. Rcvd 276 Times in 98 Posts
Thanks Given: 0
Thanks Rcvd at 309 Times in 96 Posts
Kerlingen Reputation: 200-299 Kerlingen Reputation: 200-299 Kerlingen Reputation: 200-299
My first guess would be that you have an alignment problem. Delphi 7 stores a LongInt as 32 bit, while current Delphi versions align each LongInt to 64 bit (it still only contains 32 bit). There are many possible errors when using incorrect memory addressing. Or it could be inncorrect initialisation of IV when using CBC mode, but the offsets don't really match in the strings you have posted. Or you could start encryption at String[0] and encrypt the string length together with the data.

Could you share the code (include variable declaration)? Or could you share at least some test EXE which includes the encryption code?

Did you already try ASM debugging your code?
Reply With Quote
  #9  
Old 10-22-2011, 03:49
TmC TmC is offline
VIP
 
Join Date: Aug 2004
Posts: 328
Rept. Given: 1
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 22 Times in 16 Posts
TmC Reputation: 15
OK...the problem is partly solved. Partly because one application begun to work perfeclty even under rad studio XE, while the other fails exactly as beginning. here i paste code from the first and the second:

WORKING APP (previously not working)

Code:
procedure TMainForm.BitBtn4Click(Sender: TObject);
var
  content, instr, opt: AnsiString;
  open: File;
begin
slist.Clear;
OD.DefaultExt := '.sif';
OD.Filter := 'Signatrol Instruction File|*.sif';
OD.Title := 'Please specify a valid Instruction File to load';
while OD.FileName = '' do
begin
OD.Execute;
try
begin
AssignFile(open,OD.FileName);
FileMode := fmOpenRead;
Reset(open,1);
SetLength(content,FileSize(open));
BlockRead(open,content[1],Length(content));
CloseFile(open);
end
except
Messagebox(Application.Handle,'Error while opening the file. Please check that the file exists and it is a valid Signatrol Instruction File and try again!','Severe',MB_ICONERROR);
Exit;
end;
end;
if Copy(content,1,10) <> 'SIGNATROLV' then
begin
Messagebox(Application.Handle,'This is not a valid Signatrol Instruction File. Please specify a valid Signatrol Instruction File and try again!','Severe',MB_ICONERROR);
Exit;
end;
OD.FileName := '';
content := Copy(content,Pos('$',content)+1,Length(content));
content := RC6Decrypt(content,'SIGNATROLV1183919102011');
if Copy(SHA1(Copy(content,1,Pos('###BSB###',content)-1)),1,40) <> Copy(content,Pos('###BSB###',content)+9,40) then
begin
Messagebox(Application.Handle,'File Corrupted! This file was damaged by a Virus or a Bad Sector and can''t be used anymore. Please specify another file try again!','Severe',MB_ICONERROR);
Exit;
end;
content := Copy(content,1,Pos('###BSB###', content) -1);
instr := Copy(content,1,Pos('###EDB###',content)+8);
opt := Copy(content,Pos('###BOB###',content),Length(content));
instr := StringReplace(instr,'###BDB###','',[rfReplaceAll]);
instr := StringReplace(instr,'###EDB###','',[rfReplaceAll]);
while Pos('#',instr) <> 0 do
begin
slist.AddItem(Copy(instr,1,Pos('#',instr)-1),nil);
instr := Copy(instr,Pos('#',instr)+1,Length(instr));
end;
slist.AddItem(instr,nil);
opt := StringReplace(opt,'###BOB###','',[rfReplaceAll]);
opt := StringReplace(opt,'###EOB###','',[rfReplaceAll]);
txtSpanS.Text := opt;
end;
NOT WORKING APP

Code:
function TLIC.intCheckLicKey(actcode: AnsiString): boolean;
var
  licdata: array of AnsiString;
  tmp, xx: String;
  ok: boolean;
  r, s, generator : AnsiString;
  p, q, g, y: TBIGInt;
  z,i: integer;
begin
actcode := licdata[0] + #13#10 + licdata[1] + #13#10 + licdata[2] + #13#10 + licdata[3] + #13#10 + licdata[4] + #13#10 + licdata[5] + #13#10 + licdata[6] + #13#10 + licdata[7] + #13#10 + licdata[8] + #13#10 + licdata[9] + #13#10 + licdata[10] + #13#10 + licdata[14];
s := licdata[11] + licData[12];
s := StringReplace(s,'-','',[rfReplaceAll]);
ConvertHexStringToBase256String(s,s);
r := intRC6Decrypt(Base64DecodeStr(licdata[13]),intCertificate.LSSigKeySeed); <--- I BELIEVE THIS IS THE CAUSE OF THE FAILURE
Base10StringToBIGInt(intCertificate.LSSigKeyQ,q);
Base10StringToBIGInt(intCertificate.LSSigKeyP,p);
Base10StringToBIGInt(intCertificate.LSSigKeyG,g);
Base10StringToBIGInt(intCertificate.LSSigKeyY,y);
DSAVerify(p, q, g, y, actcode, r, s, ok);
if ok then
begin
//GOOD BOY
end
else
begin
//BAD BOY
end;
The actual DSAVerify works very good, because if I replace the decrypted (partially incorrectly) from the memory loading from an external textbox (like told upper in this thread) it returns true. The same exact code in Delphi 7 works, while in Rad Studio XE does not.
Reply With Quote
  #10  
Old 10-22-2011, 16:41
Kerlingen Kerlingen is offline
VIP
 
Join Date: Feb 2011
Posts: 324
Rept. Given: 0
Rept. Rcvd 276 Times in 98 Posts
Thanks Given: 0
Thanks Rcvd at 309 Times in 96 Posts
Kerlingen Reputation: 200-299 Kerlingen Reputation: 200-299 Kerlingen Reputation: 200-299
You define licdata as a variable, but you never initialize it with any value. Did this happen when you copy&pasted the code to publish it here or does you source code really use the variable without initializing it?

RC6 is a block chiper, but I don't see a "length" field anywhere. Are you sure the block your are passing has the correct length?

You use intRC6Decrypt in one proc, but RC6Decrypt in the other. Did you check if both produce the same output using the same input data?
Reply With Quote
  #11  
Old 10-22-2011, 22:46
TmC TmC is offline
VIP
 
Join Date: Aug 2004
Posts: 328
Rept. Given: 1
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 22 Times in 16 Posts
TmC Reputation: 15
Quote:
Originally Posted by Kerlingen View Post
You define licdata as a variable, but you never initialize it with any value. Did this happen when you copy&pasted the code to publish it here or does you source code really use the variable without initializing it?
licData is a simple array of string.

Quote:
Originally Posted by Kerlingen View Post
RC6 is a block chiper, but I don't see a "length" field anywhere. Are you sure the block your are passing has the correct length?
I don't know this...the code for RC6 is not mine it is from DCPCrypt2...I am only passing him a string to be decrypted...I believe DCPCrypt code handles the whole job.

Quote:
Originally Posted by Kerlingen View Post
You use intRC6Decrypt in one proc, but RC6Decrypt in the other. Did you check if both produce the same output using the same input data?
This is because it is not the same program. The name of the function is different but the code is the same. I added "int" to the name of the function just because in the first case the function is publicly advertised to be used in the program while in the second it is only used internally in the Licensing module and can't be used outside it.
Reply With Quote
  #12  
Old 10-23-2011, 01:44
Kerlingen Kerlingen is offline
VIP
 
Join Date: Feb 2011
Posts: 324
Rept. Given: 0
Rept. Rcvd 276 Times in 98 Posts
Thanks Given: 0
Thanks Rcvd at 309 Times in 96 Posts
Kerlingen Reputation: 200-299 Kerlingen Reputation: 200-299 Kerlingen Reputation: 200-299
Quote:
Originally Posted by TmC View Post
licData is a simple array of string.
I can see this. The problem is that licdata is never assigned any value. You use licdata[0], licdata[1], ... licdata[14] in your code, but the array doesn't contain anything, it is just completely empty.

You also use actcode := in your code, but actcode is no variable parameter. You overwrite it before evaluating the value which was passed to the procedure.

Quote:
I don't know this...the code for RC6 is not mine it is from DCPCrypt2...I am only passing him a string to be decrypted...I believe DCPCrypt code handles the whole job.
Neither DCPcrypt nor DCPcrypt2 have a function called RC6Decrypt. They just have a RC6 class containing a procedure called DecryptECB which decrypts exactly one block, nothing more or less. You must be using some wrapper for DCPcrypt.

It also looks much like you're using FGInt and some prankster renamed it to BIGInt. (refactoring )
Reply With Quote
  #13  
Old 10-23-2011, 09:27
TmC TmC is offline
VIP
 
Join Date: Aug 2004
Posts: 328
Rept. Given: 1
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 22 Times in 16 Posts
TmC Reputation: 15
Quote:
Originally Posted by Kerlingen View Post
I can see this. The problem is that licdata is never assigned any value. You use licdata[0], licdata[1], ... licdata[14] in your code, but the array doesn't contain anything, it is just completely empty.

You also use actcode := in your code, but actcode is no variable parameter. You overwrite it before evaluating the value which was passed to the procedure.
Now I understand what you were saying. licdata[] IS initialised with values...the assignment is not reported to not prolong the code too much... you have a PM with real routine.

Quote:
Originally Posted by Kerlingen View Post
Neither DCPcrypt nor DCPcrypt2 have a function called RC6Decrypt. They just have a RC6 class containing a procedure called DecryptECB which decrypts exactly one block, nothing more or less. You must be using some wrapper for DCPcrypt.
Nope...I use the code suggested to decrypt or encrypt a string in the html help files bundled with the control. The code is not part of DCPCrypt 2 itself but it is suggested in the help file... (i did copy/paste...and it works perfectly in other delphi versions prior to unicode ones).

Quote:
Originally Posted by Kerlingen View Post
It also looks much like you're using FGInt and some prankster renamed it to BIGInt. (refactoring )
The BIGInt is only FGint renamed to fool some automatic scanners.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



All times are GMT +8. The time now is 10:20.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( 1998 - 2024 )