我必须使用 AES 和 RSA 实现数字信封,但我在 RSA 算法的 .NET 实现方面遇到了问题。
我已经设法用随机对称密钥加密数据(AES),但现在我必须用 RSA 加密密钥。
密钥是一个字节数组(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);
PS在回答提到的答案时rsa.ImportParameters
:我已经尝试过,rsa.ImportParameters(keyInfo)
但它会抛出一个CryptographicException
("Bad Data"
)。数组大小呢?目前,模数为 128 字节,指数为 64 字节。