【问题标题】:RSA.Cryptography "Incorrect Parameter" thrown C#RSA.Cryptography “不正确的参数”抛出 C#
【发布时间】:2020-11-23 06:49:40
【问题描述】:

我正在尝试为我和我的朋友设置一个混乱的服务器,但遇到了 RSA 解密的问题。

  1. 使用了正确的键

  2. 如果我启用 OAEP 填充,我会收到一条错误消息,仅显示“OAEPpadding”

  3. 我对这个错误失去了理智,我在下面发布脚本。

  4. 加密工作正常,只是解密有问题

  5. 请帮忙

    using System;
    using System.Net.Sockets;
    using System.Net;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Security.Cryptography;
    using System.Xml.Serialization;
    
    namespace Server_WIN_
    {
    class Program
    {
     public static XmlSerializer xs = new XmlSerializer(typeof(RSAParameters));
     public static TcpListener server = new TcpListener(IPAddress.Parse("192.168.1.93"), 78);
     public static TcpClient client = null;
     public static NetworkStream canwetalk = null;
     public static RSACryptoServiceProvider csp = new RSACryptoServiceProvider(4096);
     public static RSAParameters publickey;
     public static RSAParameters privatekey;
     static Program()
     {
         server.Start();
         csp.PersistKeyInCsp = false;
         publickey = csp.ExportParameters(false);
         privatekey = csp.ExportParameters(true);
    
         client = server.AcceptTcpClient();
         canwetalk = client.GetStream();
     }
             public static void Main(string[] args)
     {
             string strHostName = "";
             strHostName = Dns.GetHostName();
             // Then using host name, get the IP address list..
             IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
             IPAddress[] addr = ipEntry.AddressList;
         Random ran = new Random();
         HashAlgorithm sha = SHA256.Create();
         string msg = "";
         byte[] buffer = new byte[4096];
         msg = "test";
         msg = Encrypt(msg);
         msg = Decrypt(msg);
         Console.WriteLine(msg);
     }
    
     public static string PublicKeyString()
     {
         byte[] bytes = new byte[4096];
         var sw = new StringWriter();
         var xs = new XmlSerializer(typeof(RSAParameters));
         xs.Serialize(sw, publickey);
         return sw.ToString();
     }
     public static string PrivateKeyString()
     {
         byte[] bytes = new byte[4096];
         var sw = new StringWriter();
         var xs = new XmlSerializer(typeof(RSAParameters));
         xs.Serialize(sw, privatekey);
         return sw.ToString();
     }
     public static string Encrypt(string msg)
     {
         csp.ImportParameters(publickey);
    
         byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
         byte[] cipher = csp.Encrypt(data, false);
         return System.Text.Encoding.ASCII.GetString(cipher);
     }
     public static string Decrypt(string msg)
     {
         try
         { 
             csp.ImportParameters(privatekey);
             byte[] decrypted = csp.Decrypt(System.Text.Encoding.ASCII.GetBytes(msg), false);
             return System.Text.Encoding.Unicode.GetString(decrypted);
         }
         catch(CryptographicException e)
         {
             string p = e.ToString();
             Console.WriteLine(p);
         }
         return "";
     }
     public static void ExportPublicKey()
     {
         string msg = PublicKeyString();
         byte[] buffer = new byte[4096];
         byte[] msg1 = System.Text.Encoding.ASCII.GetBytes(msg);
         canwetalk.Write(msg1, 0, msg1.Length);
    
     }
     public static void ToStream(string msg, bool Encryption)
     {
         if (Encryption)
         {
             msg = Encrypt(msg);
             byte[] msgbytes = System.Text.Encoding.ASCII.GetBytes(msg);
             canwetalk.Write(msgbytes, 0, msgbytes.Length);
         }
         else
         {
             byte[] msgbytes = System.Text.Encoding.ASCII.GetBytes(msg);
             canwetalk.Write(msgbytes, 0, msgbytes.Length);
         }
     }
     public static string ReadStream()
     {
         byte[] buffer = new byte[4096];
         int i = canwetalk.Read(buffer,0,buffer.Length);
         return System.Text.Encoding.ASCII.GetString(buffer,0,i);
     }
    

    }

【问题讨论】:

  • return System.Text.Encoding.ASCII.GetString(cipher); 这永远行不通。加密的结果是一个不代表有效 ASCII 编码字符串的任意字节序列。解码器将简单地用有效的东西替换不可能的编码并继续前进。结果是损坏的数据。字节数组 (byte[]) 没有任何问题,它们可以轻松地在函数之间和通过网络流传递。但是,如果您必须转换为字符串,请使用适当的编码,例如 base64。
  • 谢谢詹姆斯,你说得对,我不知道为什么我试图将它解析为字符串。

标签: c# encryption rsa sos


【解决方案1】:

你会发现这个 stackoverflow 问题很有帮助,但它已经过时了 Error occurred while decoding OAEP padding

【讨论】:

    【解决方案2】:

    不要使用相同的提供程序。改为这样做:

    var publicKey = RSA.Create();

    publicKey.ImportParameters(PUB_PARAMS);

    var privateKey = RSA.Create();

    privateKey.ImportParameters(PRIV_PARAMS);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2017-10-22
      • 2021-09-23
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      相关资源
      最近更新 更多