01.#include <stdio.h>
02.#include <stdlib.h>
03.#include <string.h>
04. 
05. 
06.unsigned short table[256];
07.int FillTable(int a, short b, int c)
08.{
09.    int d = a << 8;
10.    for ( int i = 8; i > 0 ; --i)
11.    {
12.        if ( (c ^ d) & 0x8000)
13.        {
14.            c = b ^ (2 * c);
15.        }
16.        else
17.        {
18.            c *= 2;
19.        }
20.        d *= 2;
21.    }
22.    return c;
23.}
24. 
25.void decrypt(unsigned char *buf, int len, int key)
26.{
27.    for(int i=0;i<len;i++)
28.    {
29.        unsigned short temp = table[buf[i] ^ (key>>8) ] ^ (key<<8);
30.        buf[i] = buf[i] ^ key;
31.        key = temp;
32.    }
33.}
34.int main(int argc,char *argv[])
35.{
36.    if(argc<2)
37.    {
38.        printf("使用方法:GetGhoPwd.exe ghost.gho");
39.    }
40.    else
41.    {
42.        //
43.        for (int i=0;i<256;i++)
44.        {
45.            table[i] = FillTable(i, 4129, 1954);
46.        }
47.        
48.        //
49.        FILE * fp = fopen(argv[1],"rb");
50.        if(fp)
51.        {
52.            fseek(fp, 11, SEEK_SET);
53.            char flag = fgetc(fp);
54.            if(flag)
55.            {
56.                unsigned char encrypted1[15];
57.                unsigned char encrypted2[10];
58.                fread(encrypted1,1,15,fp);
59.                fread(encrypted2,1,10,fp);
60.                
61.                //
62.                int key = 0;
63.                while(1)
64.                {
65.                    unsigned char temp[15];
66.                    memcpy(temp, encrypted1, 15);
67.                    
68.                    decrypt(temp, 15, key);
69.                    
70.                    if( memcmp(temp,"BinaryResearch",15) ==0 )
71.                    {
72.                        break;
73.                    }
74.                    
75.                    key++;
76.                }
77.                
78.                //
79.                decrypt(encrypted2, 10, key);
80.                printf("此文件的密码是:%s", encrypted2);
81.            }
82.            else
83.            {
84.                printf("此文件没有密码保护。");
85.            }
86.            fclose(fp);
87.        }
88.    }
89.    getchar();
90.    return 0;
91.}C语言转Delphi

解决方案 »

  1.   


    program Project1;{$APPTYPE CONSOLE}uses
      Windows,
      Classes,
      SysUtils;var
      table: array[0..255] of Byte;function FillTable(a: integer; b: SmallInt; c: integer): integer;
    var
      i, d: integer;
    begin
      d := a shl 8;
      for i := 8 downto 1 do
      begin
        if (c xor d) and $8000 > 0 then
          c := b xor (2* c)
        else
          c := c *2;
        d := d *2;
      end;
      Result := c;
    end;procedure decrypt(buf: array of Char; len, key: integer);
    var
      i: integer;
      temp: Byte;
    begin
      for i := 0 to len - 1 do
      begin
        temp := table[Byte(buf[i]) xor (key shr 8)] xor (key shl 8);
        buf[i] := Char(Byte(buf[i]) xor key);
        key := temp;
      end;
    end;var
      i: Byte;
      Stream1: TMemoryStream;
      flag: ShortInt;
      encrypted1: array[0..14] of Char;
      encrypted2: array[0..9] of Char;
      key: integer;
      temp: array[0..14] of Char;
    begin
      { TODO -oUser -cConsole Main : Insert code here }
      if (ParamCount < 2) then
        Writeln('使用方法:GetGhoPwd.exe ghost.gho')
      else
      begin
        for i := 0 to 255 do
          table[i] := FillTable(i, 4129, 1954);    Stream1 := TMemoryStream.Create;
        try
          Stream1.LoadFromFile(ParamStr(1));
          Stream1.Seek(11, soFromBeginning);
          Stream1.Read(flag, 1);
          if Byte(flag) <> 0 then
          begin
            Stream1.Read(encrypted1[0], 15);
            Stream1.Read(encrypted2[0], 10);
            key := 0;
            while True do
            begin
              CopyMemory(@temp[0], @encrypted1[0], 15);
              decrypt(temp, 15, key);
              //if CompareMem(@temp[0], PChar('BinaryResearch'), 15) then
              if temp = 'BinaryResearch' then
                Break;          Inc(Key);
            end;
            //
            decrypt(encrypted2, 10, key);
            Writeln('此文件的密码是:' + encrypted2);
          end
          else
            Writeln('此文件没有密码保护。');
        finally
          Stream1.Free;
        end;
      end;
      ReadLn;
    end.
      

  2.   

    如果是需要使用delphi,就自己单独写一下,delphi编写也还是很容易的。
      

  3.   

    feiba7288能再帮忙看看吗?我把ParamCount < 2改成ParamCount < 1,因为这个程序只需要一个参数,这样下面的代码就能运行了,不过出错,错误代码如下:Exception EAccessViolation in module wqy.exe at 0004636B.Access violation at address 0044636B in module 'GetGhoPwd.exe'. Read of address 005E508E.
      

  4.   

    feiba7288能再帮忙看看吗?我把ParamCount < 2改成ParamCount < 1,因为这个程序只需要一个参数,这样下面的代码就能运行了,不过出错,错误代码如下:Exception EAccessViolation in module GetGhoPwd.exe at 0004636B.Access violation at address 0044636B in module 'GetGhoPwd.exe'. Read of address 005E508E. 
      

  5.   


    C的还是D的代码运行出错??如果是D的,单步调试看下是哪句出的错,可能我翻译有点粗糙。
      

  6.   

    出错的地方在这个循环里        while True do
            begin
              CopyMemory(@temp[0], @encrypted1[0], 15);
              decrypt(temp, 15, key);
              //if CompareMem(@temp[0], PChar('BinaryResearch'), 15) then
              if temp = 'BinaryResearch' then
                Break;          Inc(Key);
            end;
      

  7.   

    这些C代码本身就有问题,太多av错误。另外转到delphi这里也不应该用TMemoryStream,看这里“GetGhoPwd.exe ghost.gho'”的意思是要给ghost镜像文件加密的,gho文件一般都几百甚至几G大小,用 TMemoryStream  LoadFromFile 显然也有问题。还是用CreateFile 打开文件来 fileseek、fileread保险。
      

  8.   

    其实我也知道这个部分有问题,事实上只要读取GHO文件头信息即可,只是我不知道用Delphi如何读取文件头代码。
      

  9.   


    var 
    handF : DWORD;
    begin
    ....
    handF := CreateFile(PChar('c:\ghost.gho'), GENERIC_READ, 0, nil,
          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // 只读方式打开文件并返回文件句柄
    FileSeek(handF , 11, 0); // 移动第11个字节到文件头部  ,0 头部 ,1 当前位置,2尾部
    FileRead(handF , flag, 1);  // 读第1个字节到 flag 里
    ....// 问题是  table[i] := FillTable(i, 4129, 1954);  如何解释 4129 和 1954
    // 和  if( memcmp(temp,"BinaryResearch",15) ==0 ) 的 BinaryResearch 怎么来的
      

  10.   

    FillTable是一个解密函数。BinaryResearch是解密后生成的固定字符串,是内嵌在Gho文件里的