📄 twofish.pas
字号:
Inc(i,2);
end;
Xor256(@L0,@p8x8[1],(sboxKeys[1] shr 8) and $FF);
A:= (sboxKeys[0] shr 8) and $FF;
i:= 0;
while i< 256 do
begin
sBox[1 and 2,2*i+(1 and 1)]:= MDS[1,p8x8[0,L0[i]] xor A];
sBox[1 and 2,2*i+(1 and 1)+2]:= MDS[1,p8x8[0,L0[i+1]] xor A];
Inc(i,2);
end;
Xor256(@L0,@p8x8[0],(sboxKeys[1] shr 16) and $FF);
A:= (sboxKeys[0] shr 16) and $FF;
i:= 0;
while i< 256 do
begin
sBox[2 and 2,2*i+(2 and 1)]:= MDS[2,p8x8[1,L0[i]] xor A];
sBox[2 and 2,2*i+(2 and 1)+2]:= MDS[2,p8x8[1,L0[i+1]] xor A];
Inc(i,2);
end;
Xor256(@L0,@p8x8[1],(sboxKeys[1] shr 24));
A:= (sboxKeys[0] shr 24);
i:= 0;
while i< 256 do
begin
sBox[3 and 2,2*i+(3 and 1)]:= MDS[3,p8x8[1,L0[i]] xor A];
sBox[3 and 2,2*i+(3 and 1)+2]:= MDS[3,p8x8[1,L0[i+1]] xor A];
Inc(i,2);
end;
end;
192: begin
Xor256(@L0,@p8x8[1],sboxKeys[2] and $FF);
A:= sboxKeys[0] and $FF;
B:= sboxKeys[1] and $FF;
i:= 0;
while i< 256 do
begin
sBox[0 and 2,2*i+(0 and 1)]:= MDS[0,p8x8[0,p8x8[0,L0[i]] xor B] xor A];
sBox[0 and 2,2*i+(0 and 1)+2]:= MDS[0,p8x8[0,p8x8[0,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
Xor256(@L0,@p8x8[1],(sboxKeys[2] shr 8) and $FF);
A:= (sboxKeys[0] shr 8) and $FF;
B:= (sboxKeys[1] shr 8) and $FF;
i:= 0;
while i< 256 do
begin
sBox[1 and 2,2*i+(1 and 1)]:= MDS[1,p8x8[0,p8x8[1,L0[i]] xor B] xor A];
sBox[1 and 2,2*i+(1 and 1)+2]:= MDS[1,p8x8[0,p8x8[1,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
Xor256(@L0,@p8x8[0],(sboxKeys[2] shr 16) and $FF);
A:= (sboxKeys[0] shr 16) and $FF;
B:= (sboxKeys[1] shr 16) and $FF;
i:= 0;
while i< 256 do
begin
sBox[2 and 2,2*i+(2 and 1)]:= MDS[2,p8x8[1,p8x8[0,L0[i]] xor B] xor A];
sBox[2 and 2,2*i+(2 and 1)+2]:= MDS[2,p8x8[1,p8x8[0,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
Xor256(@L0,@p8x8[0],(sboxKeys[2] shr 24));
A:= (sboxKeys[0] shr 24);
B:= (sboxKeys[1] shr 24);
i:= 0;
while i< 256 do
begin
sBox[3 and 2,2*i+(3 and 1)]:= MDS[3,p8x8[1,p8x8[1,L0[i]] xor B] xor A];
sBox[3 and 2,2*i+(3 and 1)+2]:= MDS[3,p8x8[1,p8x8[1,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
end;
256: begin
Xor256(@L1,@p8x8[1],(sboxKeys[3]) and $FF);
i:= 0;
while i< 256 do
begin
L0[i ]:= p8x8[1,L1[i]];
L0[i+1]:= p8x8[1,L1[i+1]];
Inc(i,2);
end;
Xor256(@L0,@L0,(sboxKeys[2]) and $FF);
A:= (sboxKeys[0]) and $FF;
B:= (sboxKeys[1]) and $FF;
i:= 0;
while i< 256 do
begin
sBox[0 and 2,2*i+(0 and 1)]:= MDS[0,p8x8[0,p8x8[0,L0[i]] xor B] xor A];
sBox[0 and 2,2*i+(0 and 1)+2]:= MDS[0,p8x8[0,p8x8[0,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
Xor256(@L1,@p8x8[0],(sboxKeys[3] shr 8) and $FF);
i:= 0;
while i< 256 do
begin
L0[i ]:= p8x8[1,L1[i]];
L0[i+1]:= p8x8[1,L1[i+1]];
Inc(i,2);
end;
Xor256(@L0,@L0,(sboxKeys[2] shr 8) and $FF);
A:= (sboxKeys[0] shr 8) and $FF;
B:= (sboxKeys[1] shr 8) and $FF;
i:= 0;
while i< 256 do
begin
sBox[1 and 2,2*i+(1 and 1)]:= MDS[1,p8x8[0,p8x8[1,L0[i]] xor B] xor A];
sBox[1 and 2,2*i+(1 and 1)+2]:= MDS[1,p8x8[0,p8x8[1,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
Xor256(@L1,@p8x8[0],(sboxKeys[3] shr 16) and $FF);
i:= 0;
while i< 256 do
begin
L0[i ]:= p8x8[0,L1[i]];
L0[i+1]:= p8x8[0,L1[i+1]];
Inc(i,2);
end;
Xor256(@L0,@L0,(sboxKeys[2] shr 16) and $FF);
A:= (sboxKeys[0] shr 16) and $FF;
B:= (sboxKeys[1] shr 16) and $FF;
i:= 0;
while i< 256 do
begin
sBox[2 and 2,2*i+(2 and 1)]:= MDS[2,p8x8[1,p8x8[0,L0[i]] xor B] xor A];
sBox[2 and 2,2*i+(2 and 1)+2]:= MDS[2,p8x8[1,p8x8[0,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
Xor256(@L1,@p8x8[1],(sboxKeys[3] shr 24));
i:= 0;
while i< 256 do
begin
L0[i ]:= p8x8[0,L1[i]];
L0[i+1]:= p8x8[0,L1[i+1]];
Inc(i,2);
end;
Xor256(@L0,@L0,(sboxKeys[2] shr 24));
A:= (sboxKeys[0] shr 24);
B:= (sboxKeys[1] shr 24);
i:= 0;
while i< 256 do
begin
sBox[3 and 2,2*i+(3 and 1)]:= MDS[3,p8x8[1,p8x8[1,L0[i]] xor B] xor A];
sBox[3 and 2,2*i+(3 and 1)+2]:= MDS[3,p8x8[1,p8x8[1,L0[i+1]] xor B] xor A];
Inc(i,2);
end;
end;
end;
{ Generate a "random" IV }
if InitVector= nil then
begin
FillChar(IV^,BS,0);
EncryptECB(IV^,IV^);
Reset;
end
else
begin
Move(InitVector^,IV^,BS);
Reset;
end;
end;
procedure TDCP_twofish.Burn;
begin
FillChar(sBox,Sizeof(sBox),$FF);
FillChar(SubKeys,Sizeof(SubKeys),$FF);
inherited Burn;
end;
procedure TDCP_twofish.EncryptECB(const InData; var OutData);
var
i: longint;
t0, t1: DWord;
X: array[0..3] of DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
x[0]:= PDWord(@InData)^ xor SubKeys[INPUTWHITEN];
x[1]:= PDWord(longint(@InData)+4)^ xor SubKeys[INPUTWHITEN+1];
x[2]:= PDWord(longint(@InData)+8)^ xor SubKeys[INPUTWHITEN+2];
x[3]:= PDWord(longint(@InData)+12)^ xor SubKeys[INPUTWHITEN+3];
i:= 0;
while i<= NUMROUNDS-2 do
begin
t0:= sBox[0,(x[0] shl 1) and $1fe] xor sBox[0,((x[0] shr 7) and $1fe)+1]
xor sBox[2,(x[0] shr 15) and $1fe] xor sBox[2,((x[0] shr 23) and $1fe)+1];
t1:= sBox[0,((x[1] shr 23) and $1fe)] xor sBox[0,((x[1] shl 1) and $1fe)+1]
xor sBox[2,((x[1] shr 7) and $1fe)] xor sBox[2,((x[1] shr 15) and $1fe)+1];
x[3]:= (x[3] shl 1) or (x[3] shr 31);
x[2]:= x[2] xor (t0 + t1 + SubKeys[ROUNDSUBKEYS+2*i]);
x[3]:= x[3] xor (t0 + 2*t1 + SubKeys[ROUNDSUBKEYS+2*i+1]);
x[2]:= (x[2] shr 1) or (x[2] shl 31);
t0:= sBox[0,(x[2] shl 1) and $1fe] xor sBox[0,((x[2] shr 7) and $1fe)+1]
xor sBox[2,((x[2] shr 15) and $1fe)] xor sBox[2,((x[2] shr 23) and $1fe)+1];
t1:= sBox[0,((x[3] shr 23) and $1fe)] xor sBox[0,((x[3] shl 1) and $1fe)+1]
xor sBox[2,((x[3] shr 7) and $1fe)] xor sBox[2,((x[3] shr 15) and $1fe)+1];
x[1]:= (x[1] shl 1) or (x[1] shr 31);
x[0]:= x[0] xor (t0 + t1 + SubKeys[ROUNDSUBKEYS+2*(i+1)]);
x[1]:= x[1] xor (t0 + 2*t1 + SubKeys[ROUNDSUBKEYS+2*(i+1)+1]);
x[0]:= (x[0] shr 1) or (x[0] shl 31);
Inc(i,2);
end;
PDWord(longint(@OutData)+ 0)^:= x[2] xor SubKeys[OUTPUTWHITEN];
PDWord(longint(@OutData)+ 4)^:= x[3] xor SubKeys[OUTPUTWHITEN+1];
PDWord(longint(@OutData)+ 8)^:= x[0] xor SubKeys[OUTPUTWHITEN+2];
PDWord(longint(@OutData)+12)^:= x[1] xor SubKeys[OUTPUTWHITEN+3];
end;
procedure TDCP_twofish.DecryptECB(const InData; var OutData);
var
i: longint;
t0, t1: DWord;
X: array[0..3] of DWord;
begin
if not fInitialized then
raise EDCP_blockcipher.Create('Cipher not initialized');
X[2]:= PDWord(@InData)^ xor SubKeys[OUTPUTWHITEN];
X[3]:= PDWord(longint(@InData)+4)^ xor SubKeys[OUTPUTWHITEN+1];
X[0]:= PDWord(longint(@InData)+8)^ xor SubKeys[OUTPUTWHITEN+2];
X[1]:= PDWord(longint(@InData)+12)^ xor SubKeys[OUTPUTWHITEN+3];
i:= NUMROUNDS-2;
while i>= 0 do
begin
t0:= sBox[0,(x[2] shl 1) and $1fe] xor sBox[0,((x[2] shr 7) and $1fe)+1]
xor sBox[2,((x[2] shr 15) and $1fe)] xor sBox[2,((x[2] shr 23) and $1fe)+1];
t1:= sBox[0,((x[3] shr 23) and $1fe)] xor sBox[0,((x[3] shl 1) and $1fe)+1]
xor sBox[2,((x[3] shr 7) and $1fe)] xor sBox[2,((x[3] shr 15) and $1fe)+1];
x[0]:= (x[0] shl 1) or (x[0] shr 31);
x[0]:= x[0] xor (t0 + t1 + SubKeys[ROUNDSUBKEYS+2*(i+1)]);
x[1]:= x[1] xor (t0 + 2*t1 + SubKeys[ROUNDSUBKEYS+2*(i+1)+1]);
x[1]:= (x[1] shr 1) or (x[1] shl 31);
t0:= sBox[0,(x[0] shl 1) and $1fe] xor sBox[0,((x[0] shr 7) and $1fe)+1]
xor sBox[2,(x[0] shr 15) and $1fe] xor sBox[2,((x[0] shr 23) and $1fe)+1];
t1:= sBox[0,((x[1] shr 23) and $1fe)] xor sBox[0,((x[1] shl 1) and $1fe)+1]
xor sBox[2,((x[1] shr 7) and $1fe)] xor sBox[2,((x[1] shr 15) and $1fe)+1];
x[2]:= (x[2] shl 1) or (x[2] shr 31);
x[2]:= x[2] xor (t0 + t1 + SubKeys[ROUNDSUBKEYS+2*i]);
x[3]:= x[3] xor (t0 + 2*t1 + SubKeys[ROUNDSUBKEYS+2*i+1]);
x[3]:= (x[3] shr 1) or (x[3] shl 31);
Dec(i,2);
end;
PDWord(longint(@OutData)+ 0)^:= X[0] xor SubKeys[INPUTWHITEN];
PDWord(longint(@OutData)+ 4)^:= X[1] xor SubKeys[INPUTWHITEN+1];
PDWord(longint(@OutData)+ 8)^:= X[2] xor SubKeys[INPUTWHITEN+2];
PDWord(longint(@OutData)+12)^:= X[3] xor SubKeys[INPUTWHITEN+3];
end;
procedure PreCompMDS;
var
m1, mx, my: array[0..1] of DWord;
nI: longint;
begin
for nI:= 0 to 255 do
begin
m1[0]:= p8x8[0,nI];
mx[0]:= Mul_X(m1[0]);
my[0]:= Mul_Y(m1[0]);
m1[1]:= p8x8[1,nI];
mx[1]:= Mul_X(m1[1]);
my[1]:= Mul_Y(m1[1]);
mds[0,nI]:= (m1[1] shl 0) or
(mx[1] shl 8) or
(my[1] shl 16) or
(my[1] shl 24);
mds[1,nI]:= (my[0] shl 0) or
(my[0] shl 8) or
(mx[0] shl 16) or
(m1[0] shl 24);
mds[2,nI]:= (mx[1] shl 0) or
(my[1] shl 8) or
(m1[1] shl 16) or
(my[1] shl 24);
mds[3,nI]:= (mx[0] shl 0) or
(m1[0] shl 8) or
(my[0] shl 16) or
(mx[0] shl 24);
end;
end;
constructor TDCP_twofish.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if not MDSDone then
begin
PreCompMDS;
MDSDone:= true;
end;
end;
initialization
MDSdone:= false;
RegisterClass(TDCP_twofish);
DCPregcipher(TDCP_twofish,true);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -