2013-04-24 147 views
6

我需要在WP8上使下面的代碼工作,問題是WP8上沒有X509Certificate2類,我嘗試過使用彈性城堡API,但我沒有真正設法計算它出。X509Certificate2到Windows Phone上的X509證書8

有沒有辦法讓這個代碼在WP8上工作?

private string InitAuth(X509Certificate2 certificate, string systemId, string username, string password) 
    { 
     byte[] plainBytes = Encoding.UTF8.GetBytes(password); 
     var cipherB64 = string.Empty; 
     using (var rsa = (RSACryptoServiceProvider)certificate.PublicKey.Key) 
      cipherB64 = systemId + "^" + username + "^" + Convert.ToBase64String(rsa.Encrypt(plainBytes, true)); 

     return cipherB64; 
    } 

回答

1

難道你不能只是工作在X509Certificate2

private string InitAuth(X509Certificate certificate, string systemId, string username, string password) 
    { 
     byte[] plainBytes = Encoding.UTF8.GetBytes(password); 
     var cipherB64 = string.Empty; 

     //Create a new instance of RSACryptoServiceProvider. 
     RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 

     //Create a new instance of RSAParameters. 
     RSAParameters RSAKeyInfo = new RSAParameters(); 

     //Set RSAKeyInfo to the public key values. 
     RSAKeyInfo.Modulus = certificate.getPublicKey(); 
     RSAKeyInfo.Exponent = new byte[3] {1,0,1};; 

     //Import key parameters into RSA. 
     RSA.ImportParameters(RSAKeyInfo); 

     using (RSA) 
      cipherB64 = systemId + "^" + username + "^" + Convert.ToBase64String(RSA.Encrypt(plainBytes, true)); 

     return cipherB64; 
    } 

披露:我沒有嘗試上面的代碼,因爲我目前沒有C#運行時環境供我使用。

+0

我會試試,謝謝 – jjdev80 2013-05-02 19:28:01

+0

@MaryJ。你有看過嗎? – likeitlikeit 2013-05-05 20:11:36

+0

不,對不起,我還沒有時間做到這一點呢 – jjdev80 2013-05-08 08:11:52