My question is : how to sign data using .Net for Windows Phone, the managed language is C#
I use RSACryptoServiceProvider to generate a private/public keypair, and then I want to use "SHA256Managed" Hash algorithm to sign a data with the private key, what I did is:
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());
execute and I got exception: An exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary Invalid algorithm specified.
then I switch the algorithm to SHA1 or MD5, still get the same error, then I tried:
SHA256Managed hashAlgorithm = new SHA256Managed();
byte[] hashedBytes = hashAlgorithm.ComputeHash(plainBytes);
signedBytes = rsaProvider.SighHash(hashedBytes, "1.2.840.113548.1.1.11");
then I get the exception: A first chance exception of type 'System.NullReferenceException' occurred in mscorlib.ni.dll Object reference not set to an instance of an object.
then I giveup using rsaProvider, and turn to AsymmetricSignatureFormatter, what I did is :
AsymmetricSignatureFormatter formatter = new RSAPKCS1SignatureFormatter();
formatter.SetHashAlgorithm("SHA256");
formatter.SetKey(rsaProvider);
signedBytes = formatter(plainBytes);
but it still failed, what I get is :A first chance exception of type An exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary Invalid algorithm specified.
I searched lots of threads and didn't find any specific example for the windows phone platform, can anyone help me with that?