1

AES256 ビットを復号化しようとしていますが、「復号化するデータの長さが無効です」というエラーが表示されます。オンラインPlain_Text = Stream_Read.ReadToEnd();。暗号化方法は機能しますが、復号化は機能しません。誰かが私を助けることができますか?ありがとうございました。

 private static string Decrypt(string stringCypher_Text, string stringKey, string stringIV)
    {
        //Hashes, and converts key to bytes
        //hash
        //convert
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        Byte[] Key = encoding.GetBytes(stringKey);

        //converts string IV to bytes
        Byte[] IV = encoding.GetBytes(stringIV);

        //converts cypher string to bytes
        Byte[] Cypher_Text = encoding.GetBytes(stringCypher_Text);

        RijndaelManaged Crypto = null;
        MemoryStream MemStream = null;
        ICryptoTransform Decryptor = null;
        CryptoStream Crypto_Stream = null;
        StreamReader Stream_Read = null;
        string Plain_Text;

        try
        {
            Crypto = new RijndaelManaged();
            Crypto.Key = Key;
            Crypto.IV = IV;

            MemStream = new MemoryStream(Cypher_Text);

            //Create Decryptor make sure if you are decrypting that this is here and you did not copy paste encryptor.
            Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);

            //This is different from the encryption look at the mode make sure you are reading from the stream.
            Crypto_Stream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);

            //I used the stream reader here because the ReadToEnd method is easy and because it return a string, also easy.
            Stream_Read = new StreamReader(Crypto_Stream);
            Plain_Text = Stream_Read.ReadToEnd();

        }
        finally
        {
            if (Crypto != null)
                Crypto.Clear();

            MemStream.Flush();
            MemStream.Close();

        }
        return Plain_Text;

    }
4

2 に答える 2

0

に変更Plain_Text = Steam_Read.ReadToEnd();してみてください

  byte[] plainText = new byte[Plain_Text.Length];
  int dByte = Stream_Read.Read(plainText, 0, plainText.Length);
  string decryptedText = Encoding.UTF8.GetString(plainText, 0, dByte);
  return descryptedText;
于 2013-05-16T00:44:59.120 に答える