Closed BabyWhite69 closed 1 year ago
Please post the exception full stack trace. Also note that the code above only works on .NET Core (3.1 and higher, if I remember correctly).
I'm using Unity 2021.3.11f1
which uses .netstandard 2.1
Edit: Sorry, I forgot to put this excerpt earlier
CryptographicException: Unable to decode certificate.
System.Security.Cryptography.X509Certificates.X509Certificate2ImplMono..ctor (System.Byte[] rawData, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) (at <a971fed4bf844502b6501dbde9cced46>:0)
Mono.X509PalImpl.ImportFallback (System.Byte[] data) (at <a971fed4bf844502b6501dbde9cced46>:0)
Mono.X509PalImplMono.Import (System.Byte[] data) (at <a971fed4bf844502b6501dbde9cced46>:0)
Mono.SystemCertificateProvider.Import (System.Byte[] data, Mono.CertificateImportFlags importFlags) (at <a971fed4bf844502b6501dbde9cced46>:0)
System.Security.Cryptography.X509Certificates.X509Helper.Import (System.Byte[] rawData) (at <9aad1b3a47484d63ba2b3985692d80e9>:0)
System.Security.Cryptography.X509Certificates.X509Certificate..ctor (System.Byte[] data) (at <9aad1b3a47484d63ba2b3985692d80e9>:0)
System.Security.Cryptography.X509Certificates.X509Certificate2..ctor (System.Byte[] rawData) (at <a971fed4bf844502b6501dbde9cced46>:0)
JWT_Test.CreateCertificate () (at Assets/JWT_Test.cs:43)
JWT_Test..cctor () (at Assets/JWT_Test.cs:13)
Rethrow as TypeInitializationException: The type initializer for 'JWT_Test' threw an exception.
.NET Standard is a compatibility API layer. What matters is the actual runtime by the app itself. Looks like it's Mono in your case. And that might explain why it fails, because the underlying crypto API doesn't support the operation.
Oh, I still don't understand much about apis and framework. and yes, unity uses mono
or IL2CPP
, will it work with IL2CPP?
I frankly don't know what's that (beyond a basic Google search) so can't say. But it might not as it depends on the crypto API availability on certain platform.
I'd suggest googling how to construct an X509Certificate2 object specifically on Unity.
I was reading this post and maybe this is the solution, I just need to know how you created the key pair and what format they are in, specifically the command to test it the same way, because if I create the key pair with the format I described it gives me another error, however in this one it is trying to decode the certificate and maybe it is due to the password...
Overview of what I am trying to do:
I have a c# server that is trying to communicate with an API written with Node.js through a token with RSA SHA256.
In order for the API to receive the token to me, the c# server has to create a token with the private key using the RS256 algorithm.
Now the API will validate the token with the public key and give it access to the CRUD.
Key creation:
Create a pair of keys, one public and one private with the following command:
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
And replace the public key in PEM format with this command:
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
The private key I passed it to the Unity editor which I accessed with a script and passed it to String format which gave me an error
cannot convert from 'string' to 'System.Security.Cryptography.RSA'
, I read this post on GitHub which had the same error, but now it is giving me an exception to access this key created following the steps in the post mentioned above.I am receiving the error from the line:
public static readonly X509Certificate2 CertificateWithPrivateKey = CreateCertificate();
### Important note: In the Script written above the public and private key are provided in a different format than the one described for my needs which were obtained from the post mentioned above.