请问在delphi中如何调用des算法对数据流进行加密?我不是要算法,是要调用的方法!!!! (50分)

  • 主题发起人 主题发起人 asiancat
  • 开始时间 开始时间
A

asiancat

Unregistered / Unconfirmed
GUEST, unregistred user!
问题在于如何调用?加密数据流。
具体算法如下:



(**************************************************)
(* *)
(* Data Encryption Standard (DES) *)
(* *)
(* Copyright (c) 1998-2001 *)
(* EldoS, Alexander Ionov *)
(* *)
(**************************************************)

{ $B-,C-,F-,G+,H+,I-,J+,L-,M-,O+,Q-,R-,T-,U-,W-,X+,Y-,Z1}

unit AstaDES;

interface

uses
Classes, SysUtils;

type
EDESError = class(Exception);

TDESBuffer = array [0..7] of byte;
TDESKey = array [0..7] of byte;
TDESExpandedKey = array [0..15, 0..47] of byte;
PDESBuffer = ^TDESBuffer;
PDESKey = ^TDESKey;
PDESExpandedKey = ^TDESExpandedKey;

DesKeyType = ({desEncKey, desDecKey, }desBothKey);

// Key expansion

procedure ExpandDESKey(const Key: TDESKey; var ExpandedKey: TDESExpandedKey);

// Block encryption

procedure EncryptDES(const InBuf: TDESBuffer; const ExpandedKey: TDESExpandedKey;
var OutBuf: TDESBuffer);

// Block decryption

procedure DecryptDES(const InBuf: TDESBuffer; const ExpandedKey: TDESExpandedKey;
var OutBuf: TDESBuffer);

// Stream encryption routines (ECB mode)

procedure EncryptDESStreamECB(Source: TStream; Count: cardinal;
const Key: TDESKey; Dest: TStream); overload;
procedure EncryptDESStreamECB(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; Dest: TStream); overload;

// Stream encryption routines (CBC mode)

procedure EncryptDESStreamCBC(Source: TStream; Count: cardinal;
const Key: TDESKey; const InitVector: TDESBuffer; Dest: TStream); overload;
procedure EncryptDESStreamCBC(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; const InitVector: TDESBuffer;
Dest: TStream); overload;

// Stream decryption routines (ECB mode)

procedure DecryptDESStreamECB(Source: TStream; Count: cardinal;
const Key: TDESKey; Dest: TStream); overload;
procedure DecryptDESStreamECB(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; Dest: TStream); overload;

// Stream decryption routines (CBC mode)

procedure DecryptDESStreamCBC(Source: TStream; Count: cardinal;
const Key: TDESKey; const InitVector: TDESBuffer; Dest: TStream); overload;
procedure DecryptDESStreamCBC(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; const InitVector: TDESBuffer;
Dest: TStream); overload;
Procedure _SetDESStringKey(Const AKey:String;var desKey:PDESExpandedKey);

resourcestring
SInvalidInBufSize = 'Invalid buffer size for decryption';
SReadError = 'Stream read error';
SWriteError = 'Stream write error';

implementation

uses
Math, AstaResourceString;

type
PLongWord = ^LongWord;

const
PC1: 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
);
PC2: 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
);
Shifts: array [1..16] of byte =
(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1);

procedure ExpandDESKey(const Key: TDESKey; var ExpandedKey: TDESExpandedKey);
var
C, D: array [1..28] of byte;
I, J: integer;
T: word;
begin
// permuted choise 1
FillChar(C, SizeOf(C), 0);
Fillchar(D, SizeOf(D), 0);
for I := 1 to 28 do
begin
if (Key[(PC1 - 1) shr 3] and (128 shr ((PC1 - 1) and $07))) > 0 then
C := 1;
if (Key[(PC1[I + 28] - 1) shr 3] and (128 shr ((PC1[I + 28] - 1) and $07))) > 0 then
D := 1;
end;
// producing subkeys
for I := 1 to 16 do
begin
// shifting C
T := 0;
Move(C[1], T, Shifts);
Move(C[Shifts + 1], C[1], 28 - Shifts);
Move(T, C[29 - Shifts], Shifts);
// shifting D
T := 0;
Move(D[1], T, Shifts);
Move(D[Shifts + 1], D[1], 28 - Shifts);
Move(T, D[29 - Shifts], Shifts);
// permuted choise 2
for J := 1 to 48 do
if PC2[J] <= 28 then
ExpandedKey[I - 1, J - 1] := C[PC2[J]]
else
ExpandedKey[I - 1, J - 1] := D[PC2[J] - 28];
end;
end;

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
);

IPR: 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);

S1: array [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)
);

S2: array [0..3, 0..15] of byte = (
(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)
);

S3: array [0..3, 0..15] of byte = (
(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)
);

S4: array [0..3, 0..15] of byte = (
( 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)
);

S5: array [0..3, 0..15] of byte = (
( 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)
);

S6: array [0..3, 0..15] of byte = (
(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)
);

S7: array [0..3, 0..15] of byte = (
( 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)
);

S8: array [0..3, 0..15] of byte = (
(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)
);

procedure EncryptDES(const InBuf: TDESBuffer; const ExpandedKey: TDESExpandedKey;
var OutBuf: TDESBuffer);
var
L, R, T2, T3: array [1..32] of byte;
I, J: integer;
B: byte;
T: array [1..48] of byte;
Row, Col: integer;
begin
// dividing into bits and performing initial permutation
for I := 1 to 64 do
begin
B := (InBuf[(IP - 1) shr 3] and (128 shr ((IP - 1) and $07)));
if B > 0 then B := 1;
if I <= 32 then
L := B
else
R[I - 32] := B;
end;
for I := 1 to 16 do
begin
// expanding and XORing
for J := 1 to 48 do
T[J] := R[E[J]] xor ExpandedKey[I - 1, J - 1];
// selecting
// step 1
Row := (T[1] shl 1) or T[6];
Col := (T[2] shl 3) or (T[3] shl 2) or (T[4] shl 1) or T[5];
T2[1] := (S1[Row, Col] and $08) shr 3; T2[2] := (S1[Row, Col] and $04) shr 2;
T2[3] := (S1[Row, Col] and $02) shr 1; T2[4] := S1[Row, Col] and $01;
// step 2
Row := (T[7] shl 1) or T[12];
Col := (T[8] shl 3) or (T[9] shl 2) or (T[10] shl 1) or T[11];
T2[5] := (S2[Row, Col] and $08) shr 3; T2[6] := (S2[Row, Col] and $04) shr 2;
T2[7] := (S2[Row, Col] and $02) shr 1; T2[8] := S2[Row, Col] and $01;
// step 3
Row := (T[13] shl 1) or T[18];
Col := (T[14] shl 3) or (T[15] shl 2) or (T[16] shl 1) or T[17];
T2[9] := (S3[Row, Col] and $08) shr 3; T2[10] := (S3[Row, Col] and $04) shr 2;
T2[11] := (S3[Row, Col] and $02) shr 1; T2[12] := S3[Row, Col] and $01;
// step 4
Row := (T[19] shl 1) or T[24];
Col := (T[20] shl 3) or (T[21] shl 2) or (T[22] shl 1) or T[23];
T2[13] := (S4[Row, Col] and $08) shr 3; T2[14] := (S4[Row, Col] and $04) shr 2;
T2[15] := (S4[Row, Col] and $02) shr 1; T2[16] := S4[Row, Col] and $01;
// step 5
Row := (T[25] shl 1) or T[30];
Col := (T[26] shl 3) or (T[27] shl 2) or (T[28] shl 1) or T[29];
T2[17] := (S5[Row, Col] and $08) shr 3; T2[18] := (S5[Row, Col] and $04) shr 2;
T2[19] := (S5[Row, Col] and $02) shr 1; T2[20] := S5[Row, Col] and $01;
// step 6
Row := (T[31] shl 1) or T[36];
Col := (T[32] shl 3) or (T[33] shl 2) or (T[34] shl 1) or T[35];
T2[21] := (S6[Row, Col] and $08) shr 3; T2[22] := (S6[Row, Col] and $04) shr 2;
T2[23] := (S6[Row, Col] and $02) shr 1; T2[24] := S6[Row, Col] and $01;
// step 7
Row := (T[37] shl 1) or T[42];
Col := (T[38] shl 3) or (T[39] shl 2) or (T[40] shl 1) or T[41];
T2[25] := (S7[Row, Col] and $08) shr 3; T2[26] := (S7[Row, Col] and $04) shr 2;
T2[27] := (S7[Row, Col] and $02) shr 1; T2[28] := S7[Row, Col] and $01;
// step 8
Row := (T[43] shl 1) or T[48];
Col := (T[44] shl 3) or (T[45] shl 2) or (T[46] shl 1) or T[47];
T2[29] := (S8[Row, Col] and $08) shr 3; T2[30] := (S8[Row, Col] and $04) shr 2;
T2[31] := (S8[Row, Col] and $02) shr 1; T2[32] := S8[Row, Col] and $01;
// permutation
for J := 1 to 32 do
T3[J] := T2[P[J]] xor L[J];
// exchanging
Move(R, L, SizeOf(R));
Move(T3, R, SizeOf(T3));
end;
// finalization
FillChar(OutBuf, SizeOf(OutBuf), 0);
for I := 1 to 64 do
begin
B := IPR;
if B <= 32 then B := R else B := L[B - 32];
if B > 0 then
OutBuf[(I - 1) shr 3] := OutBuf[(I - 1) shr 3] or (128 shr ((I - 1) and $07));
end;
end;

procedure DecryptDES(const InBuf: TDESBuffer; const ExpandedKey: TDESExpandedKey;
var OutBuf: TDESBuffer);
var
L, R, T2, T3: array [1..32] of byte;
I, J: integer;
B: byte;
T: array [1..48] of byte;
Row, Col: integer;
begin
// dividing into bits and performing initial permutation
for I := 1 to 64 do
begin
B := (InBuf[(IP - 1) shr 3] and (128 shr ((IP - 1) and $07)));
if B > 0 then B := 1;
if I <= 32 then
L := B
else
R[I - 32] := B;
end;
for I := 1 to 16 do
begin
// expanding and XORing
for J := 1 to 48 do
T[J] := R[E[J]] xor ExpandedKey[16 - I, J - 1];
// selecting
// step 1
Row := (T[1] shl 1) or T[6];
Col := (T[2] shl 3) or (T[3] shl 2) or (T[4] shl 1) or T[5];
T2[1] := (S1[Row, Col] and $08) shr 3; T2[2] := (S1[Row, Col] and $04) shr 2;
T2[3] := (S1[Row, Col] and $02) shr 1; T2[4] := S1[Row, Col] and $01;
// step 2
Row := (T[7] shl 1) or T[12];
Col := (T[8] shl 3) or (T[9] shl 2) or (T[10] shl 1) or T[11];
T2[5] := (S2[Row, Col] and $08) shr 3; T2[6] := (S2[Row, Col] and $04) shr 2;
T2[7] := (S2[Row, Col] and $02) shr 1; T2[8] := S2[Row, Col] and $01;
// step 3
Row := (T[13] shl 1) or T[18];
Col := (T[14] shl 3) or (T[15] shl 2) or (T[16] shl 1) or T[17];
T2[9] := (S3[Row, Col] and $08) shr 3; T2[10] := (S3[Row, Col] and $04) shr 2;
T2[11] := (S3[Row, Col] and $02) shr 1; T2[12] := S3[Row, Col] and $01;
// step 4
Row := (T[19] shl 1) or T[24];
Col := (T[20] shl 3) or (T[21] shl 2) or (T[22] shl 1) or T[23];
T2[13] := (S4[Row, Col] and $08) shr 3; T2[14] := (S4[Row, Col] and $04) shr 2;
T2[15] := (S4[Row, Col] and $02) shr 1; T2[16] := S4[Row, Col] and $01;
// step 5
Row := (T[25] shl 1) or T[30];
Col := (T[26] shl 3) or (T[27] shl 2) or (T[28] shl 1) or T[29];
T2[17] := (S5[Row, Col] and $08) shr 3; T2[18] := (S5[Row, Col] and $04) shr 2;
T2[19] := (S5[Row, Col] and $02) shr 1; T2[20] := S5[Row, Col] and $01;
// step 6
Row := (T[31] shl 1) or T[36];
Col := (T[32] shl 3) or (T[33] shl 2) or (T[34] shl 1) or T[35];
T2[21] := (S6[Row, Col] and $08) shr 3; T2[22] := (S6[Row, Col] and $04) shr 2;
T2[23] := (S6[Row, Col] and $02) shr 1; T2[24] := S6[Row, Col] and $01;
// step 7
Row := (T[37] shl 1) or T[42];
Col := (T[38] shl 3) or (T[39] shl 2) or (T[40] shl 1) or T[41];
T2[25] := (S7[Row, Col] and $08) shr 3; T2[26] := (S7[Row, Col] and $04) shr 2;
T2[27] := (S7[Row, Col] and $02) shr 1; T2[28] := S7[Row, Col] and $01;
// step 8
Row := (T[43] shl 1) or T[48];
Col := (T[44] shl 3) or (T[45] shl 2) or (T[46] shl 1) or T[47];
T2[29] := (S8[Row, Col] and $08) shr 3; T2[30] := (S8[Row, Col] and $04) shr 2;
T2[31] := (S8[Row, Col] and $02) shr 1; T2[32] := S8[Row, Col] and $01;
// permutation
for J := 1 to 32 do
T3[J] := T2[P[J]] xor L[J];
// exchanging
Move(R, L, SizeOf(R));
Move(T3, R, SizeOf(T3));
end;
// finalization
FillChar(OutBuf, SizeOf(OutBuf), 0);
for I := 1 to 64 do
begin
B := IPR;
if B <= 32 then B := R else B := L[B - 32];
if B > 0 then
OutBuf[(I - 1) shr 3] := OutBuf[(I - 1) shr 3] or (128 shr ((I - 1) and $07));
end;
end;

procedure EncryptDESStreamECB(Source: TStream; Count: cardinal;
const Key: TDESKey; Dest: TStream);
var
ExpandedKey: TDESExpandedKey;
begin
ExpandDESKey(Key, ExpandedKey);
EncryptDESStreamECB(Source, Count, ExpandedKey, Dest);
end;

procedure EncryptDESStreamECB(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; Dest: TStream);
var
TempIn, TempOut: TDESBuffer;
Done: cardinal;
begin
if Count = 0 then
begin
Source.Position := 0;
Count := Source.Size;
end
else Count := Min(Count, Source.Size - Source.Position);
if Count = 0 then exit;
while Count >= SizeOf(TDESBuffer) do
begin
Done := Source.Read(TempIn, SizeOf(TempIn));
if Done < SizeOf(TempIn) then
raise EStreamError.Create(SReadError);
EncryptDES(TempIn, ExpandedKey, TempOut);
Done := Dest.Write(TempOut, SizeOf(TempOut));
if Done < SizeOf(TempOut) then
raise EStreamError.Create(SWriteError);
Dec(Count, SizeOf(TDESBuffer));
end;
if Count > 0 then
begin
Done := Source.Read(TempIn, Count);
if Done < Count then
raise EStreamError.Create(SReadError);
FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
EncryptDES(TempIn, ExpandedKey, TempOut);
Done := Dest.Write(TempOut, SizeOf(TempOut));
if Done < SizeOf(TempOut) then
raise EStreamError.Create(SWriteError);
end;
end;

procedure DecryptDESStreamECB(Source: TStream; Count: cardinal;
const Key: TDESKey; Dest: TStream);
var
ExpandedKey: TDESExpandedKey;
begin
ExpandDESKey(Key, ExpandedKey);
DecryptDESStreamECB(Source, Count, ExpandedKey, Dest);
end;

procedure DecryptDESStreamECB(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; Dest: TStream);
var
TempIn, TempOut: TDESBuffer;
Done: cardinal;
begin
if Count = 0 then
begin
Source.Position := 0;
Count := Source.Size;
end
else Count := Min(Count, Source.Size - Source.Position);
if Count = 0 then exit;
if (Count mod SizeOf(TDESBuffer)) > 0 then
raise EDESError.Create(SInvalidInBufSize);
while Count >= SizeOf(TDESBuffer) do
begin
Done := Source.Read(TempIn, SizeOf(TempIn));
if Done < SizeOf(TempIn) then
raise EStreamError.Create(SReadError);
DecryptDES(TempIn, ExpandedKey, TempOut);
Done := Dest.Write(TempOut, SizeOf(TempOut));
if Done < SizeOf(TempOut) then
raise EStreamError.Create(SWriteError);
Dec(Count, SizeOf(TDESBuffer));
end;
end;

procedure EncryptDESStreamCBC(Source: TStream; Count: cardinal;
const Key: TDESKey; const InitVector: TDESBuffer; Dest: TStream);
var
ExpandedKey: TDESExpandedKey;
begin
ExpandDESKey(Key, ExpandedKey);
EncryptDESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
end;

procedure EncryptDESStreamCBC(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; const InitVector: TDESBuffer;
Dest: TStream);
var
TempIn, TempOut, Vector: TDESBuffer;
Done: cardinal;
begin
if Count = 0 then
begin
Source.Position := 0;
Count := Source.Size;
end
else Count := Min(Count, Source.Size - Source.Position);
if Count = 0 then exit;
Vector := InitVector;
while Count >= SizeOf(TDESBuffer) do
begin
Done := Source.Read(TempIn, SizeOf(TempIn));
if Done < SizeOf(TempIn) then
raise EStreamError.Create(SReadError);
PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
EncryptDES(TempIn, ExpandedKey, TempOut);
Done := Dest.Write(TempOut, SizeOf(TempOut));
if Done < SizeOf(TempOut) then
raise EStreamError.Create(SWriteError);
Vector := TempOut;
Dec(Count, SizeOf(TDESBuffer));
end;
if Count > 0 then
begin
Done := Source.Read(TempIn, Count);
if Done < Count then
raise EStreamError.Create(SReadError);
FillChar(TempIn[Count], SizeOf(TempIn) - Count, 0);
PLongWord(@TempIn[0])^ := PLongWord(@TempIn[0])^ xor PLongWord(@Vector[0])^;
PLongWord(@TempIn[4])^ := PLongWord(@TempIn[4])^ xor PLongWord(@Vector[4])^;
EncryptDES(TempIn, ExpandedKey, TempOut);
Done := Dest.Write(TempOut, SizeOf(TempOut));
if Done < SizeOf(TempOut) then
raise EStreamError.Create(SWriteError);
end;
end;

procedure DecryptDESStreamCBC(Source: TStream; Count: cardinal;
const Key: TDESKey; const InitVector: TDESBuffer; Dest: TStream);
var
ExpandedKey: TDESExpandedKey;
begin
ExpandDESKey(Key, ExpandedKey);
DecryptDESStreamCBC(Source, Count, ExpandedKey, InitVector, Dest);
end;

procedure DecryptDESStreamCBC(Source: TStream; Count: cardinal;
const ExpandedKey: TDESExpandedKey; const InitVector: TDESBuffer;
Dest: TStream);
var
TempIn, TempOut: TDESBuffer;
Vector1, Vector2: TDESBuffer;
Done: cardinal;
begin
if Count = 0 then
begin
Source.Position := 0;
Count := Source.Size;
end
else Count := Min(Count, Source.Size - Source.Position);
if Count = 0 then exit;
if (Count mod SizeOf(TDESBuffer)) > 0 then
raise EDESError.Create(SInvalidInBufSize);
Vector1 := InitVector;
while Count >= SizeOf(TDESBuffer) do
begin
Done := Source.Read(TempIn, SizeOf(TempIn));
if Done < SizeOf(TempIn) then
raise EStreamError(SReadError);
Vector2 := TempIn;
DecryptDES(TempIn, ExpandedKey, TempOut);
PLongWord(@TempOut[0])^ := PLongWord(@TempOut[0])^ xor PLongWord(@Vector1[0])^;
PLongWord(@TempOut[4])^ := PLongWord(@TempOut[4])^ xor PLongWord(@Vector1[4])^;
Done := Dest.Write(TempOut, SizeOf(TempOut));
if Done < SizeOf(TempOut) then
raise EStreamError(SWriteError);
Vector1 := Vector2;
Dec(Count, SizeOf(TDESBuffer));
end;
end;

Procedure _SetDESStringKey(Const AKey:String;var desKey:PDESExpandedKey);
var
TempDESKey:TDESKey;
len:Integer;
begin
len:=Length(AKey);
Fillchar(TEmpDesKey,sizeof(TDesKey),#0);
if len>sizeof(TDESKey) then len:=sizeof(TDESKey);
move(AKey[1],TempDeskey,len);
ExpandDESKey(TempDesKey,deskey^);

end;

end.
 
//**************************************************************************
//*Des加密算法
//*
//*
//*
//*
//*
//*
//*王锐(wr960204武稀松)2002-3-15
//*Email:wr_960204@x263.net
//*QQ:42088303
//**************************************************************************
unit WNDES;

interface

uses
Windows, Messages, ShlObj, WinSock, IniFiles, ActiveX, SysUtils,
Classes, Graphics, Controls, Forms, Dialogs;

type
TKeyByte = array[0..5] of Byte;
TDesMode = (dmEncry, dmDESry);
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);
//////////////////DES加密算法////////////////////////////
function EncryStr(Str, Key: string): string; //加密
function EncryStrHex(Str, Key: string): string; //以十六进制加密(有些时侯加密的密文含有特殊字符文本框不能显示、数据库文本字段也不能存储。以十六进制形式表示可保证绝对不会出现该种情况)
function DESryStr(Str, Key: string): string; //解密
function DESryStrHex(StrHex, Key: string): string; //解密十六进制加密的密文

var
subKey: array[0..15] of TKeyByte;
implementation



procedure 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 shr 3] and (1 shl (7 - (BitIP 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 := newData;
end;

procedure conversePermutation(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[BitCP shr 3] and (1 shl (7 - (BitCP 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 := newData;
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 shr 3] and (1 shl (7 - (BitExp 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 shr 3] and (1 shl (7 - (BitPM 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 := newData;
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[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 shr 3] and (1 shl (7 - (BitPMC1 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 shr 3] and (1 shl (7 - (BitPMC2 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);
cycleMove(key28r, bitDisplace);
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);
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 := outBuf xor subKey;
// outBuf xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
buf[0] := outBuf[0] shr 2; //xxxxxx -> 2
buf[1] := ((outBuf[0] and $03) shl 4) or (outBuf[1] shr 4); // 4 <- xx xxxx -> 4
buf[2] := ((outBuf[1] and $0F) shl 2) or (outBuf[2] shr 6); // 2 <- xxxx xx -> 6
buf[3] := outBuf[2] and $3F; // xxxxxx
buf[4] := outBuf[3] shr 2; // xxxxxx
buf[5] := ((outBuf[3] and $03) shl 4) or (outBuf[4] shr 4); // xx xxxx
buf[6] := ((outBuf[4] and $0F) shl 2) or (outBuf[5] shr 6); // xxxx xx
buf[7] := outBuf[5] and $3F; // xxxxxx
for i := 0 to 7 do buf := si(i, buf);
for i := 0 to 3 do outBuf := (buf[i * 2] shl 4) or buf[i * 2 + 1];
permutation(outBuf);
for i := 0 to 3 do outData := outBuf;
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 := inData;
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, 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 = dmDESry 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, 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;

function EncryStr(Str, Key: string): string;
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 DESryStr(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(dmDESry, 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 DESryStrHex(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 := DESryStr(Str, Key);
end;


end.

 
TurboPower的LockBox,很快搞定.
 
关键是调用,我有des算法的代码
如何用?
 
你的算法啥样啊,整出来瞧瞧,调用算法应该很容易的。
 
Edit1.Text := EncryStr(Edit2.Text, '这里是密钥');//加密
Edit3.Text := DESryStr(Edit1.Text , '这里是密钥'); //解密

其它两个函数同样使用。
你可以替换“这里是密钥”这个文字,他就是你设定的密钥,不过必须保证加密和解密的密钥是一样的,否则你得不到正确答案的。
 
DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
  DES算法是这样工作的:如Mode为加密,则用Key 去把数据Data进行加密, 生成Data的密码形式(64位)作为DES的输出结果;如Mode为解密,则用Key去把密码形式的数据Data解密,还原为Data的明码形式(64位)作为DES的输出结果。在通信网络的两端,双方约定一致的Key,在通信的源点用Key对核心数据进行DES加密,然后以密码形式在公共通信网(如电话网)中传输到通信网络的终点,数据到达目的地后,用同样的Key对密码数据进行解密,便再现了明码形式的核心数据。这样,便保证了核心数据(如PIN、MAC等)在公共通信网中传输的安全性和可靠性。
也就是说不管是加密的数据还是KEY都要是8的倍数才行,我是这么认为的,你觉得呢?
 
原来你这个封装不完整,你看看EncryptDESStreamECB和DecryptDESStreamECB这两个方法调用方法都在里面了。

这是一个例子:
function DecrptString(AKey, S: string): string;
var
I, Count, J: Integer;
Key: TDESKey;
ExKey: TDESExpandedKey;
TempIn, TempOut: TDESBuffer;
OutStr: string;
begin
//把传入的密匙赋值给Key;
for I := Low(Key) to High(Key) do
Key := Byte(AKey[I + 1]);
ExpandDESKey(Key, ExKey);
Count := Length(S);
while Count > 0 do
begin
if Count > SizeOf(TDESBuffer) then
J := SizeOf(TDESBuffer)
else J := Count;
for I := 0 to J do
TempIn := Byte(S[I + 1]);
Delete(S, 1, SizeOf(TDESBuffer));
DecryptDES(TempIn, ExKey, TempOut);
for I := 0 to J do
OutStr[I + 1] := Char(TempOut);
Result := Result + OutStr;
Dec(Count, SizeOf(TDESBuffer));
end;
end;

 
可以同lockbox控件,很容易实现的。www.51delphi.com
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
584
import
I
I
回复
0
查看
876
import
I
后退
顶部