我原来的程序使用VC编写,现在想改用DElphi来实现,在做Des的时候遇到一个问题就是,两个程序在进行Des的时候结果不一样,请大家分析一下!
Delphi语法:
unit des32;interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs;
{$H-}
type
TActionType = (Encryption, Decryption);
TEncryption = class(TComponent)
private
{ Private declarations }
FInputString: string;
FOutputString: string;
FHexInputString: string;
FHexOutputString: string;
FKeyString: string;
FAction: TActionType;
FOutputLength: Word;
procedure DES(var Input; var Output; var Key; Encrypt: Boolean);
function BinaryToHexStr(input: string): string;
function HexStrToBinary(input: string): string;
procedure SetInputString(input: string);
procedure SetHexInputString(input: string);
procedure SetOutputString(input: string);
procedure SetHexOutputString(input: string);
procedure SetKeyString(input: string);
function StrNPas(input: PChar; Leng: integer): string;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure Execute;
published
{ Published declarations }
property Input: string read FInputString write SetInputString;
property Output: string read FOutputString write SetOutputString;
property HexInput: string read FHexInputString write SetHexInputString;
property HexOutput: string read FHexOutputString write SetHexOutputString;
property Key: string read FKeyString write SetKeyString;
property Action: TActionType read FAction write FAction;
property OutputLength: Word read FOutputLength write FOutputLength;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Samples', [TEncryption]);
end;constructor TEncryption.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Key := 'Delphi32';
FOutputLength := 8;
end;function TEncryption.BinaryToHexStr(input: string): string;
var
idx: integer;
OutStr: string;
begin
OutStr := '';
for idx := 1 to length(input) do
OutStr := OutStr + intToHex(ord(input[idx]), 2); BinaryToHexStr := OutStr;
end;function TEncryption.HexStrToBinary(input: string): string;
var
idx, cnt: integer;
OutStr: string;
begin
cnt := length(input) div 2;
for idx := 1 to cnt do
OutStr[idx] := chr(StrToint('$' + Copy(input, idx * 2 - 1, 2))); OutStr[0] := chr(cnt);
HexStrToBinary := OutStr;
end;procedure TEncryption.SetHexOutputString(input: string);
begin
FHexOutputString := input;
FOutputString := HexStrToBinary(FHexOutputString);
end;procedure TEncryption.SetOutputString(input: string);
begin
FOutputString := input;
FHexOutputString := BinaryToHexStr(FOutputString);
end;procedure TEncryption.SetHexInputString(input: string);
begin
if Length(input) > 254 then
begin
MessageDlg('HexInput String Length Must Less 255 Character.', mtInformation, [mbOk], 0);
exit;
end;
FHexInputString := input;
FInputString := HexStrToBinary(FHexInputString);
end;procedure TEncryption.SetKeyString(input: string);
begin
if length(input) > 8 then
begin
MessageDlg('Key String Length Must Less 9 Character.', mtInformation, [mbOk], 0);
exit;
end;
FKeyString := input;
end;procedure TEncryption.SetInputString(input: string);
var
leng: integer;
begin
if length(input) > 127 then
begin
MessageDlg('Input String Length Must Less 127 Character.', mtInformation, [mbOk], 0);
exit;
end;
FInputString := input;
FHexInputString := BinaryToHexStr(FInputString);
end;
Delphi语法:
unit des32;interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs;
{$H-}
type
TActionType = (Encryption, Decryption);
TEncryption = class(TComponent)
private
{ Private declarations }
FInputString: string;
FOutputString: string;
FHexInputString: string;
FHexOutputString: string;
FKeyString: string;
FAction: TActionType;
FOutputLength: Word;
procedure DES(var Input; var Output; var Key; Encrypt: Boolean);
function BinaryToHexStr(input: string): string;
function HexStrToBinary(input: string): string;
procedure SetInputString(input: string);
procedure SetHexInputString(input: string);
procedure SetOutputString(input: string);
procedure SetHexOutputString(input: string);
procedure SetKeyString(input: string);
function StrNPas(input: PChar; Leng: integer): string;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure Execute;
published
{ Published declarations }
property Input: string read FInputString write SetInputString;
property Output: string read FOutputString write SetOutputString;
property HexInput: string read FHexInputString write SetHexInputString;
property HexOutput: string read FHexOutputString write SetHexOutputString;
property Key: string read FKeyString write SetKeyString;
property Action: TActionType read FAction write FAction;
property OutputLength: Word read FOutputLength write FOutputLength;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Samples', [TEncryption]);
end;constructor TEncryption.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Key := 'Delphi32';
FOutputLength := 8;
end;function TEncryption.BinaryToHexStr(input: string): string;
var
idx: integer;
OutStr: string;
begin
OutStr := '';
for idx := 1 to length(input) do
OutStr := OutStr + intToHex(ord(input[idx]), 2); BinaryToHexStr := OutStr;
end;function TEncryption.HexStrToBinary(input: string): string;
var
idx, cnt: integer;
OutStr: string;
begin
cnt := length(input) div 2;
for idx := 1 to cnt do
OutStr[idx] := chr(StrToint('$' + Copy(input, idx * 2 - 1, 2))); OutStr[0] := chr(cnt);
HexStrToBinary := OutStr;
end;procedure TEncryption.SetHexOutputString(input: string);
begin
FHexOutputString := input;
FOutputString := HexStrToBinary(FHexOutputString);
end;procedure TEncryption.SetOutputString(input: string);
begin
FOutputString := input;
FHexOutputString := BinaryToHexStr(FOutputString);
end;procedure TEncryption.SetHexInputString(input: string);
begin
if Length(input) > 254 then
begin
MessageDlg('HexInput String Length Must Less 255 Character.', mtInformation, [mbOk], 0);
exit;
end;
FHexInputString := input;
FInputString := HexStrToBinary(FHexInputString);
end;procedure TEncryption.SetKeyString(input: string);
begin
if length(input) > 8 then
begin
MessageDlg('Key String Length Must Less 9 Character.', mtInformation, [mbOk], 0);
exit;
end;
FKeyString := input;
end;procedure TEncryption.SetInputString(input: string);
var
leng: integer;
begin
if length(input) > 127 then
begin
MessageDlg('Input String Length Must Less 127 Character.', mtInformation, [mbOk], 0);
exit;
end;
FInputString := input;
FHexInputString := BinaryToHexStr(FInputString);
end;
function TEncryption.StrNPas(input: PChar; Leng: integer): string;
var
idx: integer;
output: string;
begin
for idx := 1 to Leng do
output[idx] := input[idx - 1];
output[0] := chr(Leng);
StrNPas := output;
end;procedure TEncryption.DES(var Input; var Output; var Key; Encrypt: Boolean);const
IP: array[1..64] of Byte = (
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7);
InvIP: array[1..64] of Byte = (
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25);
E: array[1..48] of Byte = (
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1);
P: array[1..32] of Byte = (
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25);
SBoxes: array[1..8, 0..3, 0..15] of Byte =
(((14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7),
(0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8),
(4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0),
(15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13)), ((15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10),
(3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5),
(0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15),
(13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9)), ((10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8),
(13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1),
(13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7),
(1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12)), ((7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15),
(13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9),
(10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4),
(3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14)), ((2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9),
(14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6),
(4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14),
(11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3)), ((12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11),
(10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8),
(9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6),
(4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13)), ((4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1),
(13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6),
(1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2),
(6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12)), ((13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7),
(1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2),
(7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8),
(2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11))); PC_1: array[1..56] of Byte = (
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4); PC_2: array[1..48] of Byte = (
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32); ShiftTable: array[1..16] of Byte = (1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28);
InputValue: array[1..64] of Byte;
OutputValue: array[1..64] of Byte;
RoundKeys: array[1..16, 1..48] of Byte;
L, R, FunctionResult: array[1..32] of Byte;
C, D: array[1..28] of Byte; function GetBit(var Data; Index: Byte): Byte; var
Bits: array[0..7] of Byte absolute Data; begin
Dec(Index);
if Bits[Index div 8] and (128 shr (Index mod 8)) > 0 then
GetBit := 1
else
GetBit := 0;
end; procedure SetBit(var Data; Index, Value: Byte); var
Bits: array[0..7] of Byte absolute Data;
Bit: Byte;
begin
Dec(Index);
Bit := 128 shr (Index mod 8);
case Value of
0: Bits[Index div 8] := Bits[Index div 8] and (not Bit);
1: Bits[Index div 8] := Bits[Index div 8] or Bit;
end;
end; procedure F(var FR, FK, Output);
var
R: array[1..48] of Byte absolute FR;
K: array[1..48] of Byte absolute FK;
Temp1: array[1..48] of Byte;
Temp2: array[1..32] of Byte;
n, h, i, j, Row, Column: Integer;
TotalOut: array[1..32] of Byte absolute Output;
begin
for n := 1 to 48 do Temp1[n] := R[E[n]] xor K[n];
for n := 1 to 8 do begin
i := (n - 1) * 6;
j := (n - 1) * 4;
Row := Temp1[i + 1] * 2 + Temp1[i + 6];
Column := Temp1[i + 2] * 8 + Temp1[i + 3] * 4 + Temp1[i + 4] * 2 + Temp1[i + 5];
for h := 1 to 4 do begin
case h of
1: Temp2[j + h] := (SBoxes[n, Row, Column] and 8) div 8;
2: Temp2[j + h] := (SBoxes[n, Row, Column] and 4) div 4;
3: Temp2[j + h] := (SBoxes[n, Row, Column] and 2) div 2;
4: Temp2[j + h] := (SBoxes[n, Row, Column] and 1);
end;
end;
end;
for n := 1 to 32 do TotalOut[n] := Temp2[P[n]];
end; procedure Shift(var SubKeyPart); var
SKP: array[1..28] of Byte absolute SubKeyPart;
n, b: Byte;
begin
b := SKP[1];
for n := 1 to 27 do SKP[n] := SKP[n + 1];
SKP[28] := b;
end; procedure SubKey(Round: Byte; var SubKey); var
SK: array[1..48] of Byte absolute SubKey;
n, b: Byte;
begin
for n := 1 to ShiftTable[Round] do begin
Shift(C);
Shift(D);
end;
for n := 1 to 48 do begin
b := PC_2[n];
if b <= 28 then SK[n] := C[b] else SK[n] := D[b - 28];
end;
end;var
n, i, b, Round: Byte;
Outputje: array[1..64] of Byte;
K: array[1..48] of Byte;
fi: Text;
begin
for n := 1 to 64 do InputValue[n] := GetBit(Input, n);
for n := 1 to 28 do begin
C[n] := GetBit(Key, PC_1[n]);
D[n] := GetBit(Key, PC_1[n + 28]);
end;
for n := 1 to 16 do
SubKey(n, RoundKeys[n]); for n := 1 to 64 do
if n <= 32 then
L[n] := InputValue[IP[n]]
else
R[n - 32] := InputValue[IP[n]]; for Round := 1 to 16 do begin
if Encrypt then
F(R, RoundKeys[Round], FunctionResult)
else
F(R, RoundKeys[17 - Round], FunctionResult); for n := 1 to 32 do
FunctionResult[n] := FunctionResult[n] xor L[n]; L := R;
R := FunctionResult;
end;
for n := 1 to 64 do begin
b := InvIP[n];
if b <= 32 then OutputValue[n] := R[b] else OutputValue[n] := L[b - 32];
end;
for n := 1 to 64 do SetBit(Output, n, OutputValue[n]);
end;procedure TEncryption.Execute;
var
EncryptionFlag: boolean;
tmpInputStr, tmpOutputStr, tmpKey: array[0..8] of Char;
Leng, idx, LoopDiv, LoopMod: integer;
tmpStr: string;
begin
FOutputString := '';
Leng := length(FKeyString);
if Leng = 0 then
begin
FKeyString := 'Delphi30';
Leng := 8;
end;
LoopDiv := Leng div 8;
if LoopDiv = 1 then
StrPcopy(tmpKey, FKeyString)
else
begin
LoopMod := Leng mod 8;
for idx := 1 to LoopMod do
tmpKey[idx - 1] := FKeyString[idx]; for idx := LoopMod + 1 to 8 do
tmpKey[idx - 1] := chr(0);
end; Leng := length(FInputString);
LoopMod := Leng mod 8;
LoopDiv := Leng div 8; EncryptionFlag := False;
if FAction = Encryption then
EncryptionFlag := True; for idx := 1 to LoopDiv do
begin
tmpStr := copy(FInputString, (idx - 1) * 8 + 1, 8);
Move(tmpStr[1], tmpInputStr, 8);
Des(tmpInputStr, tmpOutputStr, tmpKey, EncryptionFlag);
FOutputString := FOutputString + StrNPas(tmpOutputStr, 8);
end;
if LoopMod > 0 then
begin
for idx := 1 to LoopMod do
tmpInputStr[idx - 1] := FInputString[LoopDiv * 8 + idx]; for idx := LoopMod + 1 to 8 do
tmpInputStr[idx - 1] := chr(0); Des(tmpInputStr, tmpOutputStr, tmpKey, EncryptionFlag);
FOutputString := FOutputString + StrNPas(tmpOutputStr, 8);
end;
if FOutputLength > 0 then
if Length(FOutputString) > FOutputLength then
FOutputString := Copy(FOutputString, Length(FOutputString) - FOutputLength + 1, FOutputLength); FHexOutputString := BinaryToHexStr(FOutputString);
end;end.
/********************************************************************/
/*DES加密程序 1998.3.11 */
/*使用方法: */
/* 加密: DES FILENAME <CR> */
/* TYPE PASSWORD FOR FILENAME XXXXXXXX 8字节 */
/* 解密: DES FILENAME <CR> */
/* TYPE PASSWORD FOR FILENAME XXXXXXXX 8字节 */
/*在vc++1.5或tc2.0 DOS6.22下调试通过 */
/********************************************************************/
/*置换算法:
为完成char inblock[8]按换位表charip[64]变换到char outblock[8],先生
成一个变换矩阵 char perm[i][j][k],i=16,j=16,k=8
i:把输入的64bit分成四位一组,每次处理四位信息
j:四位可表示0......15,共16个值
k:对应j的每一个值,给出一个8字节的矩阵表示对应的位的值为1
比如:
char inblock{11111110b,
01011101b,
01011010b,
00010100b,
10101011b,
11111000b,
00000100b,
10000101b}
则:
char *perm[0][15]={00000001b,
00000001b,
00000000b,
00000000b,
00000001b,
00000001b,
00000000b,
00000000b} char *perm[1][14]={00000000b,
00000000b,
00000001b,
00000000b,
00000000b,
00000000b,
00000001b,
00000001b}
char *perm[15][15]={00000000b,
00000000b,
10000000b,
10000000b,
00000000b,
00000000b,
10000000b,
10000000b}
char *ip[]
={ 58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 };
*//*#define DEBUG */////////////////////////
/*
Revision History:
2001.1.1
Modified to suffies c compiler no to generate warnings.
*/extern "C"
{int perminit(char perm[16][16][8], char p[64]);
int permute(char *inblock, char perm[16][16][8], char *outblock);
int iter(int num, char *inblock, char *outblock);
int kinit(char *key);
int sinit();
int p32init();
int getcomp(int k, int v);
int f(char *right, int num, char *fret);
int expand(char *right, char *bigright);
int contract(char *in48, char *out32);
int perm32(char *inblock, char *outblock);
static char iperm[16][16][8],fperm[16][16][8];
static char s[4][4096];
static char p32[4][256][4];
static char kn[16][6]; /* 密钥*/
int endes(char *inblock, char*outblock) /* 加密一个64-bit块*/
{
char iters[17][8];
char swap[8]; /* 放置左右交换的值*/
register int i;
register char *s, *t;
permute(inblock,iperm,iters[0]);
for (i=0; i<16; i++) /* 完成1~16层的交换*/
iter(i,iters[i],iters[i+1]);
/* iters[0][0]~iters[0][8]操作后放到iters[1][0]~iters[1][8]
iters[1][0]~iters[1][8]操作后放到iters[2][0]~iters[2][8]...以此类推*/
s = swap; t = &iters[16][4];
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
t = &iters[16][0];
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
permute(swap,fperm,outblock); return 0;
}int dedes(char *inblock, char *outblock) /* 解密一个64-bit 块*/
{
char iters[17][8];
char swap[8];
register int i;
register char *s, *t;
permute(inblock,iperm,iters[0]);
for (i=0; i<16; i++)
iter(15-i,iters[i],iters[i+1]);
s = swap; t = &iters[16][4];
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
t = &iters[16][0];
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
permute(swap,fperm,outblock); return 0;
}static int permute(char *inblock, char perm[16][16][8], char *outblock) /* 初始换位/最后换位变换*/
{
register int i,j;
register char *ib, *ob;
register char *p, *q;
for (i=0, ob = outblock; i<8; i++)
*ob++ = 0;
ib = inblock;
for (j = 0; j < 16; j += 2, ib++)
{
ob = outblock;
p = perm[j][(*ib >> 4) & 017]; /*高四位*/
q = perm[j + 1][*ib & 017]; /*低四位*/
for (i = 0; i < 8; i++)
*ob++ |= *p++ | *q++;
/*-----------------*(ob++)=(*ob)|(*(p++)|*(q++))-----------------*/
}
return 0;
}static char ip[] /* 初始换位表*/
= { 58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 };static char fp[] /* 最后换位表*/
= { 40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25 };static char pc1[] /* pc1表*/
= { 57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4 };static char totrot[] /* 密钥生成中的循环左移位的累计次数*/
= { 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };static char pc1m[56];
static char pcr[56];static char pc2[] /* pc2表*/
= { 14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32 };static char si[8][64] /* 48->32 bit 压缩表*/
= { /* S[1] */
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
/* S[2] */
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
/* S[3] */
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
/* S[4] */
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
/* S[5] */
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
/* S[6] */
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
/* S[7] */
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
/* S[8] */
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 };static char p32i[] /* 32-bit 单纯换位表*/
= { 16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25 };
unit DESCrypt;interface
uses
SysUtils; function EnCrypt(aStr: string; acKey: string ): string; //加密字符串-------------------------------
function DeCrypt(aStr: string; acKey: string ): string; //解密字符串-------------------------------implementationtype
TByte32 = array[1..32] of Byte;
TSData = array[0..63] of Byte;
TBlock = array[0..7] of Byte; const
SA1: TSData =
(1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1,
0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1);
SA2: TSData =
(1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1);
SA3: TSData =
(1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0,
1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1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, 4,
43, 54, 33, 39, 49, 29, 32, 42, 53, 47, 35, 50, 46, 51, 41, 30, 36, 55, 48, 44, 52, 38, 31, 34),
(18, 21, 15, 28, 5, 9, 7, 4, 19, 10, 25, 14, 27, 23, 16, 8, 2, 12, 20, 11, 3, 24, 17, 6,
45, 56, 35, 41, 51, 31, 34, 44, 55, 49, 37, 52, 48, 53, 43, 32, 38, 29, 50, 46, 54, 40, 33, 36),
(20, 23, 17, 2, 7, 11, 9, 6, 21, 12, 27, 16, 1, 25, 18, 10, 4, 14, 22, 13, 5, 26, 19, 8,
47, 30, 37, 43, 53, 33, 36, 46, 29, 51, 39, 54, 50, 55, 45, 34, 40, 31, 52, 48, 56, 42, 35, 38),
(22, 25, 19, 4, 9, 13, 11, 8, 23, 14, 1, 18, 3, 27, 20, 12, 6, 16, 24, 15, 7, 28, 21, 10,
49, 32, 39, 45, 55, 35, 38, 48, 31, 53, 41, 56, 52, 29, 47, 36, 42, 33, 54, 50, 30, 44, 37, 40),
(24, 27, 21, 6, 11, 15, 13, 10, 25, 16, 3, 20, 5, 1, 22, 14, 8, 18, 26, 17, 9, 2, 23, 12,
51, 34, 41, 47, 29, 37, 40, 50, 33, 55, 43, 30, 54, 31, 49, 38, 44, 35, 56, 52, 32, 46, 39, 42),
(26, 1, 23, 8, 13, 17, 15, 12, 27, 18, 5, 22, 7, 3, 24, 16, 10, 20, 28, 19, 11, 4, 25, 14,
53, 36, 43, 49, 31, 39, 42, 52, 35, 29, 45, 32, 56, 33, 51, 40, 46, 37, 30, 54, 34, 48, 41, 44),
(28, 3, 25, 10, 15, 19, 17, 14, 1, 20, 7, 24, 9, 5, 26, 18, 12, 22, 2, 21, 13, 6, 27, 16,
55, 38, 45, 51, 33, 41, 44, 54, 37, 31, 47, 34, 30, 35, 53, 42, 48, 39, 32, 56, 36, 50, 43, 46),
(1, 4, 26, 11, 16, 20, 18, 15, 2, 21, 8, 25, 10, 6, 27, 19, 13, 23, 3, 22, 14, 7, 28, 17,
56, 39, 46, 52, 34, 42, 45, 55, 38, 32, 48, 35, 31, 36, 54, 43, 49, 40, 33, 29, 37, 51, 44, 47),
(3, 6, 28, 13, 18, 22, 20, 17, 4, 23, 10, 27, 12, 8, 1, 21, 15, 25, 5, 24, 16, 9, 2, 19,
30, 41, 48, 54, 36, 44, 47, 29, 40, 34, 50, 37, 33, 38, 56, 45, 51, 42, 35, 31, 39, 53, 46, 49),
(5, 8, 2, 15, 20, 24, 22, 19, 6, 25, 12, 1, 14, 10, 3, 23, 17, 27, 7, 26, 18, 11, 4, 21,
32, 43, 50, 56, 38, 46, 49, 31, 42, 36, 52, 39, 35, 40, 30, 47, 53, 44, 37, 33, 41, 55, 48, 51),
(7, 10, 4, 17, 22, 26, 24, 21, 8, 27, 14, 3, 16, 12, 5, 25, 19, 1, 9, 28, 20, 13, 6, 23,
34, 45, 52, 30, 40, 48, 51, 33, 44, 38, 54, 41, 37, 42, 32, 49, 55, 46, 39, 35, 43, 29, 50, 53),
(9, 12, 6, 19, 24, 28, 26, 23, 10, 1, 16, 5, 18, 14, 7, 27, 21, 3, 11, 2, 22, 15, 8, 25,
36, 47, 54, 32, 42, 50, 53, 35, 46, 40, 56, 43, 39, 44, 34, 51, 29, 48, 41, 37, 45, 31, 52, 55),
(11, 14, 8, 21, 26, 2, 28, 25, 12, 3, 18, 7, 20, 16, 9, 1, 23, 5, 13, 4, 24, 17, 10, 27,
38, 49, 56, 34, 44, 52, 55, 37, 48, 42, 30, 45, 41, 46, 36, 53, 31, 50, 43, 39, 47, 33, 54, 29),
(13, 16, 10, 23, 28, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 56, 31),
(14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32));var
G: array[1..16, 1..48] of Byte;
L, R, F: TByte32;
C: array[1..56] of Byte;
procedure DES_Init(Key: TBlock; FCode: Boolean);
var
n, h: Byte;
begin
C[1] := Ord(Key[7] and 128 > 0); C[29] := Ord(Key[7] and 2 > 0);
C[2] := Ord(Key[6] and 128 > 0); C[30] := Ord(Key[6] and 2 > 0);
C[3] := Ord(Key[5] and 128 > 0); C[31] := Ord(Key[5] and 2 > 0);
C[4] := Ord(Key[4] and 128 > 0); C[32] := Ord(Key[4] and 2 > 0);
C[5] := Ord(Key[3] and 128 > 0); C[33] := Ord(Key[3] and 2 > 0);
C[6] := Ord(Key[2] and 128 > 0); C[34] := Ord(Key[2] and 2 > 0);
C[7] := Ord(Key[1] and 128 > 0); C[35] := Ord(Key[1] and 2 > 0);
C[8] := Ord(Key[0] and 128 > 0); C[36] := Ord(Key[0] and 2 > 0); C[9] := Ord(Key[7] and 64 > 0); C[37] := Ord(Key[7] and 4 > 0);
C[10] := Ord(Key[6] and 64 > 0); C[38] := Ord(Key[6] and 4 > 0);
C[11] := Ord(Key[5] and 64 > 0); C[39] := Ord(Key[5] and 4 > 0);
C[12] := Ord(Key[4] and 64 > 0); C[40] := Ord(Key[4] and 4 > 0);
C[13] := Ord(Key[3] and 64 > 0); C[41] := Ord(Key[3] and 4 > 0);
C[14] := Ord(Key[2] and 64 > 0); C[42] := Ord(Key[2] and 4 > 0);
C[15] := Ord(Key[1] and 64 > 0); C[43] := Ord(Key[1] and 4 > 0);
C[16] := Ord(Key[0] and 64 > 0); C[44] := Ord(Key[0] and 4 > 0); C[17] := Ord(Key[7] and 32 > 0); C[45] := Ord(Key[7] and 8 > 0);
C[18] := Ord(Key[6] and 32 > 0); C[46] := Ord(Key[6] and 8 > 0);
C[19] := Ord(Key[5] and 32 > 0); C[47] := Ord(Key[5] and 8 > 0);
C[20] := Ord(Key[4] and 32 > 0); C[48] := Ord(Key[4] and 8 > 0);
C[21] := Ord(Key[3] and 32 > 0); C[49] := Ord(Key[3] and 8 > 0);
C[22] := Ord(Key[2] and 32 > 0); C[50] := Ord(Key[2] and 8 > 0);
C[23] := Ord(Key[1] and 32 > 0); C[51] := Ord(Key[1] and 8 > 0);
C[24] := Ord(Key[0] and 32 > 0); C[52] := Ord(Key[0] and 8 > 0); C[25] := Ord(Key[7] and 16 > 0); C[53] := Ord(Key[3] and 16 > 0);
C[26] := Ord(Key[6] and 16 > 0); C[54] := Ord(Key[2] and 16 > 0);
C[27] := Ord(Key[5] and 16 > 0); C[55] := Ord(Key[1] and 16 > 0);
C[28] := Ord(Key[4] and 16 > 0); C[56] := Ord(Key[0] and 16 > 0); if FCode then
begin
for n := 1 to 16 do
begin
for h := 1 to 48 do
begin
G[n, h] := C[Sc[n, h]];
end;
end;
end
else
begin
for n := 1 to 16 do
begin
for h := 1 to 48 do
begin
G[17 - n, h] := C[Sc[n, h]];
end;
end;
end;
end;
TKeyByte = array[0..5] of Byte;
TDesMode = (dmEncry, dmDecry); function EncryStr(Str, Key: String): String;
function DecryStr(Str, Key: String): String;
function EncryStrHex(Str, Key: String): String;
function DecryStrHex(StrHex, Key: String): String;const
BitIP: array[0..63] of Byte =
(57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
56, 48, 40, 32, 24, 16, 8, 0,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6 ); BitCP: array[0..63] of Byte =
( 39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25,
32, 0, 40, 8, 48, 16, 56, 24 ); BitExp: array[0..47] of Integer =
( 31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9,10,
11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20,
21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0 ); BitPM: array[0..31] of Byte =
( 15, 6,19,20,28,11,27,16, 0,14,22,25, 4,17,30, 9,
1, 7,23,13,31,26, 2, 8,18,12,29, 5,21,10, 3,24 ); sBox: array[0..7] of array[0..63] of Byte =
( ( 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ), ( 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ), ( 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ), ( 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ), ( 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ), ( 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ), ( 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 ), ( 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 ) ); BitPMC1: array[0..55] of Byte =
( 56, 48, 40, 32, 24, 16, 8,
0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26,
18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14,
6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3 ); BitPMC2: array[0..47] of Byte =
( 13, 16, 10, 23, 0, 4,
2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7,
15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54,
29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52,
45, 41, 49, 35, 28, 31 );var
subKey: array[0..15] of TKeyByte;
implementationprocedure initPermutation(var inData: array of Byte);
var
newData: array[0..7] of Byte;
i: Integer;
begin
FillChar(newData, 8, 0);
for i := 0 to 63 do
if (inData[BitIP[i] shr 3] and (1 shl (7- (BitIP[i] and $07)))) <> 0 then
newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
for i := 0 to 7 do inData[i] := newData[i];
end;
var
newData: array[0..7] of Byte;
i: Integer;
begin
FillChar(newData, 8, 0);
for i := 0 to 63 do
if (inData[BitCP[i] shr 3] and (1 shl (7-(BitCP[i] and $07)))) <> 0 then
newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
for i := 0 to 7 do inData[i] := newData[i];
end;procedure expand(inData: array of Byte; var outData: array of Byte);
var
i: Integer;
begin
FillChar(outData, 6, 0);
for i := 0 to 47 do
if (inData[BitExp[i] shr 3] and (1 shl (7-(BitExp[i] and $07)))) <> 0 then
outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;procedure permutation(var inData: array of Byte);
var
newData: array[0..3] of Byte;
i: Integer;
begin
FillChar(newData, 4, 0);
for i := 0 to 31 do
if (inData[BitPM[i] shr 3] and (1 shl (7-(BitPM[i] and $07)))) <> 0 then
newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
for i := 0 to 3 do inData[i] := newData[i];
end;function si(s,inByte: Byte): Byte;
var
c: Byte;
begin
c := (inByte and $20) or ((inByte and $1e) shr 1) or
((inByte and $01) shl 4);
Result := (sBox[s][c] and $0f);
end;procedure permutationChoose1(inData: array of Byte;
var outData: array of Byte);
var
i: Integer;
begin
FillChar(outData, 7, 0);
for i := 0 to 55 do
if (inData[BitPMC1[i] shr 3] and (1 shl (7-(BitPMC1[i] and $07)))) <> 0 then
outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;procedure permutationChoose2(inData: array of Byte;
var outData: array of Byte);
var
i: Integer;
begin
FillChar(outData, 6, 0);
for i := 0 to 47 do
if (inData[BitPMC2[i] shr 3] and (1 shl (7-(BitPMC2[i] and $07)))) <> 0 then
outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;procedure cycleMove(var inData: array of Byte; bitMove: Byte);
var
i: Integer;
begin
for i := 0 to bitMove - 1 do
begin
inData[0] := (inData[0] shl 1) or (inData[1] shr 7);
inData[1] := (inData[1] shl 1) or (inData[2] shr 7);
inData[2] := (inData[2] shl 1) or (inData[3] shr 7);
inData[3] := (inData[3] shl 1) or ((inData[0] and $10) shr 4);
inData[0] := (inData[0] and $0f);
end;
end;procedure makeKey(inKey: array of Byte; var outKey: array of TKeyByte);
const
bitDisplace: array[0..15] of Byte =
( 1,1,2,2, 2,2,2,2, 1,2,2,2, 2,2,2,1 );
var
outData56: array[0..6] of Byte;
key28l: array[0..3] of Byte;
key28r: array[0..3] of Byte;
key56o: array[0..6] of Byte;
i: Integer;
begin
permutationChoose1(inKey, outData56); key28l[0] := outData56[0] shr 4;
key28l[1] := (outData56[0] shl 4) or (outData56[1] shr 4);
key28l[2] := (outData56[1] shl 4) or (outData56[2] shr 4);
key28l[3] := (outData56[2] shl 4) or (outData56[3] shr 4);
key28r[0] := outData56[3] and $0f;
key28r[1] := outData56[4];
key28r[2] := outData56[5];
key28r[3] := outData56[6]; for i := 0 to 15 do
begin
cycleMove(key28l, bitDisplace[i]);
cycleMove(key28r, bitDisplace[i]);
key56o[0] := (key28l[0] shl 4) or (key28l[1] shr 4);
key56o[1] := (key28l[1] shl 4) or (key28l[2] shr 4);
key56o[2] := (key28l[2] shl 4) or (key28l[3] shr 4);
key56o[3] := (key28l[3] shl 4) or (key28r[0]);
key56o[4] := key28r[1];
key56o[5] := key28r[2];
key56o[6] := key28r[3];
permutationChoose2(key56o, outKey[i]);
end;
end;procedure encry(inData, subKey: array of Byte;
var outData: array of Byte);
var
outBuf: array[0..5] of Byte;
buf: array[0..7] of Byte;
i: Integer;
begin
expand(inData, outBuf);
for i := 0 to 5 do outBuf[i] := outBuf[i] xor subKey[i]; buf[0] := outBuf[0] shr 2;
buf[1] := ((outBuf[0] and $03) shl 4) or (outBuf[1] shr 4);
buf[2] := ((outBuf[1] and $0f) shl 2) or (outBuf[2] shr 6);
buf[3] := outBuf[2] and $3f;
buf[4] := outBuf[3] shr 2;
buf[5] := ((outBuf[3] and $03) shl 4) or (outBuf[4] shr 4);
buf[6] := ((outBuf[4] and $0f) shl 2) or (outBuf[5] shr 6);
buf[7] := outBuf[5] and $3f;
for i := 0 to 7 do buf[i] := si(i, buf[i]);
for i := 0 to 3 do outBuf[i] := (buf[i*2] shl 4) or buf[i*2+1];
permutation(outBuf);
for i := 0 to 3 do outData[i] := outBuf[i];
end;procedure desData(desMode: TDesMode;
inData: array of Byte; var outData: array of Byte);
// inData, outData 都为8Bytes,否则出错
var
i, j: Integer;
temp, buf: array[0..3] of Byte;
begin
for i := 0 to 7 do outData[i] := inData[i];
initPermutation(outData);
if desMode = dmEncry then
begin
for i := 0 to 15 do
begin
for j := 0 to 3 do temp[j] := outData[j]; //temp = Ln
for j := 0 to 3 do outData[j] := outData[j + 4]; //Ln+1 = Rn
encry(outData, subKey[i], buf); //Rn ==Kn==> buf
for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j]; //Rn+1 = Ln^buf
end; for j := 0 to 3 do temp[j] := outData[j + 4];
for j := 0 to 3 do outData[j + 4] := outData[j];
for j := 0 to 3 do outData[j] := temp[j];
end
else if desMode = dmDecry then
begin
for i := 15 downto 0 do
begin
for j := 0 to 3 do temp[j] := outData[j];
for j := 0 to 3 do outData[j] := outData[j + 4];
encry(outData, subKey[i], buf);
for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j];
end;
for j := 0 to 3 do temp[j] := outData[j + 4];
for j := 0 to 3 do outData[j + 4] := outData[j];
for j := 0 to 3 do outData[j] := temp[j];
end;
conversePermutation(outData);
end;
var
StrByte, OutByte, KeyByte: array[0..7] of Byte;
StrResult: String;
I, J: Integer;
begin
if (Length(Str) > 0) and (Ord(Str[Length(Str)]) = 0) then
raise Exception.Create('Error: the last char is NULL char.');
if Length(Key) < 8 then
while Length(Key) < 8 do Key := Key + Chr(0);
while Length(Str) mod 8 <> 0 do Str := Str + Chr(0); for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
makeKey(keyByte, subKey); StrResult := ''; for I := 0 to Length(Str) div 8 - 1 do
begin
for J := 0 to 7 do
StrByte[J] := Ord(Str[I * 8 + J + 1]);
desData(dmEncry, StrByte, OutByte);
for J := 0 to 7 do
StrResult := StrResult + Chr(OutByte[J]);
end; Result := StrResult;
end;function DecryStr(Str, Key: String): String;
var
StrByte, OutByte, KeyByte: array[0..7] of Byte;
StrResult: String;
I, J: Integer;
begin
if Length(Key) < 8 then
while Length(Key) < 8 do Key := Key + Chr(0); for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
makeKey(keyByte, subKey); StrResult := ''; for I := 0 to Length(Str) div 8 - 1 do
begin
for J := 0 to 7 do StrByte[J] := Ord(Str[I * 8 + J + 1]);
desData(dmDecry, StrByte, OutByte);
for J := 0 to 7 do
StrResult := StrResult + Chr(OutByte[J]);
end;
while (Length(StrResult) > 0) and
(Ord(StrResult[Length(StrResult)]) = 0) do
Delete(StrResult, Length(StrResult), 1);
Result := StrResult;
end;///////////////////////////////////////////////////////////function EncryStrHex(Str, Key: String): String;
var
StrResult, TempResult, Temp: String;
I: Integer;
begin
TempResult := EncryStr(Str, Key);
StrResult := '';
for I := 0 to Length(TempResult) - 1 do
begin
Temp := Format('%x', [Ord(TempResult[I + 1])]);
if Length(Temp) = 1 then Temp := '0' + Temp;
StrResult := StrResult + Temp;
end;
Result := StrResult;
end;function DecryStrHex(StrHex, Key: String): String;
function HexToInt(Hex: String): Integer;
var
I, Res: Integer;
ch: Char;
begin
Res := 0;
for I := 0 to Length(Hex) - 1 do
begin
ch := Hex[I + 1];
if (ch >= '0') and (ch <= '9') then
Res := Res * 16 + Ord(ch) - Ord('0')
else if (ch >= 'A') and (ch <= 'F') then
Res := Res * 16 + Ord(ch) - Ord('A') + 10
else if (ch >= 'a') and (ch <= 'f') then
Res := Res * 16 + Ord(ch) - Ord('a') + 10
else raise Exception.Create('Error: not a Hex String');
end;
Result := Res;
end;var
Str, Temp: String;
I: Integer;
begin
Str := '';
for I := 0 to Length(StrHex) div 2 - 1 do
begin
Temp := Copy(StrHex, I * 2 + 1, 2);
Str := Str + Chr(HexToInt(Temp));
end;
Result := DecryStr(Str, Key);
end;end.
http://www.ksaiy.com/ynen/sf.asp对了。你在VC写的DES算法可以做成DLL来给DELPHI调用的。如果是用在软件加密上.那么在使用DLL的时候进行加壳保护.同时增加校验功能。校验功能你可以用文件大小或者是CRC来进行的。文件校验的例子源码这里下载:
http://www.ksaiy.com/ynen/index.asp
怎么少两个函数的定义呢?
function EnCrypt(aStr: string; acKey: string): string; //加密字符串-------------------------------
function DeCrypt(aStr: string; acKey: string): string; //解密字符串-------------------------------
var
n: Byte;
z: Word;
begin
L[1] := Ord(Input[7] and 64 > 0); R[1] := Ord(Input[7] and 128 > 0);
L[2] := Ord(Input[6] and 64 > 0); R[2] := Ord(Input[6] and 128 > 0);
L[3] := Ord(Input[5] and 64 > 0); R[3] := Ord(Input[5] and 128 > 0);
L[4] := Ord(Input[4] and 64 > 0); R[4] := Ord(Input[4] and 128 > 0);
L[5] := Ord(Input[3] and 64 > 0); R[5] := Ord(Input[3] and 128 > 0);
L[6] := Ord(Input[2] and 64 > 0); R[6] := Ord(Input[2] and 128 > 0);
L[7] := Ord(Input[1] and 64 > 0); R[7] := Ord(Input[1] and 128 > 0);
L[8] := Ord(Input[0] and 64 > 0); R[8] := Ord(Input[0] and 128 > 0);
L[9] := Ord(Input[7] and 16 > 0); R[9] := Ord(Input[7] and 32 > 0);
L[10] := Ord(Input[6] and 16 > 0); R[10] := Ord(Input[6] and 32 > 0);
L[11] := Ord(Input[5] and 16 > 0); R[11] := Ord(Input[5] and 32 > 0);
L[12] := Ord(Input[4] and 16 > 0); R[12] := Ord(Input[4] and 32 > 0);
L[13] := Ord(Input[3] and 16 > 0); R[13] := Ord(Input[3] and 32 > 0);
L[14] := Ord(Input[2] and 16 > 0); R[14] := Ord(Input[2] and 32 > 0);
L[15] := Ord(Input[1] and 16 > 0); R[15] := Ord(Input[1] and 32 > 0);
L[16] := Ord(Input[0] and 16 > 0); R[16] := Ord(Input[0] and 32 > 0);
L[17] := Ord(Input[7] and 4 > 0); R[17] := Ord(Input[7] and 8 > 0);
L[18] := Ord(Input[6] and 4 > 0); R[18] := Ord(Input[6] and 8 > 0);
L[19] := Ord(Input[5] and 4 > 0); R[19] := Ord(Input[5] and 8 > 0);
L[20] := Ord(Input[4] and 4 > 0); R[20] := Ord(Input[4] and 8 > 0);
L[21] := Ord(Input[3] and 4 > 0); R[21] := Ord(Input[3] and 8 > 0);
L[22] := Ord(Input[2] and 4 > 0); R[22] := Ord(Input[2] and 8 > 0);
L[23] := Ord(Input[1] and 4 > 0); R[23] := Ord(Input[1] and 8 > 0);
L[24] := Ord(Input[0] and 4 > 0); R[24] := Ord(Input[0] and 8 > 0);
L[25] := Input[7] and 1; R[25] := Ord(Input[7] and 2 > 0);
L[26] := Input[6] and 1; R[26] := Ord(Input[6] and 2 > 0);
L[27] := Input[5] and 1; R[27] := Ord(Input[5] and 2 > 0);
L[28] := Input[4] and 1; R[28] := Ord(Input[4] and 2 > 0);
L[29] := Input[3] and 1; R[29] := Ord(Input[3] and 2 > 0);
L[30] := Input[2] and 1; R[30] := Ord(Input[2] and 2 > 0);
L[31] := Input[1] and 1; R[31] := Ord(Input[1] and 2 > 0);
L[32] := Input[0] and 1; R[32] := Ord(Input[0] and 2 > 0); for n := 1 to 16 do
begin
z := ((R[32] xor G[n, 1]) shl 5) or ((R[5] xor G[n, 6]) shl 4)
or ((R[ 1] xor G[n, 2]) shl 3) or ((R[2] xor G[n, 3]) shl 2)
or ((R[ 3] xor G[n, 4]) shl 1) or ( R[4] xor G[n, 5]);
F[ 9] := L[ 9] xor SA1[z];
F[17] := L[17] xor SB1[z];
F[23] := L[23] xor SC1[z];
F[31] := L[31] xor SD1[z]; z := ((R[4] xor G[n, 7]) shl 5) or ((R[9] xor G[n, 12]) shl 4)
or ((R[5] xor G[n, 8]) shl 3) or ((R[6] xor G[n, 9]) shl 2)
or ((R[7] xor G[n, 10]) shl 1) or ( R[8] xor G[n, 11]);
F[13] := L[13] xor SA2[z];
F[28] := L[28] xor SB2[z];
F[ 2] := L[ 2] xor SC2[z];
F[18] := L[18] xor SD2[z]; z := ((R[ 8] xor G[n, 13]) shl 5) or ((R[13] xor G[n, 18]) shl 4)
or ((R[ 9] xor G[n, 14]) shl 3) or ((R[10] xor G[n, 15]) shl 2)
or ((R[11] xor G[n, 16]) shl 1) or ( R[12] xor G[n, 17]);
F[24] := L[24] xor SA3[z];
F[16] := L[16] xor SB3[z];
F[30] := L[30] xor SC3[z];
F[ 6] := L[ 6] xor SD3[z]; z := ((R[12] xor G[n, 19]) shl 5) or ((R[17] xor G[n, 24]) shl 4)
or ((R[13] xor G[n, 20]) shl 3) or ((R[14] xor G[n, 21]) shl 2)
or ((R[15] xor G[n, 22]) shl 1) or ( R[16] xor G[n, 23]);
F[26] := L[26] xor SA4[z];
F[20] := L[20] xor SB4[z];
F[10] := L[10] xor SC4[z];
F[ 1] := L[ 1] xor SD4[z]; z := ((R[16] xor G[n, 25]) shl 5) or ((R[21] xor G[n, 30]) shl 4)
or ((R[17] xor G[n, 26]) shl 3) or ((R[18] xor G[n, 27]) shl 2)
or ((R[19] xor G[n, 28]) shl 1) or ( R[20] xor G[n, 29]);
F[ 8] := L[ 8] xor SA5[z];
F[14] := L[14] xor SB5[z];
F[25] := L[25] xor SC5[z];
F[ 3] := L[ 3] xor SD5[z]; z := ((R[20] xor G[n, 31]) shl 5) or ((R[25] xor G[n, 36]) shl 4)
or ((R[21] xor G[n, 32]) shl 3) or ((R[22] xor G[n, 33]) shl 2)
or ((R[23] xor G[n, 34]) shl 1) or ( R[24] xor G[n, 35]);
F[ 4] := L[ 4] xor SA6[z];
F[29] := L[29] xor SB6[z];
F[11] := L[11] xor SC6[z];
F[19] := L[19] xor SD6[z]; z := ((R[24] xor G[n, 37]) shl 5) or ((R[29] xor G[n, 42]) shl 4)
or ((R[25] xor G[n, 38]) shl 3) or ((R[26] xor G[n, 39]) shl 2)
or ((R[27] xor G[n, 40]) shl 1) or ( R[28] xor G[n, 41]);
F[32] := L[32] xor SA7[z];
F[12] := L[12] xor SB7[z];
F[22] := L[22] xor SC7[z];
F[ 7] := L[ 7] xor SD7[z]; z := ((R[28] xor G[n, 43]) shl 5) or ((R[ 1] xor G[n, 48]) shl 4)
or ((R[29] xor G[n, 44]) shl 3) or ((R[30] xor G[n, 45]) shl 2)
or ((R[31] xor G[n, 46]) shl 1) or ( R[32] xor G[n, 47]);
F[ 5] := L[ 5] xor SA8[z];
F[27] := L[27] xor SB8[z];
F[15] := L[15] xor SC8[z];
F[21] := L[21] xor SD8[z]; L := R;
R := F;
end;
Output[0] := (L[8] shl 7) or (R[8] shl 6) or (L[16] shl 5) or (R[16] shl 4)
or (L[24] shl 3) or (R[24] shl 2) or (L[32] shl 1) or R[32];
Output[1] := (L[7] shl 7) or (R[7] shl 6) or (L[15] shl 5) or (R[15] shl 4)
or (L[23] shl 3) or (R[23] shl 2) or (L[31] shl 1) or R[31];
Output[2] := (L[6] shl 7) or (R[6] shl 6) or (L[14] shl 5) or (R[14] shl 4)
or (L[22] shl 3) or (R[22] shl 2) or (L[30] shl 1) or R[30];
Output[3] := (L[5] shl 7) or (R[5] shl 6) or (L[13] shl 5) or (R[13] shl 4)
or (L[21] shl 3) or (R[21] shl 2) or (L[29] shl 1) or R[29];
Output[4] := (L[4] shl 7) or (R[4] shl 6) or (L[12] shl 5) or (R[12] shl 4)
or (L[20] shl 3) or (R[20] shl 2) or (L[28] shl 1) or R[28];
Output[5] := (L[3] shl 7) or (R[3] shl 6) or (L[11] shl 5) or (R[11] shl 4)
or (L[19] shl 3) or (R[19] shl 2) or (L[27] shl 1) or R[27];
Output[6] := (L[2] shl 7) or (R[2] shl 6) or (L[10] shl 5) or (R[10] shl 4)
or (L[18] shl 3) or (R[18] shl 2) or (L[26] shl 1) or R[26];
Output[7] := (L[1] shl 7) or (R[1] shl 6) or (L[9] shl 5) or (R[9] shl 4)
or (L[17] shl 3) or (R[17] shl 2) or (L[25] shl 1) or R[25];
end;function StrToKey(aKey: string): TBlock;
var
Key : TBlock;
I : Integer;
begin
FillChar(Key, SizeOf(TBlock), 0);
for I := 1 to Length(aKey) do
begin
Key[I mod SizeOf(TBlock)] := Key[I mod SizeOf(TBlock)] + Ord(aKey[I]);
end; result := Key;
end;
var
ReadBuf : TBlock;
WriteBuf: TBlock;
Key : TBlock;
Count : Integer;
Offset : Integer; I : Integer;
S : string;
begin
result := ''; Key := StrToKey(acKey);
Des_Init(Key, True); Offset := 1;
Count := Length(aStr);
repeat
S := Copy(aStr, Offset, 8);
FillChar(ReadBuf, 8, 0);
Move(S[1], ReadBuf, Length(S));
Des_Code(ReadBuf, WriteBuf); for I := 0 to 7 do
begin
result := result + IntToHex(WriteBuf[I], 2);
end; Offset := Offset + 8;
until Offset > ((Count+7) div 8) * 8;
end;function DeCrypt(aStr: string; acKey: string ): string;
var
ReadBuf,
WriteBuf : TBlock;
Key : TBlock;
Offset : Integer;
Count : Integer;
I : Integer;
S : string;
begin
try
Key := StrToKey(acKey);
Des_Init(Key, False);
S := '';
I := 1;
repeat
S := S + Chr(StrToInt('$'+Copy(aStr, I, 2)));
Inc(I, 2);
until I > Length(aStr); Offset := 1;
Count := Length(S);
while Offset < ((Count+7) div 8 * 8) do
begin
FillChar(ReadBuf, 8, 0);
Move(S[Offset], ReadBuf, 8);
Des_Code(ReadBuf, WriteBuf); for I := 0 to 7 do
begin
result := result + Chr(WriteBuf[I]);
end; Offset := Offset + 8;
end; result := StrPas(PChar(result));
except
result := '';
end;
end;
end.
我测试了,好像和我的也不一样!
请参见:
http://community.csdn.net/Expert/topic/3488/3488026.xml?temp=.216839