C#从.cer(2048位)证书中提取rsa公钥并使用RSA / ECB / PKCS1Padding方法加密数据

时间:2015-07-24 05:16:59

标签: c# encryption certificate rsa public-key

`我已通过IIS管理器导入.cer证书 - >服务器证书 - > import我想从.cer证书(link)中提取公钥并使用从证书中提取的公钥(RSA 2048位)加密字符串,使用的方法应该是带有ECB的RSA和PKCS1Padding

编辑:添加以下代码

public static void SettingRSAParameters(){

 X509Certificate cert = X509Certificate.CreateFromCertFile("D:\\cer.cer");
byte[] publicKey = cert.GetPublicKey();
int keyLength = publicKey.Length;
byte[] ExponentData = new byte[3];
byte[] ModulusData = new byte[256];

Array.Copy(publicKey, publicKey.Length - ExponentData.Length, ExponentData, 0, ExponentData.Length);
Array.Copy(publicKey, publicKey.Length - ExponentData.Length - 2 - ModulusData.Length, ModulusData, 0, ModulusData.Length);
ExponentData1 = ExponentData;
ModulusData1 = ModulusData;}

以这种方式提取模数和指数数据并通过以下代码插入

static public byte[] Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
{
    SettingRSAParameters();
    RSAKey.Modulus = ModulusData1;
    RSAKey.Exponent = ExponentData1;

    try
    {
        byte[] encryptedData;
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048))
        {

            RSA.ImportParameters(RSAKey);
            encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
            return encryptedData;
        }
    }
    catch (CryptographicException e)
    {
        Console.WriteLine(e.Message);
        return null;
    }
}

代码没有正确加密,请检查代码是否正确,并建议添加密码模式ECB的位置

0 个答案:

没有答案