注册 登录
编程论坛 C# 论坛

C# DES 解密异常

ws9187 发布于 2014-08-14 09:30, 417 次点击
//C# DES解密方法         
 public string Decrypt(string pToDecrypt, string sKey)      
 {           
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();           
     byte[] inputByteArray = new byte[pToDecrypt.Length / 2];            
     for (int x = 0; x < pToDecrypt.Length / 2; x++)         
     {            
       int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));            
       inputByteArray[x] = (byte)i;         
     }        
     byte [] inputByteArraykey = new byte[sKey.Length / 2];        
     for (int x = 0; x < sKey.Length / 2; x++)         
    {              
         int i = (Convert.ToInt32(sKey.Substring(x * 2, 2), 16));      
         inputByteArraykey[x] = (byte)i;        
    }   
    des.Key = (byte[])inputByteArraykey;        
    des.IV = (byte[])inputByteArraykey;        
    MemoryStream ms = new MemoryStream();        
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);           
    cs.Write(inputByteArray, 0, inputByteArray.Length);        
    try        
    {           
         cs.FlushFinalBlock();      
    }        
    catch (SystemException ex)        
    {         
           System.Windows.Forms.MessageBox.Show(ex.Message);         
    }   
    StringBuilder ret = new StringBuilder();      
    return System.Text.Encoding.Default.GetString(ms.ToArray());   
}
   

   自己建的一个测试数据,pToDecrypt=“D9932D9DH4021B68EC91C730291EC88C”,Skey="1472583690123456",skey为16位的数据,DES解密的密钥需要8字节的skey,我按2位合为一个字节进行测试,在catch里面就会捕捉异常,“不正确的数据”,这是什么原因呢?

[ 本帖最后由 ws9187 于 2014-8-14 09:32 编辑 ]
2 回复
#2
xydddaxia2014-08-15 09:56
解密密码不对
#3
邓士林2014-08-15 20:47
不了解加密结解密
1