📄 dcpdes.pas
字号:
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of DES and Triple DES *******************}
{* Based on C source code by Eric Young ***************************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
{******************************************************************************}
{* This implementation of DES is based on the C implementation by *}
{* Eric Young (eay@mincom.oz.au) *}
{******************************************************************************}
{* DES takes a 64bit key and discards every 8th bit (56bit effectively) *}
{* 3DES takes either a <= 128bit key and uses one key twice or takes a *}
{* <= 192bit key and uses each once (again discarding every 8th bit) *}
{******************************************************************************}
unit DCPdes;
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_customdes= class(TDCP_blockcipher64)
protected
procedure DoInit(KeyB: PByteArray; KeyData: PDWordArray);
procedure EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
procedure DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
end;
type
TDCP_des= class(TDCP_customdes)
protected
KeyData: array[0..31] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
TDCP_3des= class(TDCP_customdes)
protected
KeyData: array[0..2,0..31] of dword;
procedure InitKey(const Key; Size: longword); override;
public
class function GetId: integer; override;
class function GetAlgorithm: string; override;
class function GetMaxKeySize: integer; override;
class function SelfTest: boolean; override;
procedure Burn; override;
procedure EncryptECB(const InData; var OutData); override;
procedure DecryptECB(const InData; var OutData); override;
end;
{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}
{$I DCPdes.inc}
procedure hperm_op(var a, t: dword; n, m: dword);
begin
t:= ((a shl (16 - n)) xor a) and m;
a:= a xor t xor (t shr (16 - n));
end;
procedure perm_op(var a, b, t: dword; n, m: dword);
begin
t:= ((a shr n) xor b) and m;
b:= b xor t;
a:= a xor (t shl n);
end;
procedure TDCP_customdes.DoInit(KeyB: PByteArray; KeyData: PDwordArray);
var
c, d, t, s, t2, i: dword;
begin
c:= KeyB^[0] or (KeyB^[1] shl 8) or (KeyB^[2] shl 16) or (KeyB^[3] shl 24);
d:= KeyB^[4] or (KeyB^[5] shl 8) or (KeyB^[6] shl 16) or (KeyB^[7] shl 24);
perm_op(d,c,t,4,$0f0f0f0f);
hperm_op(c,t,dword(-2),$cccc0000);
hperm_op(d,t,dword(-2),$cccc0000);
perm_op(d,c,t,1,$55555555);
perm_op(c,d,t,8,$00ff00ff);
perm_op(d,c,t,1,$55555555);
d:= ((d and $ff) shl 16) or (d and $ff00) or ((d and $ff0000) shr 16) or
((c and $f0000000) shr 4);
c:= c and $fffffff;
for i:= 0 to 15 do
begin
if shifts2[i]<> 0 then
begin
c:= ((c shr 2) or (c shl 26));
d:= ((d shr 2) or (d shl 26));
end
else
begin
c:= ((c shr 1) or (c shl 27));
d:= ((d shr 1) or (d shl 27));
end;
c:= c and $fffffff;
d:= d and $fffffff;
s:= des_skb[0,c and $3f] or
des_skb[1,((c shr 6) and $03) or ((c shr 7) and $3c)] or
des_skb[2,((c shr 13) and $0f) or ((c shr 14) and $30)] or
des_skb[3,((c shr 20) and $01) or ((c shr 21) and $06) or ((c shr 22) and $38)];
t:= des_skb[4,d and $3f] or
des_skb[5,((d shr 7) and $03) or ((d shr 8) and $3c)] or
des_skb[6, (d shr 15) and $3f ] or
des_skb[7,((d shr 21) and $0f) or ((d shr 22) and $30)];
t2:= ((t shl 16) or (s and $ffff));
KeyData^[(i shl 1)]:= ((t2 shl 2) or (t2 shr 30));
t2:= ((s shr 16) or (t and $ffff0000));
KeyData^[(i shl 1)+1]:= ((t2 shl 6) or (t2 shr 26));
end;
end;
procedure TDCP_customdes.EncryptBlock(const InData; var OutData; KeyData: PDWordArray);
var
l, r, t, u: dword;
i: longint;
begin
r:= PDword(@InData)^;
l:= PDword(dword(@InData)+4)^;
t:= ((l shr 4) xor r) and $0f0f0f0f;
r:= r xor t;
l:= l xor (t shl 4);
t:= ((r shr 16) xor l) and $0000ffff;
l:= l xor t;
r:= r xor (t shl 16);
t:= ((l shr 2) xor r) and $33333333;
r:= r xor t;
l:= l xor (t shl 2);
t:= ((r shr 8) xor l) and $00ff00ff;
l:= l xor t;
r:= r xor (t shl 8);
t:= ((l shr 1) xor r) and $55555555;
r:= r xor t;
l:= l xor (t shl 1);
r:= (r shr 29) or (r shl 3);
l:= (l shr 29) or (l shl 3);
i:= 0;
while i< 32 do
begin
u:= r xor KeyData^[i ];
t:= r xor KeyData^[i+1];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i+2];
t:= l xor KeyData^[i+3];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= r xor KeyData^[i+4];
t:= r xor KeyData^[i+5];
t:= (t shr 4) or (t shl 28);
l:= l xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
u:= l xor KeyData^[i+6];
t:= l xor KeyData^[i+7];
t:= (t shr 4) or (t shl 28);
r:= r xor des_SPtrans[0,(u shr 2) and $3f] xor
des_SPtrans[2,(u shr 10) and $3f] xor
des_SPtrans[4,(u shr 18) and $3f] xor
des_SPtrans[6,(u shr 26) and $3f] xor
des_SPtrans[1,(t shr 2) and $3f] xor
des_SPtrans[3,(t shr 10) and $3f] xor
des_SPtrans[5,(t shr 18) and $3f] xor
des_SPtrans[7,(t shr 26) and $3f];
Inc(i,8);
end;
r:= (r shr 3) or (r shl 29);
l:= (l shr 3) or (l shl 29);
t:= ((r shr 1) xor l) and $55555555;
l:= l xor t;
r:= r xor (t shl 1);
t:= ((l shr 8) xor r) and $00ff00ff;
r:= r xor t;
l:= l xor (t shl 8);
t:= ((r shr 2) xor l) and $33333333;
l:= l xor t;
r:= r xor (t shl 2);
t:= ((l shr 16) xor r) and $0000ffff;
r:= r xor t;
l:= l xor (t shl 16);
t:= ((r shr 4) xor l) and $0f0f0f0f;
l:= l xor t;
r:= r xor (t shl 4);
PDword(@OutData)^:= l;
PDword(dword(@OutData)+4)^:= r;
end;
procedure TDCP_customdes.DecryptBlock(const InData; var OutData; KeyData: PDWordArray);
var
l, r, t, u: dword;
i: longint;
begin
r:= PDword(@InData)^;
l:= PDword(dword(@InData)+4)^;
t:= ((l shr 4) xor r) and $0f0f0f0f;
r:= r xor t;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -