从证书中查找公钥作为xml字符串

时间:2011-09-24 21:51:12

标签: windows-phone-7 encryption

我需要从证书中找到公钥作为xml字符串。我只能将公钥作为字符串使用此方法:

public string CelesiPublik()
    {
        X509Certificate cer;
        cer = X509Certificate.CreateFromCertFile("C://certificate//EdonBajrami.crt");

        string Celesi = cer.GetPublicKeyString();
        return Celesi;
    }

然后我把这个值带到另一个方法。 Celesi的价值现在已经采取了celesiPublik

  celesiPublik = e.Result;
    string NrTelefonit = "044-419-109";
    string salt = "VotimiElektronikKosove";
    AesManaged aes = new AesManaged();
   Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(celesiPublik,Encoding.UTF8.GetBytes(salt));
            aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(celesiPublik);
            rsa.Encrypt(aes.Key, false);

但它显示我的错误。我该如何解决这个问题?


GregS我无法在Windows Phone 7 X509Certificate2中使用。我使用方法获取我的公钥:

`X509Certificate cer = new X509Certificate("C://certificate//EdonBajrami.crt");
        string publicKey = cer.GetPublicKeyString();`

我可以拿公钥。然后在另一种方法中,我将publicKey的值取为另一个字符串变量Key:

`string Key = publicKey; 
  //-----First------
 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            //----------
            RSAParameters RSAKeyInfo = new RSAParameters();
            byte[] celesibyte = System.Text.Encoding.UTF8.GetBytes(celesiPublik);
            byte[] Exponent = { 1, 0, 1 };

            RSAKeyInfo.Modulus = celesibyte;
            RSAKeyInfo.Exponent = Exponent;
            rsa.ImportParameters(RSAKeyInfo);
            //-----------
            /*  rsa.FromXmlString(celesi2Publik); */
            string edon = "Edon";
            byte[] edon1 = Encoding.UTF8.GetBytes(edon);
            byte[] edon2 = rsa.Encrypt(edon1, false);'

但是它向我发送了506字节的数据,我不明白为什么,有什么问题?

1 个答案:

答案 0 :(得分:2)

据我所知,X509Certificate班几乎没用。也许这就是为什么有一个班级X509Certificate2?使用X509Certificate2类。您可以直接从公钥创建RSACryptoServiceProvider,如下所示:

X509Certificate2 cert = new X509Certificate2(X509Certificate.CreateFromCertFile("C://certificate//EdonBajrami.crt"));  
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider) cert.PublicKey.Key;