• <bdo id='wI0R3'></bdo><ul id='wI0R3'></ul>

    <i id='wI0R3'><tr id='wI0R3'><dt id='wI0R3'><q id='wI0R3'><span id='wI0R3'><b id='wI0R3'><form id='wI0R3'><ins id='wI0R3'></ins><ul id='wI0R3'></ul><sub id='wI0R3'></sub></form><legend id='wI0R3'></legend><bdo id='wI0R3'><pre id='wI0R3'><center id='wI0R3'></center></pre></bdo></b><th id='wI0R3'></th></span></q></dt></tr></i><div id='wI0R3'><tfoot id='wI0R3'></tfoot><dl id='wI0R3'><fieldset id='wI0R3'></fieldset></dl></div>
  • <tfoot id='wI0R3'></tfoot>
    <legend id='wI0R3'><style id='wI0R3'><dir id='wI0R3'><q id='wI0R3'></q></dir></style></legend>

    <small id='wI0R3'></small><noframes id='wI0R3'>

      1. 如何在 C# 中使用公钥和私钥加密技术

        时间:2023-06-02
        • <i id='vuRpe'><tr id='vuRpe'><dt id='vuRpe'><q id='vuRpe'><span id='vuRpe'><b id='vuRpe'><form id='vuRpe'><ins id='vuRpe'></ins><ul id='vuRpe'></ul><sub id='vuRpe'></sub></form><legend id='vuRpe'></legend><bdo id='vuRpe'><pre id='vuRpe'><center id='vuRpe'></center></pre></bdo></b><th id='vuRpe'></th></span></q></dt></tr></i><div id='vuRpe'><tfoot id='vuRpe'></tfoot><dl id='vuRpe'><fieldset id='vuRpe'></fieldset></dl></div>
          • <tfoot id='vuRpe'></tfoot>

            <small id='vuRpe'></small><noframes id='vuRpe'>

              <bdo id='vuRpe'></bdo><ul id='vuRpe'></ul>

                  <tbody id='vuRpe'></tbody>
                <legend id='vuRpe'><style id='vuRpe'><dir id='vuRpe'><q id='vuRpe'></q></dir></style></legend>

                  本文介绍了如何在 C# 中使用公钥和私钥加密技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想使用公钥/私钥技术加密数据.我的意思是,用接收者的公钥加密,接收者可以用自己的私钥解密.

                  I want to encrypt data using public/private key technique. I mean, encrypt with the public key of receiver and the receiver can decrypt with their own private key.

                  我该怎么做?您有什么建议或示例代码吗?

                  How can I do that? Do you have any suggestion or sample code ?

                  推荐答案

                  代码示例:

                  private static string _privateKey;
                  private static string _publicKey;
                  private static UnicodeEncoding _encoder = new UnicodeEncoding();
                  
                  private static void RSA()
                  {
                    var rsa = new RSACryptoServiceProvider();
                    _privateKey = rsa.ToXmlString(true);
                    _publicKey = rsa.ToXmlString(false);
                  
                    var text = "Test1";
                    Console.WriteLine("RSA // Text to encrypt: " + text);
                    var enc = Encrypt(text);
                    Console.WriteLine("RSA // Encrypted Text: " + enc);
                    var dec = Decrypt(enc);
                    Console.WriteLine("RSA // Decrypted Text: " + dec);
                  }
                  
                  public static string Decrypt(string data)
                  {
                    var rsa = new RSACryptoServiceProvider();
                    var dataArray = data.Split(new char[] { ',' });
                    byte[] dataByte = new byte[dataArray.Length];
                    for (int i = 0; i < dataArray.Length; i++)
                    {
                      dataByte[i] = Convert.ToByte(dataArray[i]);
                    }
                  
                    rsa.FromXmlString(_privateKey);
                    var decryptedByte = rsa.Decrypt(dataByte, false);
                    return _encoder.GetString(decryptedByte);
                  }
                  
                  public static string Encrypt(string data)
                  {
                    var rsa = new RSACryptoServiceProvider();
                    rsa.FromXmlString(_publicKey);
                    var dataToEncrypt = _encoder.GetBytes(data);
                    var encryptedByteArray = rsa.Encrypt(dataToEncrypt, false).ToArray();
                    var length = encryptedByteArray.Count();
                    var item = 0;
                    var sb = new StringBuilder();
                    foreach (var x in encryptedByteArray)
                    {
                      item++;
                      sb.Append(x);
                  
                      if (item < length)
                        sb.Append(",");
                    }
                  
                    return sb.ToString();
                  }
                  

                  这篇关于如何在 C# 中使用公钥和私钥加密技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:RSACryptoServiceProvider 用自己的公钥和私钥初始化 下一篇:Rfc2898/PBKDF2 与 SHA256 作为 C# 中的摘要

                  相关文章

                  最新文章

                    • <bdo id='3H8gr'></bdo><ul id='3H8gr'></ul>

                    <small id='3H8gr'></small><noframes id='3H8gr'>

                    <legend id='3H8gr'><style id='3H8gr'><dir id='3H8gr'><q id='3H8gr'></q></dir></style></legend>
                    <tfoot id='3H8gr'></tfoot>
                    1. <i id='3H8gr'><tr id='3H8gr'><dt id='3H8gr'><q id='3H8gr'><span id='3H8gr'><b id='3H8gr'><form id='3H8gr'><ins id='3H8gr'></ins><ul id='3H8gr'></ul><sub id='3H8gr'></sub></form><legend id='3H8gr'></legend><bdo id='3H8gr'><pre id='3H8gr'><center id='3H8gr'></center></pre></bdo></b><th id='3H8gr'></th></span></q></dt></tr></i><div id='3H8gr'><tfoot id='3H8gr'></tfoot><dl id='3H8gr'><fieldset id='3H8gr'></fieldset></dl></div>