can anybody even check my code and see whats wrong??
the function CryptUnprotectData always Returns 'The Data is Invalid' :S
Code:
program Project2;
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils,
Dialogs;
const
SEED_CONSTANT = $BA0DA71D;
SecretKey : Array[0..15] Of Byte = ($A3,$1E,$F3,$69,
$07,$62,$D9,$1F,
$1E,$E9,$35,$7D,
$4F,$D2,$7D,$48);
type
TCharArray = Array[0..1023] Of Char;
_TOKEN_USER = record
User: SID_AND_ATTRIBUTES;
end;
TOKEN_USER = _TOKEN_USER;
TTokenUser = TOKEN_USER;
PTokenUser = ^TOKEN_USER;
_CREDENTIAL_ATTRIBUTEA = record
Keyword: LPSTR;
Flags: DWORD;
ValueSize: DWORD;
Value: PBYTE;
end;
PCREDENTIAL_ATTRIBUTE = ^_CREDENTIAL_ATTRIBUTEA;
_CREDENTIALA = record
Flags: DWORD;
Type_: DWORD;
TargetName: LPSTR;
Comment: LPSTR;
LastWritten: FILETIME;
CredentialBlobSize: DWORD;
CredentialBlob: PBYTE;
Persist: DWORD;
AttributeCount: DWORD;
Attributes: PCREDENTIAL_ATTRIBUTE;
TargetAlias: LPSTR;
UserName: LPSTR;
end;
PCREDENTIAL = array of ^_CREDENTIALA;
_CRYPTPROTECT_PROMPTSTRUCT = record
cbSize: DWORD;
dwPromptFlags: DWORD;
hwndApp: HWND;
szPrompt: LPCWSTR;
end;
PCRYPTPROTECT_PROMPTSTRUCT = ^_CRYPTPROTECT_PROMPTSTRUCT;
_CRYPTOAPI_BLOB = record
cbData: DWORD;
pbData: PBYTE;
end;
DATA_BLOB = _CRYPTOAPI_BLOB;
PDATA_BLOB = ^DATA_BLOB;
function CryptUnprotectData(pDataIn: PDATA_BLOB; ppszDataDescr: PLPWSTR; pOptionalEntropy: PDATA_BLOB; pvReserved: Pointer; pPromptStruct: PCRYPTPROTECT_PROMPTSTRUCT; dwFlags: DWORD; pDataOut: PDATA_BLOB): BOOL; stdcall; external 'crypt32.dll' Name 'CryptUnprotectData';
function Decode(Output: TCharArray; PassEntry: TCharArray; EntryLen: DWORD): Boolean;
var
Ret : Integer;
hToken : DWORD;
SID,
Name,
Domain : Array[0..511] Of Char;
SIDSize,
I,
J : DWORD;
CCHName,
CCHDomain : DWORD;
PEUse : SID_NAME_USE;
SIDUser : PTokenUser;
StaticKey : TByteArray;
Seed : DWORD;
A, B : PByteArray;
DataIn,
DataEntropy,
DataOut : DATA_BLOB;
begin
Ret := 0;
SIDSize := 0;
I := 0;
J := 0;
SIDUser := PTokenUser(@SID);
Move(SecretKey,StaticKey,SizeOf(SecretKey));
If OpenProcessToken(GetCurrentProcess,TOKEN_QUERY,hToken) Then
begin
If GetTokenInformation(hToken,TokenUser,SIDUser,SizeOf(SID),SIDSize) Then
begin
CCHName := SizeOf(Name);
CCHDomain := SizeOf(Domain);
If LookupAccountSID(nil,SIDUser.User.Sid,Name,CCHName,Domain,CCHDomain,PEUse) Then
begin
Seed := SEED_CONSTANT;
For I := 0 To CCHName - 1 Do
begin
StaticKey[I MOD 4] := StaticKey[I MOD 4] XOR (Byte(Name[I]) * Seed);
Seed := Seed * 48271;
end;
For J := 0 To CCHDomain - 1 Do
begin
StaticKey[I MOD 4] := StaticKey[I MOD 4] XOR (Byte(Domain[J]) * Seed);
Seed := Seed * 48271;
Inc(I);
end;
Seed := StaticKey[0] OR 1;
A := PByteArray(@PassEntry[4]);
B := PByteArray(@PassEntry[5]);
I := 0;
While I < EntryLen Do
begin
{$WARNINGS OFF}
PassEntry[I div 2] := Char((((A[I] - 1) * 16) Or (B[I] - 33)) - (Seed AND $FF));
{$WARNINGS ON}
Seed := Seed * 69621;
Inc(I,2);
end;
DataEntropy.cbData := SizeOf(SecretKey);
DataEntropy.pbData := @StaticKey;
DataIn.cbData := I div 2;
DataIn.pbData := @PassEntry;
If CryptUnprotectData(@DataIn,nil,@DataEntropy,nil,nil,1,@DataOut) Then
begin
Move(DataOut.pbData,Output,DataOut.cbData);
Output[DataOut.cbData] := #0;
LocalFree(DWORD(Pointer(DataOut.pbData)));
Ret := 1;
end
Else
begin
ShowMessage(SysErrorMessage(GetLastError));
end;
end
Else
begin
ShowMessage(SysErrorMessage(GetLastError));
end;
end
Else
begin
ShowMessage(SysErrorMessage(GetLastError));
end;
CloseHandle(hToken);
end
Else
begin
ShowMessage(SysErrorMessage(GetLastError));
end;
Result := Boolean(Ret);
end;
var
PWD : TCharArray;
aOut : TCharArray;
Param : String;
P : PChar;
I : Integer;
begin
If ParamCount < 1 Then Exit;
Param := GetCommandLine;
Param := Copy(Param,Pos('" ',Param) + 2,Length(Param));
Move(Param[1],PWD[0],Length(Param));
For I := 0 To High(PWD) Do
begin
If (PWD[I] = #13) Or (PWD[I] = #10) Then PWD[I] := #0;
end;
If Decode(aOut,PWD,Length(Param)) Then
begin
Writeln(String(aOut));
end
Else
begin
Writeln('Error');
end;
Readln;
end.