我正在尝试将一段 Java 代码移植到 .NET 中,该代码采用 Base64 编码的字符串,将其转换为字节数组,然后使用它来制作 X.509 证书以获得 RSA 加密的模数和指数。
这是我要转换的 Java 代码:
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();
Run Code Online (Sandbox Code Playgroud)
我一直试图找出将其转换为 .NET 的最佳方法。到目前为止,我想出了这个:
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;
Run Code Online (Sandbox Code Playgroud)
在我看来它应该可以工作,但是当我使用 Java …