Windows Phone API:使用RSACryptoServiceProvider对数据进行签名

时间:2013-06-19 19:25:24

标签: c# .net windows-phone-8 cryptography digital-signature

我的问题是:如何使用.Net for Windows Phone签名数据,托管语言是C#

我使用RSACryptoServiceProvider生成私钥/公钥对,然后我想使用“SHA256Managed”哈希算法用私钥签名数据,我做的是:

string DataTobeEncrypt = "upupdowndownleftleftrightrightABstart";
CspParameter cspParams = new CspParameters();
cspParams = new CspParameters();
cspParams.ProviderType = 1; // PROV_RSA_FULL
cspParams.Flags = CspProviderFlags.UseArchivableKey;
cspParams.KeyNumber = (int)KeyNumber.Exchange;
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(2048,cspParams);
byte[] plainBytes = Encoding.Unicode.GetBytes(DataTobeEncrypt);
byte[] signedBytes = rsaProvider.SignHash(plainBytes, new SHA256Managed());

执行,我得到了异常: mscorlib.ni.dll中出现“System.Security.Cryptography.CryptographicException”类型的异常,并且在托管/本机边界之前未处理 指定的算法无效。

然后我将算法切换到SHA1或MD5,仍然得到相同的错误,然后我尝试了:

SHA256Managed hashAlgorithm = new SHA256Managed();
byte[] hashedBytes = hashAlgorithm.ComputeHash(plainBytes);
signedBytes = rsaProvider.SighHash(hashedBytes, "1.2.840.113548.1.1.11");

然后我得到了例外: mscorlib.ni.dll中发生了'System.NullReferenceException'类型的第一次机会异常 对象引用未设置为对象的实例。

然后我放弃使用rsaProvider,转向AsymmetricSignatureFormatter,我做的是:

AsymmetricSignatureFormatter formatter = new RSAPKCS1SignatureFormatter();
formatter.SetHashAlgorithm("SHA256");
formatter.SetKey(rsaProvider);
signedBytes = formatter(plainBytes);

但它仍然失败,我得到的是:类型的第一次机会异常 mscorlib.ni.dll中出现“System.Security.Cryptography.CryptographicException”类型的异常,并且在托管/本机边界之前未处理 指定的算法无效。

我搜索了许多主题并没有找到任何针对Windows手机平台的具体示例,有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

他们的网页声称他们已在Windows Phone 7中成功使用它。它甚至与RSACryptoServiceProvider兼容。 http://www.dustinhorne.com/post/Asymmetric-Encryption-and-Signing-with-RSA-in-Silverlight.aspx

问题似乎是,Findings Symbol(.pdb),Source和Binary Files。我希望这会有所帮助 启用编译器设置 http://msdn.microsoft.com/en-us/library/ms241613.aspx#BKMK_Specifying_symbol_locations_and_loading_behavior