📄 des.~pas
字号:
unit des;
interface
uses
SysUtils,LbCipher,Windows, Classes, encddecd;
type
Tdes = class
iv:TDESBlock;
key:TKey64;
encodingType:string;
constructor Create();overload;
constructor Create(ikey:array of Byte;iiv:array of Byte;iencodingType:String );overload;
destructor Destroy; override;
public
function Encode(data:String):string;
function Decode(data:String):string;
end;
implementation
constructor Tdes.create();
begin
end;
destructor Tdes.Destroy;
begin
end;
constructor Tdes.create(ikey:array of Byte;iiv:array of Byte;iencodingType:String);
var
i:integer;
begin
for i:=0 to 7 do begin
key[i] :=ikey[i];
iv[i] :=iiv[i];
end;
encodingType:='gbk';
if(iencodingType='utf-8') then
begin
encodingType:='utf-8';
end;
end;
function Tdes.Encode(data:String):string;
var
Encrypt:bool;
temp,res:String;
i,j,len,posnum:integer;
Context : TDESContext;
poschar:char;
Block:TDESBlock;
begin
Encrypt := true;
InitEncryptDES(Key, Context, Encrypt);
temp :=data;
if(encodingType='utf-8') then
begin
temp := AnsiToUtf8(data);
end;
len := length(temp);
temp := temp+ char( 8- (len mod 8));
posnum := 0;
for i:=0 to len do begin
poschar:= temp[i+1];
Block[posnum] := byte(poschar);
posnum := posnum +1;
if posnum = 8 then begin
EncryptDESCBC(Context, IV, Block);
for j:= 0 to 7 do begin
res := res + char(block[j]);
end;
iv := block;
posnum := 0;
end;
end;
if posnum <> 0 then begin
for i:=posnum to 7 do begin
Block[i] := byte(poschar);
end;
EncryptDESCBC(Context, IV, Block);
for j:= 0 to 7 do begin
res := res + char(block[j]);
end;
posnum := 0;
end;
result:= EncodeString(res);
end;
function Tdes.Decode(data:String):string;
var
Encrypt:bool;
temp,res:String;
key:TKey64;
i,j,len,posnum:integer;
iv:TDESBlock;
Context : TDESContext;
poschar:char;
bak,Block:TDESBlock;
begin
Encrypt := false;
InitEncryptDES(Key, Context, Encrypt);
temp := DecodeString(data) ;
posnum := 0;
for i:=0 to length(temp)-1 do begin
Block[posnum] := byte(temp[i+1]);
posnum := posnum+1;
if posnum = 8 then begin
bak := block;
EncryptDESCBC(Context, IV, Block);
for j:= 0 to 7 do begin
// temp := temp+inttostr(byte(block[i]))+' ';
res := res + char(block[j]);
end;
iv := bak;
posnum := 0;
end;
end;
if posnum <> 0 then begin
result:='';
end else begin
temp:='';
//len := res[length(res)] ;
for i:= 1 to length(res) do begin
temp := temp+char(res[i]);
end;
temp :=trim(temp);
if(encodingType='utf-8') then
begin
temp := Utf8ToAnsi(temp);
end;
result :=temp ;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -