2011-09-24 53 views
1

我需要从证书中查找公钥作为xml字符串。我可以把公钥只作为一个字符串与这种方法:从证书中查找公钥作为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();` 

我可以采取公钥。然后,在另一种方法我把公钥的值到另一个字符串变量键:

`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,我不明白为什么,什么是一个问题的数据?

回答

2

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

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