Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

JavaScript RSA with C# encrypting in Unity

Discussion in 'Scripting' started by Deleted User, Oct 2, 2019.

  1. Deleted User

    Deleted User

    Guest

    The server uses JSEncript to create private and public keys. The public key is then send to the webclient to provide a safe Login. Now I need to encript the user data
    {"u":"username","p":"password"}
    with a given public key. for example:

    -----BEGIN PUBLIC KEY-----
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwzWTnw4NNz4349i5O9Mv
    zfXIoDDlyrSYP/kTj3BbKy1j4CD27tWfHXKomeWKNgLAOZZg9DlHNn7O2ji7lpLg
    GTCL/7sU82si9tnbYy8waOqq1uIgzd1SjCN0ZpvWbopZeEpK2w2Ua3R/TgG7wSXf
    2uzOoRcThobYporK5g97My50dZrKCSKzAOqRxGOa3YkVRYJ1cVhn5db9CKg2UtR0
    plvxAHvIBQWzdmv20mjyekfRwl/CzVKf1b5VLqCznHbz7JPZMnHkqdVv1TKMlBMV
    Or7lf3MgzhsMmhUFghIx/kznUCQL9SrTkCTkeNiUfsfmzuWSTf7fsxREczxmFlIb
    FQIDAQAB
    -----END PUBLIC KEY-----

    Has anyone got an idea how to do that? I don't understand RSACryptoServiceProvider. It seems to me that it always needs to use it's own private key. Please help.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  3. Deleted User

    Deleted User

    Guest

    That does not work for me since the keys are not generated on my local machine.
    I only take a public key in base64 and have to encrpit login data with that.
     
  4. Deleted User

    Deleted User

    Guest

    I just found a solution:

    1. implement BouncyCastle.dll found here: http://www.bouncycastle.org/csharp/

    2.
    Code (CSharp):
    1.  
    2. using Org.BouncyCastle.Asn1;
    3. using Org.BouncyCastle.Crypto.Parameters;
    4. using Org.BouncyCastle.Security;
    5.  
    6. private string EncryptWithBC(string pkey, string stringToEncrypt)
    7.     {
    8.         Asn1Object obj = Asn1Object.FromByteArray(Convert.FromBase64String(pkey));
    9.  
    10.         DerSequence publicKeySequence = (DerSequence)obj;
    11.         DerBitString encodedPublicKey = (DerBitString)publicKeySequence[1];
    12.         DerSequence publicKey = (DerSequence)Asn1Object.FromByteArray(encodedPublicKey.GetBytes());
    13.  
    14.         DerInteger modulus = (DerInteger)publicKey[0];
    15.         DerInteger exponent = (DerInteger)publicKey[1];
    16.  
    17.         RsaKeyParameters keyParameters = new RsaKeyParameters(false, modulus.PositiveValue, exponent.PositiveValue);
    18.  
    19.         RSAParameters parameters = DotNetUtilities.ToRSAParameters(keyParameters);
    20.         RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    21.         rsa.ImportParameters(parameters);
    22.  
    23.         byte[] dataToEncrypt = Encoding.UTF8.GetBytes(stringToEncrypt);
    24.         byte[] encryptedData = rsa.Encrypt(dataToEncrypt, false);
    25.         return Convert.ToBase64String(encryptedData);
    26.     }
     
    palex-nx likes this.
  5. rodeowild

    rodeowild

    Joined:
    Dec 24, 2012
    Posts:
    19
    This seems very useful. How do I go about installing the dll?
     
  6. rodeowild

    rodeowild

    Joined:
    Dec 24, 2012
    Posts:
    19
    Oh, ok. I figured it out. It was super easy. Just drag the DLL into unity.