2017-05-02 13 views
0

このWebサイトを使用してAESアルゴリズムを実装しました。 https://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged(v=vs.110).aspx復号化データの表示にメッセージボックスが表示されない

暗号化後、値は文字列に変換され、データベースに送信されます。それは成功しました。私が暗号化された値の値を取得していたとき、それは成功しました。しかし、私が復号化を開始したときに、エラーは検出されず、出力も表示されませんでした(メッセージボックスを使用した出力の取得)。私は何をすべきか?

byte[] cipherbytes = System.Text.ASCIIEncoding.Default.GetBytes(encypted); 
     //AES Decryption start 
     try 
     { 
      using (AesManaged myAes = new AesManaged()) 
      { 
       // Decrypt the bytes to a string. 
       string roundtrip = DecryptStringFromBytes_Aes(cipherbytes, myAes.Key, myAes.IV); 
       //Console.WriteLine("Round Trip: {0}", roundtrip); 
       MessageBox.Show(roundtrip, "Decrypted text"); //this meessage box is not showing 
      } 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Error: {0}", e.Message); 
      //MessageBox.Show("Inside is working"); 
     } 

//ここでは

static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) 
    { 
     // Check arguments. 
     if (cipherText == null || cipherText.Length <= 0) 
      throw new ArgumentNullException("cipherText"); 
     if (Key == null || Key.Length <= 0) 
      throw new ArgumentNullException("Key"); 
     if (IV == null || IV.Length <= 0) 
      throw new ArgumentNullException("IV"); 

     // Declare the string used to hold 
     // the decrypted text. 
     string plaintext = null; 

     // Create an AesManaged object 
     // with the specified key and IV. 
     using (AesManaged aesAlg = new AesManaged()) 
     { 
      aesAlg.Key = Key; 
      aesAlg.IV = IV; 

      // Create a decrytor to perform the stream transform. 
      ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); 

      // 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(); 
        } 
       } 
      } 

     } 

答えて

0

あなたDecryptStringFromBytes_Aes値を返しません、あなたが最後にreturn plaintext;を追加する必要がありますだけでなく復号化アルゴリズムです。それはコンパイルされますか?

MessageBoxは表示されません...その行には届かず、コードの前半(DecryptStringFromBytes_Aes関数内)に例外があります。この行にブレークポイントを入れてConsole.WriteLine("Error: {0}", e.Message);とし、エラーを確認してください。あなたはまた書かれた誤りのためにコンソールをcheclすることができました。

+0

return plaintext;残念ながら行方不明でした。私は上記の方法を使って問題を見つけようとします。あなたの提案に応じてブレークポイントを設定します。しかし、何も起こらなかった。 catch(Exception e)のメッセージボックスを有効にすると、メッセージボックスのポップアップが表示されます。例外が発生していることを示しています。コンソールエラーを使用して問題を見つけることができませんでした。 –

関連する問題