The data to be decrypted exceeds the maximum for this modulus of 128 bytes

最后都变了- 提交于 2019-12-11 04:32:06

问题


I'm trying to establish a secure connection between a Client and a Server with RSA/AES (i think you know how that works) now my Problem is, i get that error message written in the title. I know, there are 2 or 3 other threads with the same title, but all of them try to en- and decrypt from a file on the pc. i get that error at the encryption of the AES key which was encrypted using the RSA public key which is the normal way to do as i know.

here my "Client" and "Server" applications

Client:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace ClientLizenz
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DoTheThing();
        }

        private void DoTheThing()
        {
            try
            {
                textBox2.Text = "Passed Beginning";
                Encoding test = Encoding.Default;
                UTF8Encoding bla = new UTF8Encoding();
                Int32 port = 14000;
                TcpClient client = new TcpClient("192.168.1.177", port);
                NetworkStream stream = client.GetStream();
                stream.ReadTimeout = System.Threading.Timeout.Infinite;
                stream.WriteTimeout = System.Threading.Timeout.Infinite;
                textBox2.Text = "Passed Socket Creation";

                RSACryptoServiceProvider RSAC = new RSACryptoServiceProvider(4096);
                byte[] Key = bla.GetBytes(RSAC.ToXmlString(false));
                stream.Write(Key, 0, Key.Length);
                textBox2.Text = "Passed sending KEy";
                byte[] encryAES = new byte[1024];
                stream.Read(encryAES, 0, encryAES.Length);
                textBox2.Text = "Passed receiving Encrypted AES Key";
                byte[] decryAES = RSAC.Decrypt(encryAES, true);
                string decryAesString = string.Empty;
                textBox2.Text = "Passed Decryption";
                foreach (byte item in decryAES)
                {
                    decryAesString += ((int)item).ToString();
                }
                textBox1.Text = decryAesString;
                stream.Close();
                stream.Flush();
                stream.Dispose();
            }
            catch (Exception e)
            {
                textBox1.Text = e.ToString();
            }
        }
    }
}

Server:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;

namespace ServerLizenz
{
    class Program
    {
        static void Main(string[] args)
        {
            UTF8Encoding bla = new UTF8Encoding();
            //UnicodeEncoding bla = new UnicodeEncoding();


                Int32 port = 14000;
                IPAddress localAddr = IPAddress.Parse("192.168.1.177");

                TcpListener server = new TcpListener(localAddr, port);

                server.Start();
                while (true)
                {
                    Console.Write("Waiting for a connection... ");


                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");

                    NetworkStream stream = client.GetStream();
                    stream.ReadTimeout = System.Threading.Timeout.Infinite;
                    stream.WriteTimeout = System.Threading.Timeout.Infinite;

                    byte[] KeyBuffer = new Byte[2048];
                    stream.Read(KeyBuffer, 0, KeyBuffer.Length);

                    RSACryptoServiceProvider RSAC = new RSACryptoServiceProvider(4096);
                    RSAC.FromXmlString(bla.GetString(KeyBuffer));

                    RijndaelManaged RM = new RijndaelManaged();

                    byte[] RMKeyBuffer = RSAC.Encrypt(RM.Key, true);
                    stream.Write(RMKeyBuffer, 0, RMKeyBuffer.Length);

                    stream.Close();
                }

            Console.ReadKey();
        }
    }
}

Am I doing something terribly wrong or whats the point?

来源:https://stackoverflow.com/questions/18783737/the-data-to-be-decrypted-exceeds-the-maximum-for-this-modulus-of-128-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!