【发布时间】:2013-06-19 19:25:24
【问题描述】:
我的问题是:如何使用 .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 phone 平台的具体示例,有人可以帮助我吗?
【问题讨论】:
标签: c# .net windows-phone-8 cryptography digital-signature