首页 > 解决方案 > RSA.Cryptography “不正确的参数”抛出 C#

问题描述

我正在尝试为我和我的朋友设置一个混乱的服务器,但我遇到了 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);
     }
    

    }

标签: c#encryptionrsasos

解决方案


您会发现这个 stackoverflow 问题很有帮助,但它已经过时了解码 OAEP 填充时发生错误


推荐阅读