DES在C#和DELPHI下的实现
DES在C#下是由des来实现的,需要KEY和IV,其中如果KEY或者IV不足8字节,用#0 来补足 代码如下:
DELPHI下要实现相应的加密和解密,需要自己写DES代码,并加上CBC方式的异或
这里要说明一点,我发现C#下DES明文在加密前,有补足8的倍数的情况,即如果如果是“11111”就会补成“11111#3#3#3”,如果是“11111111”就会“11111111#8#8#8#8#8#8#8#8”,然后在进行异或,最后加密!
演示代码如下:
byte[] key = new byte[8];
byte[] iv = new byte[8];
for (int i = 0;i < 8 ;i ++)
{
if (i < temp.Length)
{
key[i] = temp[i];
}
else
{
key[i] = 0;
}
if (i < temp1.Length)
{
iv[i] = temp1[i];
}
else
{
iv[i] = 0;
}
}
private string EncryptString(string Value,byte[] key,byte[] iv)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
//des.KeySize = key.Length;
//des.ValidKeySize(key.Length);
//ct = des.CreateEncryptor(key,iv);
des.Key = key;
des.IV = iv;
//des.GenerateIV();
ct = des.CreateEncryptor();
//des.GenerateIV;
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
byte[] bytesCipher=ms.ToArray();
return Convert.ToBase64String(bytesCipher);
}
private string DecryptString(string Value,byte[] key,byte[] iv)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
des.Key = key;
des.IV = iv;
ct = des.CreateDecryptor();
//ct = des.CreateDecryptor(key,iv);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
byte[] bytesCipher = ms.ToArray();
return Encoding.UTF8.GetString(bytesCipher);
}
byte[] iv = new byte[8];
for (int i = 0;i < 8 ;i ++)
{
if (i < temp.Length)
{
key[i] = temp[i];
}
else
{
key[i] = 0;
}
if (i < temp1.Length)
{
iv[i] = temp1[i];
}
else
{
iv[i] = 0;
}
}
private string EncryptString(string Value,byte[] key,byte[] iv)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
//des.KeySize = key.Length;
//des.ValidKeySize(key.Length);
//ct = des.CreateEncryptor(key,iv);
des.Key = key;
des.IV = iv;
//des.GenerateIV();
ct = des.CreateEncryptor();
//des.GenerateIV;
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
byte[] bytesCipher=ms.ToArray();
return Convert.ToBase64String(bytesCipher);
}
private string DecryptString(string Value,byte[] key,byte[] iv)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
des.Key = key;
des.IV = iv;
ct = des.CreateDecryptor();
//ct = des.CreateDecryptor(key,iv);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
byte[] bytesCipher = ms.ToArray();
return Encoding.UTF8.GetString(bytesCipher);
}
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);
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;
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;