uses EncdDecd; function EncryptDES(const aStr: string): string; function DecryptDES(const aEncry: string): string; var Key, iv: TKey64;
{ TEncry } procedure IniDES; var temp: string; I: integer; begin temp := '1234567890pass'; for i:=0 to 7 do begin if i > (length(temp)-1) then key[i] := 0 else key[i] := byte(temp[i+1]); end; temp := 'worderd'; for i:=0 to 7 do begin if i > (length(temp)-1) then iv[i]:=0 else iv[i] := byte(temp[i+1]); end; end;
function TEncry.DecryptDES(const aEncry: string): string; var Context : TDESContext; Encrypt: boolean; temp, res: string; posnum, i, J: integer; bak, Block: TDESBlock; begin IniDES(); Encrypt := false; InitEncryptDES(Key, Context, Encrypt); //for i:=0 to 7 do block[0] := 0; temp := DecodeString( aEncry ) ; 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, TDESBlock(IV), Block); for j:= 0 to 7 do begin // temp := temp+inttostr(byte(block[i]))+' '; res := res + char(block[j]); end; iv := TKey64(bak); posnum := 0; end; end; if posnum <> 0 then begin raise Exception.Create('解密出错'); end else begin temp:=''; //len := res[length(res)] ; for i:= 1 to length(res) do begin temp := temp+char(res[i]); end; Result := trim(temp); end; end;
function TEncry.EncryptDES(const aStr: string): string; var Context : TDESContext; Encrypt: boolean; temp, res: string; len, posnum, i, J: integer; poschar: Char; Block: TDESBlock; begin IniDES(); Encrypt := True; InitEncryptDES(Key, Context, Encrypt); temp := aStr; 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, TDESBlock(IV), Block); for j:= 0 to 7 do begin res := res + char(block[j]); end; iv := TKey64(block); posnum := 0; end;
end; if posnum <> 0 then begin for i:=posnum to 7 do begin Block[i] := byte(poschar); end; EncryptDESCBC(Context, TDESBlock(IV), Block); for j:= 0 to 7 do begin res := res + char(block[j]); end; posnum := 0; end; // IdEncoderMIME1 Result := EncodeString(res); end;
C#实现
byte[] keyDES = new byte[8]; byte[] iv = new byte[8];
#region DESDelphi通用
private void iniDES() { byte[] temp = Encoding.ASCII.GetBytes("1234567890pass"); byte[] temp1 = Encoding.ASCII.GetBytes("worderd"); for (int i = 0; i < 8; i++) { if (i < temp.Length) { keyDES[i] = temp[i]; } else { keyDES[i] = 0; } if (i < temp1.Length) { iv[i] = temp1[i]; } else { iv[i] = 0; } } }
type TEncry = class public class procedure IniDES(aKey: AnsiString); class function EncryptDES(const aSrc, aKey: AnsiString): AnsiString; class function DecryptDES(const aEncry, aKey: AnsiString): AnsiString; end;
var Key, iv: TKey64;
implementation
{ TEncry }
class procedure TEncry.IniDES(aKey: AnsiString); var temp: AnsiString; I: integer; begin temp := '1234567890pass'; //aKey;// for i:=0 to 7 do begin if i > (length(temp)-1) then key[i] := 0 else key[i] := byte(temp[i+1]); end; temp := 'worderd'; for i:=0 to 7 do begin if i > (length(temp)-1) then iv[i]:=0 else iv[i] := byte(temp[i+1]); end; end;
class function TEncry.EncryptDES(const aSrc, aKey: AnsiString): AnsiString; var Context : TDESContext; Encrypt: boolean; temp, res: AnsiString; len, posnum, i, J: integer; poschar: AnsiChar; Block: TDESBlock; begin IniDES(aKey); Encrypt := True; InitEncryptDES(Key, Context, Encrypt); temp := aSrc; 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, TDESBlock(IV), Block); for j:= 0 to 7 do begin res := res + char(block[j]); end; iv := TKey64(block); posnum := 0; end;
end; if posnum <> 0 then begin for i:=posnum to 7 do begin Block[i] := byte(poschar); end; EncryptDESCBC(Context, TDESBlock(IV), Block); for j:= 0 to 7 do begin res := res + char(block[j]); end; posnum := 0; end; // IdEncoderMIME1 Result := EncodeString(res); end;
class function TEncry.DecryptDES(const aEncry, aKey: AnsiString): AnsiString; var Context : TDESContext; Encrypt: boolean; temp, res: string; posnum, i, J: integer; bak, Block: TDESBlock; begin IniDES(aKey); Encrypt := false; InitEncryptDES(Key, Context, Encrypt); //for i:=0 to 7 do block[0] := 0; temp := DecodeString( aEncry ) ; 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, TDESBlock(IV), Block); for j:= 0 to 7 do begin // temp := temp+inttostr(byte(block[i]))+' '; res := res + char(block[j]); end; iv := TKey64(bak); posnum := 0; end; end; if posnum <> 0 then begin raise Exception.Create('解密出错'); end else begin temp:=''; //len := res[length(res)] ; for i:= 1 to length(res) do begin temp := temp+char(res[i]); end; Result := trim(temp); end; end;