2

AES と RSA を使用してデジタル エンベロープを実装する必要がありますが、RSA アルゴリズムの .NET 実装に問題があります。

ランダム対称キーでデータ (AES) を暗号化できましたが、今度は RSA でキーを暗号化する必要があります。

キーはバイトの配列 ( byte[]) であり、私が持っている公開キーは、モジュラスと公開指数 (どちらもバイトの配列 ( byte[])) のみを教えてくれます。

これら 2 つのパラメーターのみを使用して、AES で生成されたキーを RSA で暗号化するにはどうすればよいですか?

次のコードは、ファイルからメッセージを取得し、AES で暗号化します。その後、公開鍵が公開鍵ファイルから読み取られ、モジュラスと指数が適切なバイト配列に格納されます。symmetricKeyRSA で暗号化を続行するにはどうすればよいですか?

String msgString = Systematic.GetFileContents(messagePath);
Byte[] initVector = new byte[] { 50, 60, 70, 80, 90, 40, 50, 60, 70, 80, 90, 40, 60, 80, 70, 90 };
Byte[] symetricKey = AesCrypt.GenerateRandomKey();
Byte[] encryptedMessage = AesCrypt.Encrypt(msgString, symetricKey, initVector, mode);
Byte[] modulus = null;
Byte[] publicExp = null; 
DataFormatHelper.ReadPublicKey(publicKeyPath, "RSA", ref modulus, ref publicExp);

PS言及した回答への返信でrsa.ImportParameters:私は試してみましたが、 ( )rsa.ImportParameters(keyInfo)をスローします。配列のサイズはどうですか?現在、モジュラスは 128 バイト、指数は 64 バイトです。CryptographicException"Bad Data"

4

1 に答える 1

5

RSACryptoServiceProvider の使用

static public byte[] RSAEncrypt(byte[] data,
    RSAParameters keyInfo, 
    bool doOAEPPadding)
{
    byte[] encryptedData;
    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
    {
        //Import the RSA Key information. This only needs
        //toinclude the public key information.
        rsa.ImportParameters(keyInfo);

        //Encrypt the passed byte array and specify OAEP padding.  
        //OAEP padding is only available on Microsoft Windows XP or later.  
        encryptedData = rsa.Encrypt(data, doOAEPPadding);
    }
    return encryptedData;       
}

したがって、必要なのはRSAParametersですが、設定する必要があるのは、暗号化する Modulus と Exponent だけです。

于 2009-06-04T09:57:57.817 に答える