簡體   English   中英

Windows Phone數字簽名XML

[英]Windows Phone Digital Sign XML

是否可以在Windows Phone 8 Silverlight項目中簽署xml? 我在Google上搜索了很多,卻一無所獲。 移動的SignedXML對象不存在。 對於與我合作的銀行,這是強制性的。

我已經解決了。 實際上,使用第三方工具( BouncyCastle )是可能的。 這是一個完整的代碼和文檔在這里

沒有文檔的完整源代碼如下:

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.");
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM