문제

I've been researching an issue I'm having while attempting to decrypt using the Rijndael C# libraries. I've tried several solutions that have been posted on here but none seem to work or apply.

The issue: I'm attempting to decrypt a HTTP Request that is sent from a piece of hardware. However, I'm not getting the HTTP request converted into the correct number of bytes that match my decryption methods( I Think this is the issue?).

Here is my code:

System.Text.Encoding enc = System.Text.Encoding.ASCII;
System.Text.Encoding req = System.Text.Encoding.ASCII;

if (curContext != null)
{
    string decrypted = "";
    int totalBytes = curContext.Request.TotalBytes;
    StreamReader sr = new StreamReader(curContext.Request.InputStream);
    string request = sr.ReadToEnd();

    if (!String.IsNullOrEmpty(request)) 
    {
        using (RijndaelManaged myRijndael = new RijndaelManaged())
        {
            myRijndael.Mode = CipherMode.ECB;
            myRijndael.Padding = PaddingMode.None;
            byte[] key = enc.GetBytes(WebConfigurationManager.AppSettings["32B"].ToString());
            myRijndael.KeySize = 256;
            myRijndael.Key = key;

            decrypted = DecryptStringFromBytes(req.GetBytes(request), myRijndael.Key);
        }
    }
}

And Decrypt method:

static string DecryptStringFromBytes(byte[] cipherText, byte[] Key)
{
    using (RijndaelManaged rijAlg = new RijndaelManaged())
    {
        rijAlg.Key = Key;
        rijAlg.Mode = CipherMode.ECB;
        rijAlg.Padding = PaddingMode.None;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform decryptor = rijAlg.CreateDecryptor();

        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {
                    // Read the decrypted bytes from the decrypting stream
                    // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }
    }
}

On the srDecrypt.ReadToEnd() I get the error message stated in title.

I'm rather new to this so I'm not sure where I'm going wrong. Any advice would be appreciated. Thanks~!

도움이 되었습니까?

해결책

"Stream to string to bytes" conversion sequence feels very wrong. Make sure you really need to do it instead of simply reading bytes from response.

다른 팁

Try this instead at the bottom of your Decrypt method:

int plainByteCount = int.MinValue;

// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
  using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  {
    plainBytes = new byte[cipherText.Length];
    plainByteCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);
  }
}

string plainText = Encoding.UTF8.GetString(plainBytes, 0, plainByteCount);

return plainText;

I think I might have found your problem. According to the constructor for StreamReader, the default encoding is UTF8Encoding. Trying using the other constructor overload and pass in the ASCII encoding:

StreamReader sr = new StreamReader(
   curContext.Request.InputStream, Encoding.ASCII);
string request = sr.ReadToEnd();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top