Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting a byte array to a X.509 certificate
    primarykey
    data
    text
    <p>I'm trying to port a piece of Java code into .NET that takes a Base64 encoded string, converts it to a byte array, and then uses it to make a X.509 certificate to get the modulus &amp; exponent for RSA encryption.</p> <p>This is the Java code I'm trying to convert:</p> <pre><code>byte[] externalPublicKey = Base64.decode("base 64 encoded string"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(externalPublicKey); Key publicKey = keyFactory.generatePublic(publicKeySpec); RSAPublicKey pbrtk = (java.security.interfaces.RSAPublicKey) publicKey; BigInteger modulus = pbrtk.getModulus(); BigInteger pubExp = pbrtk.getPublicExponent(); </code></pre> <p>I've been trying to figure out the best way to convert this into .NET. So far, I've come up with this:</p> <pre><code>byte[] bytes = Convert.FromBase64String("base 64 encoded string"); X509Certificate2 x509 = new X509Certificate2(bytes); RSA rsa = (RSA)x509.PrivateKey; RSAParameters rsaParams = rsa.ExportParameters(false); byte[] modulus = rsaParams.Modulus; byte[] exponent = rsaParams.Exponent; </code></pre> <p>Which to me looks like it should work, but it throws a <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptographicexception.aspx" rel="nofollow noreferrer">CryptographicException</a> when I use the base 64 encoded string from the Java code to generate the X509 certificate. The exact message I receive is:</p> <blockquote> <p>Cannot find the requested object.</p> </blockquote> <p>Is Java's X.509 implementation just incompatible with .NET's, or am I doing something wrong in my conversion from Java to .NET? </p> <p>Or is there simply no conversion from Java to .NET in this case?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload