des算法的delphi原代码或控件 (200分)

  • 主题发起人 主题发起人 wwwljy
  • 开始时间 开始时间
W

wwwljy

Unregistered / Unconfirmed
GUEST, unregistred user!
各位:
本人编程时用到des算法,但手头无DES 的delphi原代码,那位好人能够提供相关资料信息给我,
谢谢!!!!!!!!
 
C语言的要么?windows下应该可以编译成dll调用的
 
好,先谢谢你啦!
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=646310
不过结果我不是很满意。最终用了一个C的。Delphi的好像都是改进(退)过的。
 
留个邮箱吧,有点大,这里贴不下,是我在原来的c基础上改成c++的,虽然是用在嵌入式
操作系统中,但是应该很容易改成windows下的(改unix版也很简单)。

支持标准DES加密和3DES-ECB模式加密,加密块为8字节,密钥为64位。
 
zyy04:
我的邮箱:delphi163@163.net,给我发过来吧:)
 
下面是我收藏的DELPHI版的des算法资料,你可以参考参考
unit des32;

interface

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

implementation

procedure 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,19,
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,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1);

Var
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 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 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是什么东东?
 
QStrings.pas 单元,内有很多高速 string 操作函数,包括 DES & DES32, 全是汇编优化,
速度是我见过最快的,超过C写的,本人一直在用,支持CLX,挺好的,吐血推荐!(俄罗斯出品):)
 
我下载了http://www.dbwork.kuban.ru/download/qstrings.zip,版本6.03.420
加密算法倒是有,那个是DES的?
procedure IntRC6EncryptECB(ID: TRC6ID; P: Pointer);
procedure IntRC6DecryptECB(ID: TRC6ID; P: Pointer);
 
不客气的说,aq100提供的TEncryption执行的结果,和我手头有的C的标准DES加密的结果不一样。
应该,同样的明文,同样的密钥,同样的加密结果才对。

可是我收集的不下于15个的Delphi版的DES加密实现,居然没有一个和标准的执行结果一样,尤其
好玩儿的是,这十来个号称“标准加密”算法的执行结果没有两个是相同的!不知道他们是怎样加
密的!大家有试明白的,可以告诉我。
 
呵呵,不好意思,里面有 CRC CRC32 RC6 SHA ,偏偏没 DES,那是我记错了 :(
不过我刚查了一下,我机器上还有 DCPciphers, Cipher 等几个加密包,都有 DES 算法
还有独立的DES32, DESUNIT 不过都不是汇编写的了:(
我发给你? very very 不好意思 :)
 
aq100提供源码就叫Des32,dfw里许多富翁都贴过DesUnit,这两个东东我都作过测试,如我在前所言,
他们的结果都不一样,不像是标准的算法。我就想要一个标准的算法。
谢谢轻松虎。
不好意思,借用楼主宝地。
 
拜托!大家如果想对这些标准发表看法的话,能不能先研究一下DES的标准?
该标准文档很容易找到的,中文版的好像也有。

大家注意没有在加密函数中有10几个静态数组,如果你使用的数组与别人不同是允许的,
当然加密结果也就不一样了。
第二个:即使所使用的常数数组相同,但是加密块长度和秘钥长度不同的话,加密结果
也未必相同。
第三:现在很多DES控件都支持各种3DES加密,向我发的cpp文件中就支持3des-ecb加密,
你确定测试时调用的函数正确么?
 
to aq100:
这段代码我也有。发现加密密钥只能设置成
8位的。改变密钥长度应该怎么做啊??我是新手!!!
 
//**************************************************************************
//*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的那一套也可以呀, 也包含DES加密的组件, 另外也有很多这样的组件啊

我手头上有一个,下面是介绍,好像是在 www.torry.net 上面下载的
里面就有

Delphi Encryption Compedium Part I v.3.0 FWS 677 Kb 31.08.99
By Hagen Reddmann. A Compendium of various strong Encryption Algorithm
and more... includes:
40 Ciphers (IDEA, Blowfish, Twofish, DES, Square ...)
23 Hashs (MD4/5, SHA/1, RipeMD, Havel, Tiger ...)
5 Checksums (CRC32, CRC16-CCITT, CRC16-Standard ...)
6 Textformats (MIME Base 64, XX/UU Coding, RFC1760 Six Word ...)
2 Randoms (LFSR Linear Feedback Shift Register with variable Period
upto 2^2032-1 ...)
with Ciphers can be encrypt in Modes CTS/ CBC/ CFB/ OFB/ ECB
Ciphers supports Message Authentication Codes in CBC-MAC/ CFB-MAC/ CTS-MAC
fast Implementation (i.e. Blowfish > 8Mb/sec, MD4 > 27Mb/sec) Internet
RFC2289/RFC1760/RFC2444 One Time Password Routines as Component
Many more...


Fully functional
Source: Included
Exe-demo included
Download: CB3 CB4 D2 D3 D4 D5
 
后退
顶部