多谢帮忙!
简单的DES,原始文件:<?xml version=\"1.0\" encoding=\"UTF-8\"?><ChatMember><ID /><Name /><Dept /><Tel/><email>q</email><Password>q</Password><Discription /><AdditionalInfo /><URL /><RejectReason /></ChatMember>加密再解密后变成:<?xml version=\"1.0\" encoding=\"UTF-8\"?><ChatMember><ID /><Name /><Dept /><Tel /><email>q</email><Password>q</Password><Discription /><AdditionalInfo /><URL /><RejectReason /></ChatMember>覰m滣?緑.H??緑.H??緑.H??緑.H??緑.H??緑.H?\0\0\0\0\0\0\0\0
public byte[] EncryptSend(byte[] buf, byte[] desKey, byte[] desIV)
{ MemoryStream fout=new MemoryStream();
try
{
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
encStream.Write(buf, 0, buf.Length);
encStream.Close();  
fout.Close();

}
catch(System.Security.Cryptography.CryptographicException)
{
fout.Close();
return null;
}
catch(Exception e)
{
MessageBox.Show("Error\n"+e);
return null;
}
return fout.GetBuffer();;
}public byte[] Decrypt(byte[] buf, byte[] desKey, byte[] desIV)
{

    MemoryStream fout = new MemoryStream();
try
{ DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
encStream.Write(buf, 0, buf.Length);
fout.Close();

}
catch(System.Security.Cryptography.CryptographicException e)
{
MessageBox.Show(e.Message+e.StackTrace);

fout.Close();
return null;
}
catch(Exception e)
{
MessageBox.Show("Error\n"+e);
return null;
}            
return fout.GetBuffer();
}请问这个该怎么处理好呢?

解决方案 »

  1.   

    http://www.loveyuki.com/blogview.asp?logID=167
      

  2.   

    多谢孟子大哥!
    可是我还是不能明白,我按您给的代码加入了FlushFinalBlock:
    public byte[] EncryptSend(byte[] buf, byte[] desKey, byte[] desIV)
    { MemoryStream fout=new MemoryStream();
    try
    {
    DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
    encStream.Write(buf, 0, buf.Length);
    encStream.FlushFinalBlock();
    encStream.Close();  
    fout.Close();

    }
    catch(System.Security.Cryptography.CryptographicException)
    {
    fout.Close();
    return null;
    }
    catch(Exception e)
    {
    MessageBox.Show("Error\n"+e);
    return null;
    }
    return fout.GetBuffer();;
    }public byte[] Decrypt(byte[] buf, byte[] desKey, byte[] desIV)
    {
        MemoryStream fout = new MemoryStream();
    try
    {
    DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
    encStream.Write(buf, 0, buf.Length);
    encStream.FlushFinalBlock();
    encStream.Close();
    fout.Close();
    }
    catch(System.Security.Cryptography.CryptographicException e)
    {
    MessageBox.Show(e.Message+e.StackTrace);
    fout.Close();
    return null;
    }
    catch(Exception e)
    {
    MessageBox.Show("Error\n"+e);
    return null;
    }            
    return fout.GetBuffer();
    }但是,在解密的时候,一调用FlushFinalBlock,就触发CryptographicException,给出的信息是“不正确的数据”
    我给加密端输入的数据是这样生成的:
    XmlDocument doc= new XmlDocument();
    ……
    byte[] bb=System.Text.Encoding.Default.GetBytes(doc.OuterXml);