想对软件加密

解决方案 »

  1.   

    拷贝些资料 供你参考吧:
    function GetHDNumber(Drv : String): DWORD; //得到硬盘序列号
    var
      VolumeSerialNumber : DWORD;
      MaximumComponentLength : DWORD;
      FileSystemFlags : DWORD;
    begin
      if Drv[Length(Drv)] =':' then Drv := Drv + '\';
      GetVolumeInformation(pChar(Drv),
                           nil,
                           0,
                           @VolumeSerialNumber,
                           MaximumComponentLength,
                           FileSystemFlags,
                           nil,
                           0);
      Result:= (VolumeSerialNumber);end;function Serial(Num:DWORD):string;  //这个号码是用户给你生成注册码的,它通过对硬盘序列号编码而来。
    var sNum:string;  inChar:array[1..4]of char;
    begin  Num:=Num xor 8009211011;
      sNum:=inttostr(Num);
      inChar[1]:=char(((integer(sNum[1])+integer(sNum[2]))mod 5)+integer('a'));
      inChar[2]:=char(((integer(sNum[3])+integer(sNum[4]))mod 5)+integer('a'));
      inChar[3]:=char(((integer(sNum[5])+integer(sNum[6]))mod 5)+integer('a'));
      inChar[4]:=char(((integer(sNum[7])+integer(sNum[8])+integer(sNum[9]))mod 5)+integer('a'));
      insert(inChar[1],sNum,1);
      insert(inChar[4],sNum,3);
      insert(inChar[2],sNum,5);
      insert(inChar[3],sNum,9);
      Result:=sNum;end;function encode(License:string):string;
    var str,sNum:string;   number:dword; byte,byte1:array[1..4]of dword;
        inChar:array[1..3]of char;
    begin
      str:=license;
      delete(str,1,1);
      delete(str,2,1);
      delete(str,3,1);
      delete(str,6,1);
      number:=strtoint64(str);
      number:=not number;
      number:=number xor $1973122980;
      byte[1]:=(number and $0ff000000) shr 24;
      byte[2]:=(number and $0ff0000) shr 16;
      byte[3]:=(number and $0ff00) shr 8;
      byte[4]:=(number and $0ff);  byte1[1]:=((byte[1]and $0c0)+(byte[2]and $0c0)shr 2)+((byte[3]and $0c0)shr 4)+((byte[4]and $0c0)shr 6);
      byte1[2]:=((byte[1]and $30)shl 2)+(byte[2]and $30)+((byte[3]and $30)shr 2)+((byte[4]and $30)shr 4);
      byte1[3]:=((byte[1]and $0c)shl 4)+((byte[2]and $0c)shl 2)+(byte[3]and $0c)+((byte[4]and $0c)shr 2);
      byte1[4]:=((byte[1]and $03)shl 6)+((byte[2]and $03)shl 4)+((byte[3]and $03)shl 2)+(byte[4]and $03);
      number:=((byte1[1])shl 24)+((byte1[2])shl 16)
                 +((byte1[3])shl 8)+(byte1[4]);  byte[1]:=((number and $0ff000000)shr 24);//右移24位
      byte[2]:=((number and $0ff0000)shr 16);
      byte[3]:=((number and $0ff00)shr 8);
      byte[4]:=(number and $0ff);  byte[1]:=(((byte[1] and $f0))shr 4)+(((byte[1] and $0f))shl 4);
      byte[2]:=(((byte[2] and $f0))shr 4)+(((byte[2] and $0f))shl 4);
      byte[3]:=(((byte[3] and $f0))shr 4)+(((byte[3] and $0f))shl 4);
      byte[4]:=(((byte[4] and $f0))shr 4)+(((byte[4] and $0f))shl 4);  number:=((byte[2])shl 24)+((byte[1])shl 16)
                 +((byte[4])shl 8)+(byte[3]);  sNum:=inttostr(Number);
      inChar[1]:=char(((integer(sNum[1])+integer(sNum[2]))mod 5)+integer('a'));
      inChar[2]:=char(((integer(sNum[3])+integer(sNum[4]))mod 5)+integer('a'));
      inChar[3]:=char(((integer(sNum[5])+integer(sNum[6]))mod 5)+integer('a'));
      insert(inChar[1],sNum,1);
      insert(inChar[2],sNum,5);
      insert(inChar[3],sNum,9);
      result:=sNum;
    end;function decode(code:string):dword;
    var str:string; number:dword; byte,byte1:array[1..4]of dword;
    begin  str:=code;
      delete(str,1,1);
      delete(str,4,1);
      delete(str,7,1);
      number:= strtoint64(str);
      byte[1]:=(number and $0ff000000) shr 24;
      byte[2]:=(number and $0ff0000) shr 16;
      byte[3]:=(number and $0ff00) shr 8;
      byte[4]:=(number and $0ff); ////0123 --> 1032
     byte[1]:=(((byte[1] and $f0))shr 4)+(((byte[1] and $0f))shl 4);
     byte[2]:=(((byte[2] and $f0))shr 4)+(((byte[2] and $0f))shl 4);
     byte[3]:=(((byte[3] and $f0))shr 4)+(((byte[3] and $0f))shl 4);
     byte[4]:=(((byte[4] and $f0))shr 4)+(((byte[4] and $0f))shl 4); number:=((byte[2])shl 24)+((byte[1])shl 16)
            +((byte[4])shl 8)+(byte[3]); byte[1]:=((number and $0ff000000)shr 24);//右移24位
     byte[2]:=((number and $0ff0000)shr 16);
     byte[3]:=((number and $0ff00)shr 8);
     byte[4]:=(number and $0ff); byte1[1]:=(byte[1]and $0c0)+((byte[2]and $0c0)shr 2)+((byte[3]and $0c0)shr 4)+((byte[4]and $0c0)shr 6);
     byte1[2]:=((byte[1]and $30)shl 2)+(byte[2]and $30)+((byte[3]and $30)shr 2)+((byte[4]and $30)shr 4);
     byte1[3]:=((byte[1]and $0c)shl 4)+((byte[2]and $0c)shl 2)+(byte[3]and $0c)+((byte[4]and $0c)shr 2);
     byte1[4]:=((byte[1]and $03)shl 6)+((byte[2]and $03)shl 4)+((byte[3]and $03)shl 2)+(byte[4]and $03); number:=((byte1[1])shl 24)+((byte1[2])shl 16)
                 +((byte1[3])shl 8)+(byte1[4]); number:=number xor $1973122980;
     number:= not number;
     result:= number;
      
    end;加解密部分自己要改一下,因为我已经贴出来了,改几个常量即可。 
      

  2.   

    在win server2003 中好象不能用
      

  3.   

    unit PNPCore; 
    interfaceconst
      WinSize = 6;procedure Encrypt (SName, TName, Password: string);
    procedure Decrypt (SName, TName, Password: string);implementationtype
      TKey = array[1..WinSize, 1..WinSize] of Boolean;var
      Key: TKey;
      SFile, Tfile: file;
      FSize: LongInt;procedure InitKey (Password: string);
    const
      CodeSize = WinSize*(WinSize+2) shr 3;
    var
      Code: array[1..CodeSize] of 0..3;
      PassL: Integer;
      Max, Half, Bit, Start, Sum, X, Y: Integer;
      A, B: Integer;
    begin
      PassL:= Length(Password);
      Max:= 2*PassL-3;
      if Max>CodeSize then Max:=CodeSize;
      Half:= Max div 2;
      Start:= PassL-Half;
      for Bit:= 1 to Half do
        begin
          Y:= Start+Bit; X:= 1; Sum:= 0;
          repeat
            Inc (Sum, Abs(Ord(Password[X])-Ord(Password[Y])));
            Inc (X); Dec (Y);
          until X>=Y;
          Code[Bit]:= Sum;
        end;
      for Bit:= Half+1 to Max do
        begin
          Y:= PassL; X:= Bit-Half+1; Sum:= 0;
          repeat
            Inc (Sum, Abs(Ord(Password[X])-Ord(Password[Y])));
            Inc (X); Dec (Y);
          until X>=Y;
          Code[Bit]:=Sum;
        end;
      for Bit:= Max+1 to CodeSize do
        Code[Bit]:= Code[Bit-Max];
      Y:= 1; Bit:= 0;
      FillChar (Key, SizeOf(Key), False);
      for Y:= 1 to WinSize shr 1 do
        for X:= Y to WinSize shr 1 do
         begin
           Inc (Bit);
           B:=Code[Bit] mod 4;
           A:=Code[Bit] shr 2 mod 4;
           case B of
             0:Key[X, Y]:= True;
             1:Key[WinSize+1-Y, X]:= True;
             2:Key[WinSize+1-X, WinSize+1-Y]:= True;
             3:Key[Y, WinSize+1-X]:= True;
           end;
           if not ((X=Y) or (X+Y=WinSize+1)) then
             case A of
               0:Key[Y, X]:= True;
               1:Key[X, WinSize+1-Y]:= True;
               2:Key[WinSize+1-Y, WinSize+1-X]:= True;
               3:Key[WinSize+1-X, Y]:= True;
             end;
         end;
    end;procedure TurnKey (var Key: TKey);
    var
      TempKey: TKey;
      I, J: Integer;
    begin
      for I:=1 to WinSize do
        for J:=1 to WinSize do
          TempKey[J, WinSize+1-I]:= Key[I, J];
      Key:= TempKey;
    end;procedure Encrypt (SName, TName, Password: string);
    const
      Count = WinSize*WinSize;
    var
      Buf: array[1..Count] of Byte;
      Matrix: array[1..WinSize, 1..WinSize] of Byte;
      CurKey: TKey;
      I, J, X, Y, PassL, Result, PassD: Integer;
    begin
      InitKey (Password);
      Assign (SFile, SName);
      Assign (TFile, TName);
      Reset (SFile, 1);
      Rewrite (TFile, 1);
      PassL:= Length(Password); PassD:= PassL; CurKey:= Key;
      FSize:= FileSize(SFile);
      BlockWrite (TFile, FSize, SizeOf(FSize));
      FillChar (Buf, SizeOf(Buf), 0);
      BlockRead (SFile, Buf, Count, Result);
      while Result>0 do
       begin
         if Result<Count then
           for I:= Result+1 to Count do
            begin
              RandSeed:= MaxAvail;
              Buf[I]:= Random(256);
            end;
         for I:= 1 to Count do
          begin
            Inc (PassD);
            if PassD>PassL then PassD:= 1;
            Buf[I]:= Buf[I] xor Byte(Password[PassD]);
          end;
         J:= 0;
         for I:= 1 to 4 do
          begin
            for X:= 1 to WinSize do
              for Y:= 1 to WinSize do
                if CurKey[X, Y] then
                 begin
                   Inc (J);
                   Matrix[X, Y]:= Buf[J];
                 end;
            TurnKey (CurKey);
          end;
         BlockWrite (TFile, Matrix, Count);
         FillChar (Buf, SizeOf(Buf), 0);
         BlockRead (SFile, Buf, Count, Result);
       end;
      Close (TFile);
      Close (SFile);
    end;procedure Decrypt (SName, TName, Password: string);
    const
      Count = WinSize*WinSize;
    var
      Buf: array[1..Count] of Byte;
      Matrix: array[1..WinSize, 1..WinSize] of Byte;
      CurKey: TKey;
      I, J, X, Y, PassL, Result, PassD: Integer;
      Readed, EofSign: LongInt;
    begin
      InitKey (Password);
      Assign (SFile, SName);
      Assign (TFile, TName);
      Reset (SFile, 1);
      Rewrite (TFile, 1);
      PassL:= Length(Password); PassD:= PassL; CurKey:= Key;
      FSize:= 0;
      BlockRead (SFile, FSize, SizeOf(FSize));
      FillChar (Matrix, SizeOf(Matrix), 0);
      BlockRead (SFile, Matrix, Count, Result);
      Readed:= 0;
      while Result>0 do
       begin
         J:= 0;
         EofSign:= FSize-Readed;
         for I:= 1 to 4 do
          begin
            for X:= 1 to WinSize do
              for Y:= 1 to WinSize do
                if CurKey[X, Y] then
                 begin
                   Inc (J);
                   Buf[J]:= Matrix[X, Y];
                 end;
            TurnKey (CurKey);
          end;
         for I:= 1 to Count do
          begin
            Inc (PassD);
            if PassD>PassL then PassD:= 1;
            Buf[I]:= Buf[I] xor Byte(Password[PassD]);
            if I=EofSign then
             begin
               BlockWrite (TFile, Buf, I);
               Close (TFile);
               Close (SFile);
               Exit;
             end;
          end;
         BlockWrite (TFile, Buf, Count);
         FillChar (Matrix, SizeOf(Matrix), 0);
         BlockRead (SFile, Matrix, Count, Result);
         Inc (Readed, Count);
       end;
      Close (TFile);
      Close (SFile);
    end;end.