0

I need to use RSA encryption in my wp8 app and send it to the server. But the trouble I am facing is that I know the public key of the server and I need to encrypt the data in app side using the key. But as far I infer from all the posting here, RSACryptoservice provider class doesn't support a key from another source(Am I wrong?). Is there any way to use the class in such a scenario? Or can this be done by using third party library only?

I tried the following function but still no use. What am I doing wrong here?

public static string RSAEncrypt(string data)
{
    try
    {       
        //initialze the byte arrays to the public key information. 
        string  pk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0cNKUgLdLMpW5BWB+PAlIIIiqhSXk66PQVemUnRs3nowRcBETfUkMIfDcPDM1FXhh+/2FqsnFLveCYl980bylZlBghkjUleknV4dGLfQPuLE7oxk4tbQF6Zk9Fmc9ynxvZ7XDuLmdn/4mdxW7BmcSomLIxkkGHynKkkXk5QcKQIDAQAB";

        byte[] PublicKey = Convert.FromBase64String(pk);
        //byte[] Exponent = { 1, 0, 1 }; 

        UnicodeEncoding pi = new UnicodeEncoding();

        //Values to store encrypted symmetric keys. 
        byte[] dataBytes = pi.GetBytes(data);

        //Create a new instance of RSACryptoServiceProvider.
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

        //Create a new instance of RSAParameters.
        RSAParameters RSAKeyInfow = new RSAParameters();

        //Set RSAKeyInfo to the public key values. 
        RSAKeyInfow.Modulus = PublicKey;
        // RSAKeyInfow.Exponent = Exponent;

        //Import key parameters into RSA.
        RSA.ImportParameters(RSAKeyInfow);

        var rslt = RSA.Encrypt(dataBytes, false);
        System.Diagnostics.Debug.WriteLine(rslt);

        return Convert.ToBase64String(rslt);
    }
    catch
    {
        return null;
    }
}

Am calling this function in another page as:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var myString = "this is my string data";

    var x = Class1.RSAEncrypt(myString);
    MessageBox.Show(x);
}

The error I get is "Value cannot be null. Parameter name: messageBoxText"

I think the problem here is not passing the exponent, but I don't know how.

chue x
  • 18,573
  • 7
  • 56
  • 70
Akhil S A
  • 93
  • 7
  • 1
    Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Rick S Aug 21 '14 at 19:51
  • You should try google. – Adam Aug 21 '14 at 19:53
  • Reword the problem to remove the recommendation request. Its an OK question without it (any code you have would also be nice). – BradleyDotNET Aug 21 '14 at 19:54
  • some libraries include openssl, gnupg, gnutls, and others – programmerjake Aug 21 '14 at 19:59
  • Better. The formatting could still be improved and it would be nice to see the code you *think* should work, but doesn't. Thank you for updating it though. – BradleyDotNET Aug 21 '14 at 20:02
  • Thanks for the replies and I am sorry that I asked an Off-Topic question. I have come across several suggestions made by members here in some other posts(not related to my question). So thought it is ok to ask. Sorry again. Thanks Jake :) – Akhil S A Aug 21 '14 at 20:06
  • 1
    The biggest mistake you made was asking for a library, which is explicitly off-topic here. I'm not going to say it was a model question otherwise, but I've seen far worse... Hopefully it will get reopened. Continuing to edit and improve the question will help your changes at that. – BradleyDotNET Aug 21 '14 at 20:24
  • Thanks Bradley for your kind comments. Will always keep this in mind while asking question. Thanks again for taking time to share your thoughts.. – Akhil S A Aug 21 '14 at 21:08
  • You are almost there! (4 reopen votes, just one more needed). To let you know, you can notify me in a comment by using the @ symbol (for example, put @BradleyDotNET in your comment). This will put a notification in my inbox so that I know that I have something to read! You can do this to one person per comment, the original poster of the question/answer (in this case you) will always be notified (unless you posted *and* you are the auto-notify of course) – BradleyDotNET Aug 22 '14 at 00:38
  • I do not really know anything about what you are trying to do, or how the RSA class works. However, in looking at the example in the [Encrypt method documentation](http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.encrypt(v=vs.110).aspx), they use a public key there. Does that do what you want? If so, please post your solution as an answer. – chue x Aug 22 '14 at 18:43
  • @chuex Thanks for the example. It works but unfortunately that is not the scenario what I Am looking for. All I have is a public key string. like mQGiBEAMxJ8RBACL..... I am looking for a way to encrypt data using this key. – Akhil S A Aug 22 '14 at 22:54
  • That seems to be a different question than the one you have posted above. Just speculating, but perhaps it is as simple as converting the string to a byte array: http://stackoverflow.com/a/12178532/1822514 – chue x Aug 22 '14 at 23:28
  • @chuex thanks again. but it didn't work. I've edited the question with the changes I made in the code. thanks for your help – Akhil S A Aug 25 '14 at 22:11
  • I am sry to ask again but can anybody help? – Akhil S A Aug 27 '14 at 20:39

0 Answers0