1

I'm trying to set up a messing server for me and my friends and I ran into issues with RSA Decryption.

  1. The correct keys are used

  2. If I enable OAEP padding I get a error that simply states "OAEPpadding"

  3. I'm losing my mind on this bug, I'm posting the script below.

  4. Encryption works fine, its just decryption that's problematic

  5. Please Help

    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);
     }
    

    }

  • 1
    `return System.Text.Encoding.ASCII.GetString(cipher);` This can never work. The result of encryption is a sequence of arbitrary bytes that will not represent a valid ASCII encoded string. The decoder will simply replace impossible encodings with something that's valid and move on. The result is corrupted data. There is nothing wrong with byte arrays (byte[]), they can be passed between functions and over network streams with ease. However, if you must convert to a string then use a proper encoding like base64. – President James K. Polk Nov 23 '20 at 13:20
  • Thanks James, you were right about that I don't know why I tried to parse it to String. – user14690246 Nov 23 '20 at 21:53

2 Answers2

0

You can find this stackoverflow question helpful, but it's quite out of date Error occurred while decoding OAEP padding

GoodboY
  • 299
  • 2
  • 12
0

Don't use the same provider. Do this instead:

var publicKey = RSA.Create();

publicKey.ImportParameters(PUB_PARAMS);

var privateKey = RSA.Create();

privateKey.ImportParameters(PRIV_PARAMS);

Steve Johnson
  • 2,958
  • 13
  • 15