我正在尝试使用 RSA/AES 在客户端和服务器之间建立安全连接(我想你知道它是如何工作的)现在我的问题是,我收到标题中写的错误消息。我知道,有 2 或 3 个其他线程具有相同的标题,但它们都试图从 pc 上的文件中加密和解密。我在使用 RSA 公钥加密的 AES 密钥时遇到该错误,这是我所知道的正常方法。
这是我的“客户端”和“服务器”应用程序
客户:
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();
}
}
}
}
服务器:
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();
}
}
}
我是在做一些非常错误的事情还是有什么意义?