📄 cast128.pas
字号:
procedure Cast128EncryptECB;
var
t, l, r: DWord;
begin
Move(InData^,l,Sizeof(l));
Move(pointer(integer(InData)+4)^,r,Sizeof(r));
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
t:= LRot32(Data.xKey[0]+r, Data.xKey[0+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[1] xor l, Data.xKey[1+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[2]-r, Data.xKey[2+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[3]+l, Data.xKey[3+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[4] xor r, Data.xKey[4+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[5]-l, Data.xKey[5+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[6]+r, Data.xKey[6+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[7] xor l, Data.xKey[7+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[8]-r, Data.xKey[8+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[9]+l, Data.xKey[9+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[10] xor r, Data.xKey[10+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[11]-l, Data.xKey[11+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
if Data.Rounds> 12 then
begin
t:= LRot32(Data.xKey[12]+r, Data.xKey[12+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[13] xor l, Data.xKey[13+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[14]-r, Data.xKey[14+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[15]+l, Data.xKey[15+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
end;
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
Move(r,OutData^,Sizeof(r));
Move(l,pointer(integer(OutData)+4)^,Sizeof(l));
end;
procedure Cast128DecryptECB;
var
t, l, r: DWord;
begin
Move(InData^,r,Sizeof(l));
Move(pointer(integer(InData)+4)^,l,Sizeof(l));
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
if Data.Rounds> 12 then
begin
t:= LRot32(Data.xKey[15]+l, Data.xKey[15+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[14]-r, Data.xKey[14+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[13] xor l, Data.xKey[13+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[12]+r, Data.xKey[12+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
end;
t:= LRot32(Data.xKey[11]-l, Data.xKey[11+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[10] xor r, Data.xKey[10+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[9]+l, Data.xKey[9+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[8]-r, Data.xKey[8+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[7] xor l, Data.xKey[7+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[6]+r, Data.xKey[6+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[5]-l, Data.xKey[5+16]);
r:= r xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[4] xor r, Data.xKey[4+16]);
l:= l xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[3]+l, Data.xKey[3+16]);
r:= r xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[2]-r, Data.xKey[2+16]);
l:= l xor (((cast_sbox1[t shr 24] + cast_sbox2[(t shr 16) and $FF]) xor
cast_sbox3[(t shr 8) and $FF]) - cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[1] xor l, Data.xKey[1+16]);
r:= r xor (((cast_sbox1[t shr 24] - cast_sbox2[(t shr 16) and $FF]) +
cast_sbox3[(t shr 8) and $FF]) xor cast_sbox4[t and $FF]);
t:= LRot32(Data.xKey[0]+r, Data.xKey[0+16]);
l:= l xor (((cast_sbox1[t shr 24] xor cast_sbox2[(t shr 16) and $FF]) -
cast_sbox3[(t shr 8) and $FF]) + cast_sbox4[t and $FF]);
l:= (l shr 24) or ((l shr 8) and $FF00) or ((l shl 8) and $FF0000) or (l shl 24);
r:= (r shr 24) or ((r shr 8) and $FF00) or ((r shl 8) and $FF0000) or (r shl 24);
Move(l,OutData^,Sizeof(l));
Move(r,pointer(integer(OutData)+4)^,Sizeof(r));
end;
procedure Cast128EncryptCBC;
begin
XorBlock(InData,@Data.LastBlock,OutData,8);
Cast128EncryptECB(Data,OutData,OutData);
Move(OutData^,Data.LastBlock,8);
end;
procedure Cast128DecryptCBC;
var
TempBlock: array[0..7] of byte;
begin
Move(InData^,TempBlock,8);
Cast128DecryptECB(Data,InData,OutData);
XorBlock(OutData,@Data.LastBlock,OutData,8);
Move(TempBlock,Data.LastBlock,8);
end;
procedure Cast128EncryptCFB;
var
i: integer;
TempBlock: array[0..7] of byte;
begin
for i:= 0 to Len-1 do
begin
Cast128EncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
Move(Data.LastBlock[1],Data.LastBlock[0],7);
Data.LastBlock[7]:= PByteArray(OutData)[i];
end;
end;
procedure Cast128DecryptCFB;
var
i: integer;
TempBlock: array[0..7] of byte;
b: byte;
begin
for i:= 0 to Len-1 do
begin
b:= PByteArray(InData)[i];
Cast128EncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
Move(Data.LastBlock[1],Data.LastBlock[0],7);
Data.LastBlock[7]:= b;
end;
end;
procedure Cast128EncryptOFB;
begin
Cast128EncryptECB(Data,@Data.LastBlock,@Data.LastBlock);
XorBlock(@Data.LastBlock,InData,OutData,8);
end;
procedure Cast128DecryptOFB;
begin
Cast128EncryptECB(Data,@Data.LastBlock,@Data.LastBlock);
XorBlock(@Data.LastBlock,InData,OutData,8);
end;
procedure Cast128EncryptOFBC;
var
i: integer;
TempBlock: array[0..7] of byte;
begin
for i:= 0 to Len-1 do
begin
Cast128EncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
IncBlock(@Data.LastBlock,8);
end;
end;
procedure Cast128DecryptOFBC;
var
i: integer;
TempBlock: array[0..7] of byte;
begin
for i:= 0 to Len-1 do
begin
Cast128EncryptECB(Data,@Data.LastBlock,@TempBlock);
PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
IncBlock(@Data.LastBlock,8);
end;
end;
procedure Cast128Reset;
begin
Move(Data.InitBlock,Data.LastBlock,8);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -