【发布时间】:2014-05-14 11:23:19
【问题描述】:
我有 RSA 模数和指数,我想用这个组件生成一个公钥。然后我想用这个公钥加密一个数据。
所以我写了这个函数:
public static byte[] EncryptRSA(byte[] rsaModulus, byte[] exponent, byte[] data)
{
byte[] response = null;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters rsaPar = rsa.ExportParameters(false);
rsaPar.Modulus = rsaModulus;
rsaPar.Exponent = exponent;
rsa.ImportParameters(rsaPar);
response = rsa.Encrypt(data, false);
return response;
}
但是 rsa.ExportParameters 方法需要很长时间。
public RSACryptoServiceProvider ()
: this (1024)
{
// Here it's not clear if we need to generate a keypair
// (note: MS implementation generates a keypair in this case).
// However we:
// (a) often use this constructor to import an existing keypair.
// (b) take a LOT of time to generate the RSA keypair
// So we'll generate the keypair only when (and if) it's being
// used (or exported). This should save us a lot of time (at
// least in the unit tests).
}
如您所见,ExportParameters() 方法正在执行 RSA 密钥对生成,这是一个耗时的操作。
之后我在导入 RSA 参数时收到异常“私钥/公钥不匹配”。
【问题讨论】:
-
提示:
private const bool PKCS1_1_5_PADDING = false;使代码在调用Encrypt时至少可读。
标签: c# mono cryptography rsa monodevelop