开发者

Getting "Bad Data" exception on DES decrypt in .NET

In C#.NET, I want to decrypt some data that has been encrypted by DES algorithm before, and just have a key but no IV! I get exception when reading from stream. what should i write? here 开发者_开发知识库is my code:

byte[] byaText = new byte[] { 0x91, 0x6e , 0x6e , 0x75, 0x76 , 0xa5 , 0x73 , 0x55 };
byte[] byaKey = new byte[] { 0x9B, 0x43, 0xBF, 0x66, 0x98, 0xDE, 0x67, 0xFB };
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
cryptoProvider.Mode = CipherMode.ECB; // even when remarking this line exception occures
MemoryStream memoryStream = new MemoryStream(byaRes);
ICryptoTransform ict = cryptoProvider.CreateDecryptor(byaKey, byaKey); // even "new byte[] {0,0,0,0,0,0,0,0}" as IV, exception occures
CryptoStream cryptoStream = new CryptoStream(memoryStream,
    ict, CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
string decrypted = reader.ReadToEnd(); // here the exception 'Bad Data' occures


You may have a padding exception. Check what padding the sender is using, possibly PKCS5, and ensure that your decrypt function is expecting the same padding.

ECB mode is not secure. If at all possible change to CBC mode of CTR mode. There is a good illustration (literally) of why ECB mode is insecure on Wikipedia: Electronic codebook (ECB)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜