📄 dcpserpent.pas
字号:
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Serpent ******************************}
{* Based on C source written by Brian Gladman (gladman@seven77.demon.co.uk) ***}
{* Thanks to Bruce Christensen for the initial Delphi translation *************}
{******************************************************************************}
{* Copyright (c) 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. *}
{******************************************************************************}
unit DCPserpent;
interface
uses
Classes, Sysutils, DCPcrypt2, DCPconst, DCPblockciphers;
type
TDCP_serpent= class(TDCP_blockcipher128)
protected
l_key: array[0..131] 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-}
class function TDCP_serpent.GetID: integer;
begin
Result:= DCP_serpent;
end;
class function TDCP_serpent.GetAlgorithm: string;
begin
Result:= 'Serpent';
end;
class function TDCP_serpent.GetMaxKeySize: integer;
begin
Result:= 256;
end;
class function TDCP_serpent.SelfTest: boolean;
const
Key1: array[0..15] of byte=
($ff,$ee,$dd,$cc,$bb,$aa,$99,$88,$77,$66,$55,$44,$33,$22,$11,$00);
InData1: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData1: array[0..15] of byte=
($d5,$ba,$a0,$0a,$4b,$b9,$d8,$a7,$c9,$81,$c8,$dc,$90,$d8,$9d,$92);
Key2: array[0..23] of byte=
($88,$99,$aa,$bb,$cc,$dd,$ee,$ff,$ff,$ee,$dd,$cc,$bb,$aa,$99,$88,
$77,$66,$55,$44,$33,$22,$11,$00);
InData2: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData2: array[0..15] of byte=
($da,$86,$08,$42,$b7,$20,$80,$2b,$f4,$04,$a4,$c7,$10,$34,$87,$9a);
Key3: array[0..31] of byte=
($00,$11,$22,$33,$44,$55,$66,$77,$88,$99,$aa,$bb,$cc,$dd,$ee,$ff,
$ff,$ee,$dd,$cc,$bb,$aa,$99,$88,$77,$66,$55,$44,$33,$22,$11,$00);
InData3: array[0..15] of byte=
($10,$32,$54,$76,$98,$ba,$dc,$fe,$ef,$cd,$ab,$89,$67,$45,$23,$01);
OutData3: array[0..15] of byte=
($93,$df,$9a,$3c,$af,$e3,$87,$bd,$99,$9e,$eb,$e3,$93,$a1,$7f,$ca);
var
Block: array[0..15] of byte;
Cipher: TDCP_serpent;
begin
Cipher:= TDCP_serpent.Create(nil);
Cipher.Init(Key1,Sizeof(Key1)*8,nil);
Cipher.EncryptECB(InData1,Block);
Result:= boolean(CompareMem(@Block,@OutData1,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData1,16));
Cipher.Init(Key2,Sizeof(Key2)*8,nil);
Cipher.EncryptECB(InData2,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData2,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData2,16));
Cipher.Init(Key3,Sizeof(Key3)*8,nil);
Cipher.EncryptECB(InData3,Block);
Result:= Result and boolean(CompareMem(@Block,@OutData3,16));
Cipher.DecryptECB(Block,Block);
Cipher.Burn;
Result:= Result and boolean(CompareMem(@Block,@InData3,16));
Cipher.Free;
end;
procedure TDCP_serpent.InitKey(const Key; Size: longword);
var
kp: array[0..139] of dword;
i, n: integer;
t, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17: dword;
a, b, c, d: dword;
begin
FillChar(kp,256 div 8,0);
Move(Key,kp,Size div 8);
if Size < 256 then
begin
i:= Size div 32;
t:= 1 shl (Size mod 32);
kp[i]:= (kp[i] and (t - 1)) or t;
end;
for i:= 8 to 139 do
begin
t:= kp[i - 8] xor kp[i - 5] xor kp[i - 3] xor kp[i - 1] xor $9e3779b9 xor longword(i-8);
kp[i]:= (t shl 11) or (t shr 21);
end;
for i:= 0 to 3 do
begin
n:= i*32;
a:= kp[n + 4*0 + 8]; b:= kp[n + 4*0 + 9]; c:= kp[n + 4*0 + 10]; d:= kp[n + 4*0 + 11];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -