添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

My main issue is that I can’t load a X509Certificate2 in order to use SSL encryption in Unity.

I’m currently using Unity Editor 2021.3.5f1. Changing the Unity version is a not a viable solution for me.

So what I have tried.

  • Loading the certificate straight up:
  • X509Certificate2 serverCertificate = new X509Certificate2("c:/my/path/server_pfx.pfx");

    This has failed as I get the a Format Exception
    FormatException: Only integer can be converted Mono.Security.ASN1Convert.ToInt32

    The certificate was created by using

       openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -sha256 -keyout key.pem -out cert.pem
       openssl pkcs12 -export -in cert.pem -inkey key.pem -out server_pfx.pfx -macalg sha1
    
  • Loading the certificate withouth a private key and adding the private key in C#
  • X509Certificate2 pubOnly = new X509Certificate2("c:/my/path/cert.pem"); // Loading the prviate Key. string privateKeyPem = File.ReadAllText("c:/my/path/key.pem"); RSA rsa = RSA.Create(); var privateKey = privateKeyPem.Replace("-----BEGIN PRIVATE KEY-----", string.Empty).Replace("-----END PRIVATE KEY-----", string.Empty); privateKey = privateKey.Replace(Environment.NewLine, string.Empty); var privateKeyBytes = Convert.FromBase64String(privateKey); rsa.ImportPkcs8PrivateKey(privateKeyBytes, out int _); // Now creating the certificate with the private key. X509Certificate2 pubWithPrivate = pubOnly.CopyWithPrivateKey(rsa); this.serverCertificate = new X509Certificate2(pubWithPrivate.Export(X509ContentType.Pfx));

    This doesn’t work either because the rsa.ImportPkcs8PrivateKey throws a PlatformNotSupported Exception.

    I have tried to use Unity Transport. The version is 1.0.0.
    I install the package but when I get to this line of code:

    var settings = new NetworkSettings();
        settings.WithSecureServerParameters(
            certificate: SecureParameters.MyGameServerCertificate,
            privateKey: SecureParameters.myGameServerPrivateKey);
    

    From here (Encrypted communications | Unity Transport | 2.0.2)

    I get a compilation error saying that WithSecureServerParameters does not exist. As far as I’ve ben able to tell the function is available on Version 1.0.0 of Unity Transport, so I’ve no idea what do.

    Can anyone tell me a fix for ANY of these issues? Or Provide me with another thing to try?