Длина данных для дешифрования недопустима

Легко

if (lblExample.ForeColor != System.Drawing.Color.Red)
{
    lblExample.ForeColor = System.Drawing.Color.Red;
}
else
{
    lblExample.ForeColor = new System.Drawing.Color();
}
12
задан Tipx 23 April 2014 в 21:48
поделиться

3 ответа

It looks to me like you're not properly sending the final block. You need to at least FlushFinalBlock() the sending CryptoStream in order to ensure that the final block (which the receiving stream is looking for) is sent.

By the way, CipherMode.ECB is more than likely an epic fail in terms of security for what you're doing. At least use CipherMode.CBC (cipher-block chaining) which actually uses the IV and makes each block dependent on the previous one.

EDIT: Whoops, the enciphering stream is in read mode. In that case you need to make sure you read to EOF so that the CryptoStream can deal with the final block, rather than stopping after readBytes. It's probably easier to control if you run the enciphering stream in write mode.

One more note: You cannot assume that bytes in equals bytes out. Block ciphers have a fixed block size they process, and unless you are using a cipher mode that converts the block cipher to a stream cipher, there will be padding that makes the ciphertext longer than the plaintext.

6
ответ дан 2 December 2019 в 23:08
поделиться

После комментария Джеффри Хантина я изменил некоторые строки в receiveFile на

using (stream) {
    FileInfo finfo = new FileInfo(transferFile.Path);
    long position = finfo.Length;
    while (position < transferFile.Length) {
        int maxRead = Math.Min(array.Length, (int)(transferFile.Length - position));
        int read = position < array.Length
                   ? streamSocket.Receive(array, maxRead, SocketFlags.None)
                   : streamSocket.Receive(array, SocketFlags.None);
        stream.Write(array, 0, read);
        position += read;
    }
}

->

using (stream) {
    int read = array.Length;
    while ((read = streamSocket.Receive(array, read, SocketFlags.None)) > 0) {
        stream.Write(array, 0, read);
        if ((read = streamSocket.Available) == 0) {
            break;
        }
    }
}

И вуаля, она работает (из-за очень доброй набивки, о которой я не заботился раньше). Я не уверен, что произойдет, если Available вернет 0, хотя все данные не были переданы, но я постараюсь сделать это позже в этом случае. Спасибо за помощь, Джеффри!

С уважением.

1
ответ дан 2 December 2019 в 23:08
поделиться
cipher.Mode = CipherMode.ECB;

Ага! Обновление собственного кода безопасности - почти всегда плохая идея.

0
ответ дан 2 December 2019 в 23:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: