Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

X509Certificate2 to X509Certificate on Windows Phone 8

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;
    }
like image 342
jjdev80 Avatar asked Apr 24 '13 08:04

jjdev80


People also ask

What is the difference between x509Certificate and X509Certificate2?

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.

Should I dispose X509Certificate2?

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.

What is X509Certificate2 C#?

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.

What is X509Chain?

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.


1 Answers

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.

like image 68
likeitlikeit Avatar answered Sep 20 '22 02:09

likeitlikeit