1

わかりました、これは奇妙です、私にとって。私はこのコードを持っています。

using (MemoryStream memStream = new MemoryStream(inBytes))
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
using (CryptoStream cs = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
    byte[] buffer;
    if (inBytes.Length < (1024 * 10)) buffer = new byte[inBytes.Length];
    else buffer = new byte[(1024 * 10)];
    long readBytes = 0;
    long totalBytes = inStream.Length;
    int currBytes;

    while (readBytes < totalBytes)
    {
        currBytes = cs.Read(buffer, 0, buffer.Length);
        fs.Write(buffer, 0, currBytes);
        readBytes += currBytes;
    }
}

これにより、復号化されたデータがファイルに書き込まれます。

次に、 a に書き込む(および返す)ことを除いて、まったく同じことを行う次のコードがありますMemoryStream

using(MemoryStream memStream = new MemoryStream(inBytes))
using(MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
    byte[] buffer;
    if (inBytes.Length < (1024 * 10)) buffer = new byte[inBytes.Length];
    else buffer = new byte[(1024 * 10)];
    long readBytes = 0;
    long totalBytes = inStream.Length;
    int currBytes;

    while (readBytes < totalBytes)
    {
        currBytes = cs.Read(buffer, 0, buffer.Length);
        ms.Write(buffer, 0, currBytes);
        readBytes += currBytes;
    }

    return ms;
}

currBytes = cs.Read(buffer, 0, buffer.Length)で「復号化するデータの長さが無効です」というエラーが表示されますが、最初ではなく2番目のセットでのみ発生します。「ICryptoTransformデクリプター」は一般的な方法で作成されており、同じキーを使用していることはわかっています。

最初のインスタンスではこのエラーが発生しないのに、2 番目のインスタンスではエラーが発生する理由と、(さらに重要なことに) 修正方法を教えてください。

そして、はい、DES がこれまでで最高の暗号化方式ではないことはわかっています。これは、実稼働環境で日の目を見ることのない概念実証の性質のものです。

4

2 に答える 2

1

今日、このエラーに遭遇しました。1 つの関数で ASCII を使用して 1 つのソース文字列をバイト配列に変換し、別の関数で Base64 を使用して別のソース文字列を変換したことが判明しました。

入力は正しい長さですが、同じエンコーディングを使用していない可能性があります。

于 2012-09-27T08:27:35.470 に答える
0

これらのチェックを両方のコードに追加してみてください。これらのいずれかまたは両方が失敗すると強く思います。

if ( inStream.Length != inBytes.Length )
  throw new Exception("inBytes read incorrectly");
if ( inBytes.Length % 8 == 0 )
  throw new Exception("inBytes is not a valid DES encryption");
于 2012-02-16T18:41:58.333 に答える