Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Resource entschlüsseln und laden (https://www.delphipraxis.net/26288-resource-entschluesseln-und-laden.html)

Uncle Cracker 21. Jul 2004 14:46


Resource entschlüsseln und laden
 
Ich habe folgenden Quelltext gefunden um eine WAV-Datei aus einer Resource zuladen und direkt abzuspielen ohne sie auf der Festplatte zwischen zuspeichern. Ich habe die WAV-Datei folgendermaßen verschlüsselt:

Delphi-Quellcode:
type
  TWordTriple = Array[0..2] of Word;

function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
  if SrcSize = TargetSize then
  begin
    pIn := Src;
    pOut := Target;
    for i := 1 to SrcSize do
    begin
      pOut^ := pIn^ xor (Key[2] shr 8);
      Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1];
      inc(pIn);
      inc(pOut);
    end;
    Result := True;
  end else
    Result := False;
end;

function FileCrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
var
  MIn, MOut: TMemoryStream;
begin
  MIn := TMemoryStream.Create;
  MOut := TMemoryStream.Create;
  Try
    MIn.LoadFromFile(InFile);
    MOut.SetSize(MIn.Size);
    Result := MemoryEncrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key)
    MOut.SaveToFile(OutFile);
  finally
    MOut.Free;
    MIn.Free;
  end;
end;

function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean;
begin
  Result := FileCrypt(InFile, OutFile, Key);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Key : TWordTriple;
begin
Key[0] := 25;
Key[1] := 11;
Key[2] := 1987;
FileEncrypt('C:\Test.wav','C:\Test.wav',Key)
end;
Das wäre die Funktion zum entschlüsseln:

Delphi-Quellcode:
function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean;
var
  pIn, pOut: ^byte;
  i : Cardinal;
begin
  if SrcSize = TargetSize then
  begin
    pIn := Src;
    pOut := Target;
    for i := 1 to SrcSize do
    begin
      pOut^ := pIn^ xor (Key[2] shr 8);
      Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1];
      inc(pIn);
      inc(pOut);
    end;
    Result := True;
  end else
    Result := False;
end;
Ich würde diese Funktion zum entschlüssen der Datei gern für diese Funktion zum laden und abspielen der WAV-Datei nutzten, aber irgendwie bekomme ich das nicht hin. Hier erstmal die Funktion zum laden und abspielen:

Delphi-Quellcode:
{$R wav.res}

var
  sample_wav: pointer;

function GetResourceAsPointer(ResName: pchar; ResType: pchar;
                              out Size: longword): pointer;
var
  InfoBlock: HRSRC;
  GlobalMemoryBlock: HGLOBAL;
begin
  InfoBlock := FindResource(hInstance, resname, restype);
  if InfoBlock = 0 then
    raise Exception.Create(SysErrorMessage(GetLastError));
  size := SizeofResource(hInstance, InfoBlock);
  if size = 0 then
    raise Exception.Create(SysErrorMessage(GetLastError));
  GlobalMemoryBlock := LoadResource(hInstance, InfoBlock);
  if GlobalMemoryBlock = 0 then
    raise Exception.Create(SysErrorMessage(GetLastError));
    Result := LockResource(GlobalMemoryBlock);
  if Result = nil then
    raise Exception.Create(SysErrorMessage(GetLastError));
end;

function GetResourceAsString(ResName: pchar; ResType: pchar): string;
var
  ResData: PChar;
  ResSize: Longword;
begin
  ResData := GetResourceAsPointer(resname, restype, ResSize);
  SetString(Result, ResData, ResSize);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  size: longword;
begin
  sample_wav := GetResourceAsPointer('Datei', 'WAVE', size);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound(sample_wav, SND_MEMORY or SND_NODEFAULT or SND_ASYNC);
end;
Ich bekomme es aber einfach nicht hin die Resource zuladen, zu entschlüsseln und dann weiter damit zuarbeiten (abspielen).

Hat vielleicht jemand eine Idee wie man das machen könnte? Muss ich die Datei in einem Stream entschlüsseln und dann den Pointer draufsetzten? Wenn JA, wie denn?


:love: Danke UC

Blutiger Anfänger 22. Aug 2004 00:18

Re: Resource entschlüsseln und laden
 
1. Sind DLLs im Spiel? Dann kann der Zugriff auf hInstance schiefgehen ...
2. Dein Algo funktioniert hin und zurück und verändert nicht die Größe der Daten?


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:51 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz