使用模数和公共指数的C#RSA加密

Kor*_*tak 1 c# encryption rsa

我必须使用AES和RSA实现数字信封,但我遇到了RSA算法的.NET实现问题.

我已设法使用随机对称密钥加密数据(AES),但现在我必须使用RSA加密密钥.

键是一个bytes(byte[])数组,我只告诉我模数和公共指数,两个字节数组(byte[]).

仅使用这两个参数,如何使用RSA加密AES生成的密钥?

以下代码从文件中检索消息并使用AES对其进行加密.然后,从公钥文件中读取公钥,并且模数和指数在其适当的字节数组中.我如何继续symmetricKey使用RSA加密?

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);
Run Code Online (Sandbox Code Playgroud)

PS回答提到的答案rsa.ImportParameters:我已经尝试了rsa.ImportParameters(keyInfo)但它抛出了一个CryptographicException("Bad Data").数组大小怎么样?目前,模数为128字节,指数为64字节.

Shu*_*oUk 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;       
}
Run Code Online (Sandbox Code Playgroud)

所以你需要的是RSAParameters,但你需要设置的只是要加密的模数和指数.