3

秘密鍵を知っている場合、特定の AES 256 ビットで暗号化された文字列をどのように復号化するか、誰かに尋ねられました。私は暗号化にあまり詳しくないので、座って問題を調べてみました。

MSDN でこの例を見つけて、復号化のみを実行するように変更しようとしました。

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

internal class AesExample
{
    public static void Main()
    {
        var encryptedString = "U2FsdGVkX1/cHT8XuHCfpw0AV4jpaO8JfLqUeCRJqjY=";
        var secret = "SPARKY";

        // I know this is not the correct way to get my input byte arrays...
        // Just illustrating that I DO need byte arrays.
        var encryptedBytes = Encoding.UTF8.GetBytes(encryptedString);
        var secretBytes = Encoding.UTF8.GetBytes(secret);

        try
        {
            using (var aes = new AesManaged())
            {
                aes.Key = secretBytes;

                // Decrypt the bytes to a string. 
                var decryptedString = Decrypt(encryptedBytes, aes.Key, aes.IV);

                //Display the original data and the decrypted data.
                Console.WriteLine("Encrypted: {0}", encryptedString);
                Console.WriteLine("Decrypted: {0}", decryptedString);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e.Message);
        }
    }

    private static string Decrypt(byte[] cipherText, byte[] key, byte[] iv)
    {
        // Declare the string used to hold 
        // the decrypted text. 
        string plaintext;

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

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

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

        return plaintext;
    }
}

もちろん、次の行にヒットするとすぐに、CryptographicExcetion がスローされ、「指定されたキーは、このアルゴリズムに対して有効なサイズではありません」というメッセージが表示されます。==> aes.Key = secretBytes

誰かが、シークレットの SHA1 ハッシュを取得し、それを 20 byes にトリミングすることを提案しました。それを試してみたところ、「復号化するデータの長さが無効です」というメッセージとともに新しい CryptographicException が発生し始めました。

それで、いくつか質問があります:

1) 暗号化されたテキストと秘密鍵だけがあれば、これは可能ですか?

2) もしそうなら、それらは CipherMode のように、作る必要があるいくつかの基本的な仮定ですか? ECB モードには初期化ベクトルがないことを読んでいました。それが私が尋ねる理由です。

3) 入力 (暗号化されたテキストと秘密鍵) を正しい Byte[] 形式に変換して復号化するには、どうすればよいですか?

ありがとう!

4

2 に答える 2

2

AES キーの長さは、使用する暗号に応じて 128、192、および 256 ビットです。文字列が適切なバイト長であることを確認する必要があります。

于 2013-08-14T20:25:39.957 に答える