locked
[U8.1] [C#] Generate RSA public key - from key that was sent from the server RRS feed

  • Question

  • Hi, 

    I'm trying to generate RSA public to encrypt a message to the server, after i got the public key from it.

    I've tried using "CryptographicBuffer" & "RSACryptoServiceProvider" to encrypt with "CryptographicEngine" but got an exception.

    I've tried Bouncy Castle & some other 3rd party - without any success...

    Any ideas how to do that ??

    Thank you vary much.


    Sunday, September 6, 2015 11:31 AM

Answers

All replies

  • Hi,

    Welcome to the Developing Universal Windows apps forum! 

    What type of app are you writing? As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    >>I've tried using "CryptographicBuffer" & "RSACryptoServiceProvider" to encrypt with "CryptographicEngine" but got an exception.

    Please provide detailed exception and code snippets for troubleshooting.

    >>I'm trying to generate RSA public to encrypt a message to the server

    Please see my reply in this case about how to implement RSA Algorithm in Windows Runtime app: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/d3e5b697-2942-49e9-a56d-a890f31849fc/u81using-given-modulus-and-exponent-to-encrypt-data-using-rsa-algorithm-in-universal-apps?forum=wpdevelop


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 7, 2015 11:10 AM
  • Hi,

    Thanks for your reply.

    I'm writing universal app, the server written in Java.

    I'm getting the public key from the server and generate a public key that i want to encrypt the data with (using rsa provider).

    byte[] publicKeyBytes = Convert.FromBase64String(serverPublicKey);
    
    AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes);                
    
    RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
    
    RSAParameters rsaParameters = new RSAParameters
    
    {   Modulus = rsaKeyParameters.Modulus.ToByteArray(), Exponent = rsaKeyParameters.Exponent.ToByteArray()     };
    
    rsa.ImportParameters(rsaParameters); // set with my public key.
    
    var encryptedData = rsa.Encrypt(Encoding.Unicode.GetBytes(message), false); 
    
    return Convert.ToBase64String(encryptedData, 0 ,encryptedData.Length);
    

    well it works fine - but i need to use CBC cipher and not EBC (default).

    any ideas how to change the cipher mode ?

    thank you,

    nadav

    Tuesday, September 8, 2015 7:48 AM
  • Hi,

    >>but i need to use CBC cipher and not EBC (default). any ideas how to change the cipher mode ?

    We need to select the appropriate algorithm, select SymmetricAlgorithmNames

    You can use SymmetricAlgorithmNames.AesCbcPkcs7 algorithm, for example(change code to fit your scenario):

    // convert message string to binary
    var input = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);
    var iv = CryptographicBuffer.DecodeFromBase64String(this.IV);
    
    // select the appropriate algorithm
    var encryptor = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
      
    // get the key
    var key = encryptor.CreateSymmetricKey(CryptographicBuffer.DecodeFromBase64String(this.symmetricKey));
         
    // do the encryption
    var encryptedText = CryptographicEngine.Encrypt(key, input, iv);
      
    // convert the encrypted text into hex format and return
    return CryptographicBuffer.EncodeToHexString(encryptedText);
    


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 8, 2015 9:58 AM
  • Hi,

    why select symmetric algorithm?

    i'm trying to use asymmetric algorithm, is there another way accomplish ?

    the server uses asymmetric algorithm 

    Thanks. 

    Tuesday, September 8, 2015 10:13 AM
  • Hi,

    why select symmetric algorithm?

    i'm trying to use asymmetric algorithm, is there another way accomplish ?

    the server uses asymmetric algorithm 

    Thanks. 


    Hi nadav.beithalevi,

    >>i'm trying to use asymmetric algorithm, is there another way accomplish ?

    As far as I know, the answer is no for asymmetric algorithm, the supported algorithm has been listed here: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.security.cryptography.core.asymmetricalgorithmnames.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 16, 2015 7:56 AM