X509encodedkeyspec和keyfactory的C#替代

时间:2019-04-01 09:36:13

标签: java c# rsa x509

我需要将以下JAVA代码转换为C#。该代码用于使用API​​密钥和公共密钥生成授权令牌。我无法找到所提供代码的C#替代品。

我已经探索过使用BouncyCastle替代X509EncodedKeySpec和KeyFactory。

这是用JAVA编写的解决方案,我需要在c#中做同样的事情

  KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  Cipher cipher = Cipher.getInstance("RSA");
  byte[] encodedPublicKey = Base64.decodeBase64(publicKey);
  System.out.println("Bytes = " + encodedPublicKey);
  X509EncodedKeySpec publicKeySpec = new 
  X509EncodedKeySpec(encodedPublicKey);
  PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

  cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  byte[] encryptedApiKey = 
  Base64.encodeBase64(cipher.doFinal(apiKey.getBytes("UTF-8")));
  return new String(encryptedApiKey, "UTF-8");

到目前为止,这是我的c#,缺少的组件是X509EncodedKeySpec。

        var RSA = new RSACryptoServiceProvider(4096);

        var RSAPublicKey = RSA.ExportParameters(false);

        RSA = new RSACryptoServiceProvider();
        RSA.ImportParameters(RSAPublicKey);

        var APIKeyRSACipher = 
        RSA.Encrypt(Convert.FromBase64String(APIKey),false);

        var DigestedRSACipher = Convert.ToBase64String(APIKeyRSACipher);

        Console.WriteLine(DigestedRSACipher);
        Console.ReadKey();

任何帮助/建议都将不胜感激,因为我已经尝试了一段时间了:)

0 个答案:

没有答案