I am trying to port a piece of Java code to .NET that takes a Base64 encoded string, converts it to an array of bytes, and then uses it to get an X.509 certificate to get the module and metric for RSA encryption.
This is the Java code I'm trying to convert:
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();
I am trying to find the best way to convert this to .NET. So far I have come up with this:
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;
It seems to me that it should work, but it throws a CryptographicException when I use base 64 encoding from Java code to generate an X509 certificate. The exact message I receive is:
Cannot find the requested object.
Is the Java X.509 implementation simply incompatible with .NET, or am I doing something wrong in my conversion from Java to .NET?
Or is there simply no conversion from Java to .NET in this case?
source share