请问我这段程序为什么每次得到得return出来的值都不样呢,有什么问题呢。其中传人key的值为"02258"
public string  GetICPDESPassword(string key)
{
 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
 ICryptoTransform cc = des.CreateEncryptor();
 byte[] _output =  System.Text.Encoding.Default.GetBytes(key);
 des.Mode = CipherMode.ECB;
 des.Key =getKey(_output); 
 
 return HttpUtility.UrlEncode(cc.TransformFinalBlock(_output,0,_output.Length));
}
private byte[] getKey(byte[] arrBTmp)
{
byte[] arrB = new byte[8];
//将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.Length && i < arrB.Length; i++)
{
arrB[i] = arrBTmp[i];
} //生成密钥
  return arrB;
}

解决方案 »

  1.   

    http://blog.csdn.net/21aspnet/archive/2004/10/26/153184.aspx
    看一下这个
      

  2.   

    每次得到得return出来的值都不样?有意思,值得关注
      

  3.   

    好像ECB模式是不需要初始化向量的
      

  4.   

    如果当前的 Key 为空引用(Visual Basic 中为 Nothing),则将调用 GenerateKey 创建新的随机 Key。如果当前的 IV 为空引用 (Nothing),则将调用 GenerateIV 创建新的随机 IV。
      

  5.   

    ECB模式是不需要初始化向量的
      

  6.   

    主要是看这个方法CreateEncryptor()
    看看微软给的说明:如果当前的 Key 为空引用(Visual Basic 中为 Nothing),则将调用 GenerateKey 创建新的随机 Key。如果当前的 IV 为空引用 (Nothing),则将调用 GenerateIV 创建新的随机 IV。使用具有相同签名的 CreateDecryptor 重载来解密此方法的结果。很明显在调用这个方法的时候,KEY和IV都没有。所以两者都是随机的。改为:
    DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Mode = CipherMode.ECB;
    byte[] _output =  System.Text.Encoding.Default.GetBytes(key);
    des.Key =getKey(_output);ICryptoTransform cc = des.CreateEncryptor();
    就没有问题了!