在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
byte[] key = new byte[8];
byte[] iv = new byte[8]; for (int i = 0;i < 8 ;i ++) } DELPHI下要实现相应的加密和解密,需要自己写DES代码,并加上CBC方式的异或 这里要说明一点,我发现C#下DES明文在加密前,有补足8的倍数的情况,即如果如果是“11111”就会补成“11111#3#3#3”,如果是“11111111”就会“11111111#8#8#8#8#8#8#8#8”,然后在进行异或,最后加密! 演示代码如下:
Encrypt := true;
temp := edit2.Text; res := ''; 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 := edit3.Text; for i:=0 to 7 do begin if i > (length(temp)-1) then iv[i]:=0 else iv[i] := byte(temp[i+1]); end; InitEncryptDES(Key, Context, Encrypt); temp := edit1.Text; 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; edit4.Text := IdEncoderMIME1.EncodeString(res);
Encrypt := false;
temp := edit2.Text; res := ''; 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 := edit3.Text; for i:=0 to 7 do begin if i > (length(temp)-1) then iv[i] := 0 else iv[i] := byte(temp[i+1]); end; InitEncryptDES(Key, Context, Encrypt); //for i:=0 to 7 do block[0] := 0; temp := IdDecoderMIME1.DecodeString(edit4.Text) ; 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 showmessage('解密出错'); end else begin temp:=''; //len := res[length(res)] ; for i:= 1 to length(res) do begin temp := temp+char(res[i]); end; edit1.Text := trim(temp); end; |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论