5

「復号化するデータの長さが無効です」というメッセージが表示されます。メモリ ストリームを復号化しようとすると例外が発生します。私は初心者で、何が悪いのかわかりません。どうしたの?

public bool EncryptStream()
    {

        string password = @"myKey123"; // Your Key Here
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        s_EncryptedStream = new MemoryStream();
        int NoOfBytes;
        byte[] b_Buffer = new byte[8192];

        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        RijndaelManaged RMCrypto = new RijndaelManaged();

        s_CrytpoStream = new CryptoStream(s_EncryptedStream,
            RMCrypto.CreateEncryptor(key, key),
            CryptoStreamMode.Write);

        while (s_MemoryStream.Length < s_MemoryStream.Position)
        {
            NoOfBytes = s_MemoryStream.Read(b_Buffer, 0, 8192);
            s_CrytpoStream.Write(b_Buffer, 0, NoOfBytes);
        }

        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        while (s_EncryptedStream.Position < s_EncryptedStream.Length)
        {
            NoOfBytes = s_EncryptedStream.Read(b_Buffer, 0, 8192);
            s_MemoryStream.Write(b_Buffer, 0, NoOfBytes);

        }
        s_CrytpoStream.Flush();
        s_CrytpoStream.Close();

        return true;

    }


    public bool DecryptStream()
    {


        string password = @"myKey123"; // Your Key Here

        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        int NoOfBytes;
        byte[] b_Buffer = new byte[8192];

        s_DecryptedStream = new MemoryStream();


        RijndaelManaged RMCrypto = new RijndaelManaged();

        s_CrytpoStream = new CryptoStream(s_MemoryStream,
            RMCrypto.CreateDecryptor(key, key),
            CryptoStreamMode.Read);

        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        while (s_MemoryStream.Length > s_MemoryStream.Position)
        {
            NoOfBytes = s_CrytpoStream.Read(b_Buffer, 0, 8192);
            s_DecryptedStream.Write(b_Buffer, 0, NoOfBytes);
        }

        s_DecryptedStream.Seek(0, SeekOrigin.Begin);
        s_MemoryStream.Seek(0, SeekOrigin.Begin);

        while (s_DecryptedStream.Position < s_DecryptedStream.Length)
        {
            NoOfBytes = s_DecryptedStream.Read(b_Buffer, 0, 8192);
            s_MemoryStream.Write(b_Buffer, 0, NoOfBytes);

        }

        s_CrytpoStream.Flush();
        s_CrytpoStream.Close();

        return true;

    }
4

3 に答える 3

7

まず、この while ループ条件は決して正しくありません。

while (s_MemoryStream.Length < s_MemoryStream.Position)

どのように位置が長さを超えることができますか?

ストリームの長さを使用するのではなく、ストリームをコピーする通常のパターンは、返される値が正でなくなるまで繰り返し読み取ることです。とにかく、このコードでそれを 2 回行っているので、それをカプセル化することもできます。

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[8192];
    int read;
    while ( (read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, read);
    }
}

また、usingステートメントを使用して文字列をクリーンアップすることをお勧めします。さらに、このEncoding.Unicodeプロパティは、自分で新しいものを作成する必要がないことを意味しUnicodeEncodingます。また、Positionプロパティを設定する方が を使用するよりも読みやすいことが一般的にわかりますSeek。最後に、値が常に になる場合、値を返すメソッドには意味がありませんtrue。したがって、コードは次のようになります。

public void EncryptStream()
{
    string password = @"myKey123"; // Your Key Here
    byte[] key = Encoding.Unicode.GetBytes(password);

    s_EncryptedStream = new MemoryStream();
    s_MemoryStream.Position = 0;

    RijndaelManaged RMCrypto = new RijndaelManaged();

    using (Stream crytpoStream = new CryptoStream(s_EncryptedStream,
        RMCrypto.CreateEncryptor(key, key),
        CryptoStreamMode.Write))
    {
        CopyStream(s_MemoryStream, cryptoStream);
    }

    s_MemoryStream.Position = 0;
    s_EncryptedStream.Position = 0;
    CopyStream(s_EncryptedStream, s_MemoryStream);
}

public void DecryptStream()
{
    string password = @"myKey123"; // Your Key Here
    byte[] key = Encoding.Unicode.GetBytes(password);

    s_DecryptedStream = new MemoryStream();
    s_MemoryStream.Position = 0;

    RijndaelManaged RMCrypto = new RijndaelManaged();

    using (Stream crytpoStream = new CryptoStream(s_MemoryStream,
        RMCrypto.CreateDecryptor(key, key),
        CryptoStreamMode.Read))
    {
        CopyStream(cryptoStream, s_DecryptedStream);
    }

    s_DecryptedStream.Position = 0;
    s_MemoryStream.Position = 0;

    CopyStream(s_DecryptedStream, s_MemoryStream);
}

このコードを修正した後でも、ここに非ローカル変数が多すぎるように感じます。これがインスタンス変数にある理由がわかりません暗号化または復号化するストリームを (パスワードと共に) パラメータにし、暗号化/復号化されたデータを含むメモリ ストリーム、または単なるバイト配列を返します。

于 2009-05-10T19:57:25.230 に答える
2

入力データの読み取りが完了FlushFinalBlockした後、 でメソッドを呼び出す必要がある場合があります。CryptoStream(つまりcrytpoStream.FlushFinalBlock()、CopyStream の後)

于 2009-05-10T20:30:45.893 に答える
1

私は解決策を考え出し、それを私の新しいブログに投稿しました

constotech.blogspot.com/2009/05/net-encryption-using-symmetricalgorithm.html

于 2009-05-29T14:37:06.243 に答える