首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

何位能帮忙将下面C语言转换成Delphi代码

2013-04-07 
哪位能帮忙将下面C语言转换成Delphi代码?01.#include stdio.h02.#include stdlib.h03.#include strin

哪位能帮忙将下面C语言转换成Delphi代码?
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
[解决办法]


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.


[解决办法]

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 怎么来的

热点排行