I need to make the following code work on WP8, the problem is that there is no X509Certificate2 class on WP8, I have tried using bouncy castle apis but I haven't really managed to figure it out.
Is there a way to make this code work on WP8?
private string InitAuth(X509Certificate2 certificate, string systemId, string username, string password)
{
byte[] plainBytes = Encoding.UTF8.GetBytes(password);
var cipherB64 = string.Empty;
using (var rsa = (RSACryptoServiceProvider)certificate.PublicKey.Key)
cipherB64 = systemId + "^" + username + "^" + Convert.ToBase64String(rsa.Encrypt(plainBytes, true));
return cipherB64;
}
It can be used to get information about an existing certificate (valid dates, issuer, etc.). It had simple methods/operations (i.e. reading a cert from disk). The x509Certificate2 is a subclass of x509Certificate with additional functionality. It represents an actual X509 certificate.
No, you should not dispose certificate object while the application runs, because when requested, IdentityServer will attempt to use disposed certificate object and will fail.
X509Certificate2(Byte[], SecureString, X509KeyStorageFlags) Initializes a new instance of the X509Certificate2 class using a byte array, a password, and a key storage flag. X509Certificate2(Byte[], String) Initializes a new instance of the X509Certificate2 class using a byte array and a password.
The X509Chain object has a global error status called ChainStatus that should be used for certificate validation. The rules governing certificate validation are complex, and it is easy to oversimplify the validation logic by ignoring the error status of one or more of the elements involved.
Can't you just work around the availability of X509Certificate2
?
private string InitAuth(X509Certificate certificate, string systemId, string username, string password)
{
byte[] plainBytes = Encoding.UTF8.GetBytes(password);
var cipherB64 = string.Empty;
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Create a new instance of RSAParameters.
RSAParameters RSAKeyInfo = new RSAParameters();
//Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = certificate.getPublicKey();
RSAKeyInfo.Exponent = new byte[3] {1,0,1};;
//Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo);
using (RSA)
cipherB64 = systemId + "^" + username + "^" + Convert.ToBase64String(RSA.Encrypt(plainBytes, true));
return cipherB64;
}
DISCLOSURE: I did not try the code above because I don't have a C# runtime environment at my disposal currently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With