简体   繁体   English

Windows Phone数字签名XML

[英]Windows Phone Digital Sign XML

Is it possible to sign xml in windows phone 8 silverlight project? 是否可以在Windows Phone 8 Silverlight项目中签署xml? I googled a lot and found nothing. 我在Google上搜索了很多,却一无所获。 SignedXML object doesnt exist for mobile. 移动的SignedXML对象不存在。 It is a mandatory for the bank that I have corporated with. 对于与我合作的银行,这是强制性的。

I have solved it. 我已经解决了。 Actually it is possible with a 3rd party tool ( BouncyCastle ). 实际上,使用第三方工具( BouncyCastle )是可能的。 Here is a full code and documentation here . 这是一个完整的代码和文档在这里

Full source code without documentation is below: 没有文档的完整源代码如下:

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