如何在 RSA 加密 C#(.net 标准 2.0)中使用 public 密钥

How to use public key in RSA encryption C# (.net standard 2.0)

我正在开发一个 .net 标准(.net 标准 2.0)库。

我有一个 public 密钥,它是在服务器端创建的,我通过 api 调用收到了它。

现在我有一个要使用 RSA 算法加密的字符串。此外,我必须在加密我拥有的字符串时使用此 public 密钥。

我检查了各种样本,但没有任何帮助。

试过这个RSA public key encryption in C#

但出现错误“此平台不支持操作。” .

尝试了 this 一个,但找不到使用我从服务器收到的 public 密钥的方法。

这是我试过的最新代码。我不确定如何以及在何处使用我的 public 密钥字符串。

static public byte[] Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
    {
        try
        {
            byte[] encryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSA.ImportParameters(RSAKey);
                encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
            }
            return encryptedData;
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.Message);
            return null;
        }
    }
    static public byte[] Decryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
    {
        try
        {
            byte[] decryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSA.ImportParameters(RSAKey);
                decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
            }
            return decryptedData;
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.ToString());
            return null;
        }
    }

在这里我可以看到RSAKey但是它不接受字符串输入。

任何人都可以指导我一些示例,通过这些示例我可以使用我拥有的 public 加密和解密简单的文本。

谢谢, 努鲁

下面是一个完整的工作代码,它使用 XML 中的 RSA public 密钥加密字符串,并使用 RSA 私钥解密密文(以及 XML编码)。密文以 base64 编码。这里给一个在线编译器link看代码运行:https://repl.it/@javacrypto/CpcCsharpRsaEncryptionOaepSha1String#main.cs

输出:

RSA 2048 encryption OAEP SHA-1 string
plaintext: The quick brown fox jumps over the lazy dog

* * * encrypt the plaintext with the RSA public key * * *
ciphertextBase64: lFZwBN5SnlqAKEYktjjTmDUyIAFA86CDIrOyPeRLVXLIManP6wTUgZ3NBSYynfgwXTmVzhPQL3wZ6kngWIgVbl1sMOSiTt6BOvop5HwEU6ejUOrhgDzuxSnA4e8txNG0X8NZ4kt1bzE42gHlbjxHG+CYawAVYECPMyZ4Jc/n7qH8McsUh3n/KUOB2h5R6DULjf2Qfl4aQHppbEeECNmpSFJFUSu3iGBI7hacHbb+1myyLteS1o3FiBuSVbG+L7h8DEokbhE5yVTwamFF+qM+/HTWeGfE+CGC6Kb8cAucP7Zoov6e+Gr7aFYMvhrWjTvApzMBu5XD0j3JzxQD9OmA8g==

* * * decrypt the ciphertext with the RSA private key * * *
ciphertextReceivedBase64: lFZwBN5SnlqAKEYktjjTmDUyIAFA86CDIrOyPeRLVXLIManP6wTUgZ3NBSYynfgwXTmVzhPQL3wZ6kngWIgVbl1sMOSiTt6BOvop5HwEU6ejUOrhgDzuxSnA4e8txNG0X8NZ4kt1bzE42gHlbjxHG+CYawAVYECPMyZ4Jc/n7qH8McsUh3n/KUOB2h5R6DULjf2Qfl4aQHppbEeECNmpSFJFUSu3iGBI7hacHbb+1myyLteS1o3FiBuSVbG+L7h8DEokbhE5yVTwamFF+qM+/HTWeGfE+CGC6Kb8cAucP7Zoov6e+Gr7aFYMvhrWjTvApzMBu5XD0j3JzxQD9OmA8g==
decryptedData: The quick brown fox jumps over the lazy dog

安全警告:代码使用硬编码RSA [示例]密钥仅用于演示。该代码没有异常处理,仅用于教育目的。

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

class RsaEncryptionOaepSha1 {
    static void Main() {
    Console.WriteLine("RSA 2048 encryption OAEP SHA-1 string");
    string dataToEncryptString = "The quick brown fox jumps over the lazy dog";
    Console.WriteLine("plaintext: " + dataToEncryptString);

    // # # # usually we would load the private and public key from a file or keystore # # #
    // # # # here we use hardcoded keys for demonstration - don't do this in real programs # # #
    string filenamePrivateKeyXml = "privatekey2048.xml";
    string filenamePublicKeyXml = "publickey2048.xml";

        try {
      // encryption
        Console.WriteLine("\n* * * encrypt the plaintext with the RSA public key * * *");
        //string publicKeyLoad = loadRsaPublicKeyPem();
        // use this in production
        string publicKeyLoad = File.ReadAllText(filenamePublicKeyXml);
        byte[] dataToEncrypt = Encoding.UTF8.GetBytes(dataToEncryptString);
        string ciphertextBase64 = Base64Encoding(rsaEncryptionOaepSha1(publicKeyLoad, dataToEncrypt));
        //string ciphertextBase64 = "";
        Console.WriteLine("ciphertextBase64: " + ciphertextBase64);

        // transport the encrypted data to recipient

        // receiving the encrypted data, decryption
        Console.WriteLine("\n* * * decrypt the ciphertext with the RSA private key * * *");
        string ciphertextReceivedBase64 = ciphertextBase64;
        Console.WriteLine("ciphertextReceivedBase64: " + ciphertextReceivedBase64);
        //string privateKeyLoad = loadRsaPrivateKeyPem();
        // use this in production
        string privateKeyLoad = File.ReadAllText(filenamePrivateKeyXml);
        byte[] ciphertextReceived = Base64Decoding(ciphertextReceivedBase64);
        byte[] decryptedtextByte = rsaDecryptionOaepSha1(privateKeyLoad, ciphertextReceived);
        Console.WriteLine("decryptedData: " + Encoding.UTF8.GetString(decryptedtextByte, 0, decryptedtextByte.Length));
        }
        catch(ArgumentNullException) {
            Console.WriteLine("The data was not RSA encrypted");
        }
    }

  public static byte[] rsaEncryptionOaepSha1(string publicKeyXml, byte[] plaintext) {
    RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(2048);
        RSAalg.PersistKeyInCsp = false;
        RSAalg.FromXmlString(publicKeyXml);
        return RSAalg.Encrypt(plaintext, true);
  }

  public static byte[] rsaDecryptionOaepSha1(string privateKeyXml, byte[] ciphertext) {
    RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(2048);
        RSAalg.PersistKeyInCsp = false;
        RSAalg.FromXmlString(privateKeyXml);
    return RSAalg.Decrypt(ciphertext, true);
  }

  static string Base64Encoding(byte[] input) {
    return Convert.ToBase64String(input);
  }

  static byte[] Base64Decoding(String input) {
    return Convert.FromBase64String(input);
  }

  public static string loadRsaPublicKeyPem() {
    return "<RSAKeyValue><Modulus>8EmWJUZ/Osz4vXtUU2S+0M4BP9+s423gjMjoX+qP1iCnlcRcFWxthQGN2CWSMZwR/vY9V0un/nsIxhZSWOH9iKzqUtZD4jt35jqOTeJ3PCSr48JirVDNLet7hRT37Ovfu5iieMN7ZNpkjeIG/CfT/QQl7R+kO/EnTmL3QjLKQNV/HhEbHS2/44x7PPoHqSqkOvl8GW0qtL39gTLWgAe801/w5PmcQ38CKG0oT2gdJmJqIxNmAEHkatYGHcMDtXRBpOhOSdraFj6SmPyHEmLBishaq7Jm8NPPNK9QcEQ3q+ERa5M6eM72PpF93g2p5cjKgyzzfoIV09Zb/LJ2aW2gQw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
  }

  public static string loadRsaPrivateKeyPem() {
    return "<RSAKeyValue><Modulus>8EmWJUZ/Osz4vXtUU2S+0M4BP9+s423gjMjoX+qP1iCnlcRcFWxthQGN2CWSMZwR/vY9V0un/nsIxhZSWOH9iKzqUtZD4jt35jqOTeJ3PCSr48JirVDNLet7hRT37Ovfu5iieMN7ZNpkjeIG/CfT/QQl7R+kO/EnTmL3QjLKQNV/HhEbHS2/44x7PPoHqSqkOvl8GW0qtL39gTLWgAe801/w5PmcQ38CKG0oT2gdJmJqIxNmAEHkatYGHcMDtXRBpOhOSdraFj6SmPyHEmLBishaq7Jm8NPPNK9QcEQ3q+ERa5M6eM72PpF93g2p5cjKgyzzfoIV09Zb/LJ2aW2gQw==</Modulus><Exponent>AQAB</Exponent><P>/8atV5DmNxFrxF1PODDjdJPNb9pzNrDF03TiFBZWS4Q+2JazyLGjZzhg5Vv9RJ7VcIjPAbMy2Cy5BUffEFE+8ryKVWfdpPxpPYOwHCJSw4Bqqdj0Pmp/xw928ebrnUoCzdkUqYYpRWx0T7YVRoA9RiBfQiVHhuJBSDPYJPoP34k=</P><Q>8H9wLE5L8raUn4NYYRuUVMa+1k4Q1N3XBixm5cccc/Ja4LVvrnWqmFOmfFgpVd8Bc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