bok
2012-06-16 04:49:16 UTC
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
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