2015-06-18 50 views
1

是否可以在windows phone 8 silverlight項目中籤名xml?我搜索了很多,什麼都沒找到。 SignedXML對象不存在移動設備。這是我與之合併的銀行的強制性規定。Windows Phone數字簽名XML

+0

你讀這https://msdn.microsoft.com/en-us/library/ms229745(v=vs.110).aspx –

+0

沒錯,但是windows phone沒有'System.Security.Cryptography.Xml'命名空間。問題是這樣的。 – hsynlms

+0

對不起。我沒有看到你最後的標籤:/ –

回答

0

我已經解決了。其實這可能與第三方工具(BouncyCastle)。這是一個完整的代碼和文檔here。沒有證件

完整的源代碼如下:

using System; 
using System.IO; 
using System.Security.Cryptography; 
using System.Text; 
using Org.BouncyCastle.Crypto.Parameters; 
using Org.BouncyCastle.OpenSsl; 

namespace hsynlms.Classes 
{ 
    public class Cryptography 
    { 
     public string GetSHA1withRSAKey(string pemTxtFilePath, string sData) 
     { 
      try 
      { 
       if (!File.Exists(pemTxtFilePath)) 
       { 
        throw new Exception("PEM (txt) file not found."); 
       } 

       using (var reader = File.OpenText(pemTxtFilePath)) 
       { 
        var pemReader = new PemReader(reader); 
        var bouncyRsaParameters = (RsaPrivateCrtKeyParameters)pemReader.ReadObject(); 

        var rsaParameters = new RSAParameters(); 
        rsaParameters.Modulus = bouncyRsaParameters.Modulus.ToByteArrayUnsigned(); 
        rsaParameters.P = bouncyRsaParameters.P.ToByteArrayUnsigned(); 
        rsaParameters.Q = bouncyRsaParameters.Q.ToByteArrayUnsigned(); 
        rsaParameters.DP = bouncyRsaParameters.DP.ToByteArrayUnsigned(); 
        rsaParameters.DQ = bouncyRsaParameters.DQ.ToByteArrayUnsigned(); 
        rsaParameters.InverseQ = bouncyRsaParameters.QInv.ToByteArrayUnsigned(); 
        rsaParameters.D = bouncyRsaParameters.Exponent.ToByteArrayUnsigned(); 
        rsaParameters.Exponent = bouncyRsaParameters.PublicExponent.ToByteArrayUnsigned(); 

        var privateKey = new RSACryptoServiceProvider(); 
        privateKey.ImportParameters(rsaParameters); 

        var sha = new SHA1Managed(); 

        UTF8Encoding str = new UTF8Encoding(true); 
        byte[] signedData = privateKey.SignData(str.GetBytes(sData), sha); 
        var result = Convert.ToBase64String(signedData); 

        return result; 
       } 
      } 
      catch (Exception) 
      { 
       throw new Exception("Signing SHA1 with RSA failed."); 
      } 
     } 
    } 
}