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:
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.
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.
I just found a solution:
-
implement BouncyCastle.dll found here: http://www.bouncycastle.org/csharp/
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
private string EncryptWithBC(string pkey, string stringToEncrypt)
{
Asn1Object obj = Asn1Object.FromByteArray(Convert.FromBase64String(pkey));
DerSequence publicKeySequence = (DerSequence)obj;
DerBitString encodedPublicKey = (DerBitString)publicKeySequence[1];
DerSequence publicKey = (DerSequence)Asn1Object.FromByteArray(encodedPublicKey.GetBytes());
DerInteger modulus = (DerInteger)publicKey[0];
DerInteger exponent = (DerInteger)publicKey[1];
RsaKeyParameters keyParameters = new RsaKeyParameters(false, modulus.PositiveValue, exponent.PositiveValue);
RSAParameters parameters = DotNetUtilities.ToRSAParameters(keyParameters);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(parameters);
byte[] dataToEncrypt = Encoding.UTF8.GetBytes(stringToEncrypt);
byte[] encryptedData = rsa.Encrypt(dataToEncrypt, false);
return Convert.ToBase64String(encryptedData);
}
1 Like
This seems very useful. How do I go about installing the dll?
Oh, ok. I figured it out. It was super easy. Just drag the DLL into unity.