Discussion:
Works in D6 not in XE2... any help is appreciated
(too old to reply)
bok
2012-06-16 04:49:16 UTC
Permalink
The following works fine in D6+Win7 but I get "Invalid buffer size for decryption" error under XE2+Win7 during decrypion(button2Click). A mozillla liscensed unit from 2001 that does the actual AES operations is raising that error, but I hesitate to include that unit here as it is hundreds of lines long,
but TAESKey128 = array [0..15] of byte is in the TYPE statement there.

I'm guessing that the problem is either a change in the "Size" "fillchar" pointer "^" or "move" operations between D6 and XE2 or the fact that I'm running a 64bit OS (Windows was 16 bit in 2001)
=======================================================================

function StringToHex(S: string): string; // Convert all characters to hex
var i: integer;
begin
Result := '';
for i := 1 to Length( S ) do Result := Result + IntToHex( Ord( S[i] ), 2 );
end;

function HexToString(S: string): string;
var i: integer;
begin
Result := '';
for i := 1 to Length( S ) do
if ((i mod 2) = 1) then
Result := Result + Chr( StrToInt( '0x' + Copy( S, i, 2 )));
end;

procedure TForm1.Button1Click(Sender: TObject);// Encryption
var
Source: TStringStream;
Dest: TStringStream;
Size: integer;
Key: TAESKey128;
begin
Source := TStringStream.Create( Memo1.Text );
Dest := TStringStream.Create( '' );
try
Size := Source.Size;
Dest.WriteBuffer( Size, SizeOf(Size) );
FillChar( Key, SizeOf(Key), 0 ); // Prepare key
Move( PChar(edit1.Text)^, Key, Min( SizeOf( Key ), Length( edit1.Text )));
EncryptAESStreamECB( Source, 0, Key, Dest );
Memo2.Text := StringToHex( Dest.DataString );
finally
Source.Free;
Dest.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Source: TStringStream;
Dest: TStringStream;
Size: integer;
Key: TAESKey128;
begin
Source := TStringStream.Create( HexToString( Memo2.Text ));
Dest := TStringStream.Create( '' );
try
Size := Source.Size;
Source.ReadBuffer(Size, SizeOf(Size));
FillChar(Key, SizeOf(Key), 0);
Move(PChar(edit1.Text)^, Key, Min(SizeOf(Key), Length(edit1.Text)));
DecryptAESStreamECB(Source, Source.Size - Source.Position, Key,Dest);
Memo1.Text := Dest.DataString;
finally
Source.Free;
Dest.Free;
end;
end;

Thanks for your help
DBM
Maarten Wiltink
2012-06-16 05:30:44 UTC
Permalink
Post by bok
The following works fine in D6+Win7 but I get "Invalid buffer size
for decryption" error under XE2+Win7 during decrypion(button2Click).
[...]
Post by bok
I'm guessing that the problem is either a change in the "Size"
"fillchar" pointer "^" or "move" operations between D6 and XE2 or
the fact that I'm running a 64bit OS (Windows was 16 bit in 2001)
Windows was 32 bits although it had a 16-bits Windows subsystem.
Delphi was and is 32 bits but I hear a 64 bits version is coming soon.

The difference is more likely in the string types. They became Unicode
between D6 and XE2, and characters went from one to two bytes.

Groetjes,
Maarten Wiltink
bok
2012-06-16 07:09:18 UTC
Permalink
Post by Maarten Wiltink
The difference is more likely in the string types. They became Unicode
between D6 and XE2, and characters went from one to two bytes.
Groetjes,
Maarten Wiltink
Maarten,

Thank you for your reply. I am not a professional and program only as a hobby. Can you tell me how to fix this code so that it will work (ascii vs. unicode)under XE2?

Thank You
Bok
S.G
2012-06-16 09:01:21 UTC
Permalink
Post by bok
Can you tell me how to fix this code so that it will
work (ascii vs. unicode)under XE2?
I tried to install XE2 evaluation package about one year ago, but never
got it to work. The first Delphi version, ever, that refused to compile
even a one Button test application.

So my XE2 expertise and even upgrading to All Unicode is quite thin. Yet
you could try to replace all you String types to AnsiString types, and
your problem at this specific place might go away.

http://edn.embarcadero.com/article/38437
"The default string in Delphi 2009 is the new UnicodeString type. By
default, the UnicodeString type will have an affinity for UTF-16, the
same encoding used by Windows. This is a change from previous versions
which had AnsiString as the default type."

S.G.
JJ
2012-06-16 15:39:17 UTC
Permalink
Post by bok
Maarten,
Thank you for your reply. I am not a professional and program only as
a hobby. Can you tell me how to fix this code so that it will work
(ascii vs. unicode)under XE2?
Thank You
Bok
Change all refererence of "string" variable type to "ansistring" in all of
your source codes including third-party ones that are not Delphi 2005+
aware.
Rudy Velthuis
2012-06-16 15:58:18 UTC
Permalink
Post by JJ
Post by bok
Maarten,
Thank you for your reply. I am not a professional and program only
as a hobby. Can you tell me how to fix this code so that it will
work (ascii vs. unicode)under XE2?
Thank You
Bok
Change all refererence of "string" variable type to "ansistring" in
all of your source codes
No, don't. On the contrary. Simple leave things as they are, recompile
and handle and warnings or errors you may get. Soon it will compile and
be fully Unicode.
--
Rudy Velthuis

"It's clearly a budget. It's got a lot of numbers in it."
-- George W. Bush
Marco van de Voort
2012-06-16 18:37:36 UTC
Permalink
Post by Rudy Velthuis
Post by JJ
Post by bok
Thank You
Bok
Change all refererence of "string" variable type to "ansistring" in
all of your source codes
No, don't. On the contrary. Simple leave things as they are, recompile
and handle and warnings or errors you may get. Soon it will compile and
be fully Unicode.
Strange. One of the glaring mistakes is a move(..,..,length(s)); that is
never going to fix itself.
Rudy Velthuis
2012-06-17 03:37:45 UTC
Permalink
Post by Marco van de Voort
Post by Rudy Velthuis
Post by JJ
Post by bok
Thank You
Bok
Change all refererence of "string" variable type to "ansistring" in
all of your source codes
No, don't. On the contrary. Simple leave things as they are,
recompile and handle and warnings or errors you may get. Soon it
will compile and be fully Unicode.
Strange. One of the glaring mistakes is a move(..,..,length(s)); that
is never going to fix itself.
Correct.

I assumed that people don't do this.

Of course any low level stuff like Move, FillChar, GetMem, FreeMem,
etc. must be looked at with suspicion. Just like any "clever" code that
uses strings for non-text binary data storage.
--
Rudy Velthuis

"You have a cough? Go home tonight, eat a whole box of Ex-Lax
-- tomorrow you'll be afraid to cough." -- Pearl Williams.
JJ
2012-06-17 04:44:52 UTC
Permalink
Post by Marco van de Voort
Post by Rudy Velthuis
Post by JJ
Post by bok
Thank You
Bok
Change all refererence of "string" variable type to "ansistring" in
all of your source codes
No, don't. On the contrary. Simple leave things as they are,
recompile
Post by Marco van de Voort
Post by Rudy Velthuis
and handle and warnings or errors you may get. Soon it will compile and
be fully Unicode.
Strange. One of the glaring mistakes is a move(..,..,length(s)); that is
never going to fix itself.
If converting to Unicode...
If a "length(s)" is being used as a byte count rather than a character
count, for a move statement, it should be changed to:
move(..., ..., length(s) * sizeof(char))
Rudy Velthuis
2012-06-16 15:58:11 UTC
Permalink
Post by Maarten Wiltink
Windows was 32 bits although it had a 16-bits Windows subsystem.
Delphi was and is 32 bits but I hear a 64 bits version is coming soon.
Have you been sleeping, the last few months? Delphi XE2 can compile
natively for Win32, Win64 and OS X targets, these days.
--
Rudy Velthuis

"For animals, the entire universe has been neatly divided into
things to (a) mate with, (b) eat, (c) run away from, and (d)
rocks."
-- Terry Pratchett (Equal Rites)
Ph. B.
2012-06-16 17:53:49 UTC
Permalink
Post by Rudy Velthuis
Post by Maarten Wiltink
Windows was 32 bits although it had a 16-bits Windows subsystem.
Delphi was and is 32 bits but I hear a 64 bits version is coming soon.
Have you been sleeping, the last few months? Delphi XE2 can compile
natively for Win32, Win64 and OS X targets, these days.
May be he was speaking about IDE which is until now only 32 bits... ;-)
--
Philippe.
Rudy Velthuis
2012-06-17 03:38:51 UTC
Permalink
Post by Ph. B.
Post by Rudy Velthuis
Post by Maarten Wiltink
Windows was 32 bits although it had a 16-bits Windows subsystem.
Delphi was and is 32 bits but I hear a 64 bits version is coming soon.
Have you been sleeping, the last few months? Delphi XE2 can compile
natively for Win32, Win64 and OS X targets, these days.
May be he was speaking about IDE which is until now only 32 bits... ;-)
Perhaps, but since this thread was about code posted, and not about the
IDE, I doubt it a bit.
--
Rudy Velthuis

"Go away...I'm alright." -- H.G.Wells, dying words
JJ
2012-06-17 04:48:41 UTC
Permalink
Post by Rudy Velthuis
Have you been sleeping, the last few months? Delphi XE2 can compile
natively for Win32, Win64 and OS X targets, these days.
He's busy at work, apparently. That's why he's still using Delphi 6 or at
least still keep it.
bok
2012-06-18 09:48:43 UTC
Permalink
Post by Rudy Velthuis
Have you been sleeping, the last few months? Delphi XE2 can compile
natively for Win32, Win64 and OS X targets, these days.
Not the "starter" edition
Post by Rudy Velthuis
If converting to Unicode...
If a "length(s)" is being used as a byte count rather than a character
move(..., ..., length(s) * sizeof(char))
Got it. Thank you JJ for your help. I am 'almost' a senior citizen and
except for a Fortran class 30 years ago I'm trying to learn this stuff
on my own.
Rudy Velthuis
2012-06-18 11:52:25 UTC
Permalink
Post by bok
Post by Rudy Velthuis
Have you been sleeping, the last few months? Delphi XE2 can
compile natively for Win32, Win64 and OS X targets, these days.
Not the "starter" edition
I don't know. I do know that the other editions do.
--
Rudy Velthuis

"When his life was ruined, his family killed, his farm
destroyed, Job knelt down on the ground and yelled up to the
heavens, 'Why god? Why me?' and the thundering voice of God
answered, 'There's just something about you that pisses me
off.'" -- Stephen King.
Maarten Wiltink
2012-06-26 21:06:13 UTC
Permalink
Have you been sleeping, the last few months? ...
Short answer: yes.

I don't have the time anymore to keep up with the Borland, no CodeGear,
no Embarcadero, newsgroups. Those who know the details also know why I
suffer this gladly.
He's busy at work, apparently. That's why he's still using Delphi 6 ...
Delphi 6? I wish.

One of these days I'm going to have to sabotage my ocmputer and claim
that it can only be fixed by buying a new, _recent_, Delphi.

At which point they may listen throw away the legacy application
instead.

Groetjes,
Maarten Wiltink

Loading...