1. <legend id='07SNa'><style id='07SNa'><dir id='07SNa'><q id='07SNa'></q></dir></style></legend>

        <bdo id='07SNa'></bdo><ul id='07SNa'></ul>

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

        <small id='07SNa'></small><noframes id='07SNa'>

        C# RSA 加密/解密与传输

        时间:2023-06-01
          <bdo id='ohDkA'></bdo><ul id='ohDkA'></ul>

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

            <legend id='ohDkA'><style id='ohDkA'><dir id='ohDkA'><q id='ohDkA'></q></dir></style></legend>

                <tfoot id='ohDkA'></tfoot>
                  <tbody id='ohDkA'></tbody>
              • <i id='ohDkA'><tr id='ohDkA'><dt id='ohDkA'><q id='ohDkA'><span id='ohDkA'><b id='ohDkA'><form id='ohDkA'><ins id='ohDkA'></ins><ul id='ohDkA'></ul><sub id='ohDkA'></sub></form><legend id='ohDkA'></legend><bdo id='ohDkA'><pre id='ohDkA'><center id='ohDkA'></center></pre></bdo></b><th id='ohDkA'></th></span></q></dt></tr></i><div id='ohDkA'><tfoot id='ohDkA'></tfoot><dl id='ohDkA'><fieldset id='ohDkA'></fieldset></dl></div>
                  本文介绍了C# RSA 加密/解密与传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在网上看到了大量使用 System.Security.Cryptography.RSACryptoServiceProvider 的 C# 加密/解密教程和示例,但我希望能够做到的是:

                  I've seen plenty of encryption/decryption tutorials and examples on the net in C# that use the System.Security.Cryptography.RSACryptoServiceProvider, but what I'm hoping to be able to do is:

                  • 创建 RSA 公钥/私钥对
                  • 传输公钥(或者为了概念验证,只需将其移动到字符串变量中)
                  • 创建新的 RSA 加密提供程序并使用公钥加密字符串
                  • 将加密的字符串(或数据)传输回原始加密提供者并解密字符串

                  谁能给我指出一个有用的资源?

                  Could anyone point me to a useful resource for this?

                  推荐答案

                  确实有足够的例子,但不管怎样,给你

                  well there are really enough examples for this, but anyway, here you go

                  using System;
                  using System.Security.Cryptography;
                  
                  namespace RsaCryptoExample
                  {
                    static class Program
                    {
                      static void Main()
                      {
                        //lets take a new CSP with a new 2048 bit rsa key pair
                        var csp = new RSACryptoServiceProvider(2048);
                  
                        //how to get the private key
                        var privKey = csp.ExportParameters(true);
                  
                        //and the public key ...
                        var pubKey = csp.ExportParameters(false);
                  
                        //converting the public key into a string representation
                        string pubKeyString;
                        {
                          //we need some buffer
                          var sw = new System.IO.StringWriter();
                          //we need a serializer
                          var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
                          //serialize the key into the stream
                          xs.Serialize(sw, pubKey);
                          //get the string from the stream
                          pubKeyString = sw.ToString();
                        }
                  
                        //converting it back
                        {
                          //get a stream from the string
                          var sr = new System.IO.StringReader(pubKeyString);
                          //we need a deserializer
                          var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
                          //get the object back from the stream
                          pubKey = (RSAParameters)xs.Deserialize(sr);
                        }
                  
                        //conversion for the private key is no black magic either ... omitted
                  
                        //we have a public key ... let's get a new csp and load that key
                        csp = new RSACryptoServiceProvider();
                        csp.ImportParameters(pubKey);
                  
                        //we need some data to encrypt
                        var plainTextData = "foobar";
                  
                        //for encryption, always handle bytes...
                        var bytesPlainTextData = System.Text.Encoding.Unicode.GetBytes(plainTextData);
                  
                        //apply pkcs#1.5 padding and encrypt our data 
                        var bytesCypherText = csp.Encrypt(bytesPlainTextData, false);
                  
                        //we might want a string representation of our cypher text... base64 will do
                        var cypherText = Convert.ToBase64String(bytesCypherText);
                  
                  
                        /*
                         * some transmission / storage / retrieval
                         * 
                         * and we want to decrypt our cypherText
                         */
                  
                        //first, get our bytes back from the base64 string ...
                        bytesCypherText = Convert.FromBase64String(cypherText);
                  
                        //we want to decrypt, therefore we need a csp and load our private key
                        csp = new RSACryptoServiceProvider();
                        csp.ImportParameters(privKey);
                  
                        //decrypt and strip pkcs#1.5 padding
                        bytesPlainTextData = csp.Decrypt(bytesCypherText, false);
                  
                        //get our original plainText back...
                        plainTextData = System.Text.Encoding.Unicode.GetString(bytesPlainTextData);
                      }
                    }
                  }
                  

                  附带说明:对 Encrypt() 和 Decrypt() 的调用有一个 bool 参数,可在 OAEP 和 PKCS#1.5 填充之间切换……如果您的情况可用,您可能希望选择 OAEP

                  as a side note: the calls to Encrypt() and Decrypt() have a bool parameter that switches between OAEP and PKCS#1.5 padding ... you might want to choose OAEP if it's available in your situation

                  这篇关于C# RSA 加密/解密与传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用纯 .net 框架生成和签署证书请求 下一篇:如何在 C# 中生成加密安全的伪随机数?

                  相关文章

                  最新文章

                    <tfoot id='bJRKK'></tfoot>

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

                    1. <legend id='bJRKK'><style id='bJRKK'><dir id='bJRKK'><q id='bJRKK'></q></dir></style></legend>

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