我有一个项目,它的主要目标是通过一个用python编写的服务器在两个用c编写的客户机之间创建一个共享空间。 c#客户端使用完全相同的代码编写,GUI表单通过服务器上的文本消息进行同步(服务器应该知道如何读取这些消息,以确保它们不是“恶意”消息)。 在客户机中,我有一个singleton静态类,它负责与服务器的所有通信。服务器将其发送到另一个客户端。这个静态类在每个窗体中调用一个特定的方法,这个方法处理输入。例如,消息“0040mainForm/Mousemove 320 150”将被调用到名为mainForm的窗体,并将鼠标移动到x=320和y=150。 在服务器中,在客户端之间建立连接之前,每个客户端都需要注册、登录并调用另一个客户端。如果其他客户端应答,会话将启动。你知道吗
我成功地实现了所有这些代码,然后我的讲师告诉我,我必须加密所有在web上发送的消息。我一开始尝试在服务器和客户机之间使用RSA加密,但是解密方法没有成功地加密加密的文本。稍后我尝试使用diffiehellman,然后使用它来传输AES密钥,以便在客户端和服务器之间建立对称加密,但它不会太起作用。 最后,我决定只加密从第一个客户机到第二个客户机的连接(会话开始前的通信保持简单),我也尝试使用diffiehellman传输对称密钥,但它不会工作太多。你知道吗
你能帮我找到加密这些信息的适当和最简单的方法吗? *在添加任何密码之前,代码运行良好。 **代码中的对称加密方法不起作用。 c中静态类中接收消息的线程-
public static void RecievingMessage()
{
if (srvr != null && openForms != null)
{
bool sent = true;
int i = 0;
int length = 0;
string msg;
string[] msg_array;
if (srvr != null)
while (true)
{
Thread.Sleep(20);
byte[] fullData = new byte[4096];
int bytesRec = srvr.Receive(fullData);
string stringData = Encoding.ASCII.GetString(fullData);
length = int.Parse(string.Join("",stringData.Take(4).ToArray()));
sent = false;
if (talkingTo) // Value true when talking to server and false when to client.
{
byte[] data = fullData.Skip(4).Take(length).ToArray();
msg = Encoding.ASCII.GetString(data);
}
else
{
stringData = string.Join("", stringData.Skip(4).Take(length).ToArray());
byte[] data = Encoding.ASCII.GetBytes(stringData);
msg = SymmetricDecrypt(data, addresseeKey);
}
msg_array = msg.Split(' ');
for (i = 0; i < openForms.Count; i++)
{
if (msg_array[0].Equals(openForms[i].Name))
{
openForms[i].Recieve(msg_array);
sent = true;
}
}
}
}
}`
在客户端(c#)的静态类中发送消息方法-
public static void SendMessage(string sender, string message)
{
if (srvr != null)
{
i ++;
try
{
byte[] data = new byte[4096];
byte[] lengthOfData = new byte[4];
string data2 = sender + " " + message + " ";
lengthOfData = GetDataLength(data2.Length);
if (talkingTo)
{
srvr.Send(CombineByteArrays(new byte[][] { lengthOfData, Encoding.ASCII.GetBytes(data2) }));
}
else
{
byte[] datatosend = SymmetricEncrypt(data2,addresseeKey);
srvr.Send(CombineByteArrays(new byte[][] { GetDataLength(datatosend.Length), datatosend }));
}
}
catch (SocketException e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
加密方法-AES我想,(给定密钥)-
public static byte[] SymmetricEncrypt(string plaintext, byte[] key)
{
using (Rijndael desObj = Rijndael.Create())
{
desObj.Key = key;
desObj.Mode = CipherMode.CFB;
desObj.Padding = PaddingMode.PKCS7;
using (MemoryStream ms = new MemoryStream())
{
//Append the random IV that was generated to the front of the stream.
ms.Write(desObj.IV, 0, desObj.IV.Length);
//Write the bytes to be encrypted.
using (CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] plainTextBytes = Encoding.ASCII.GetBytes(plaintext);
cs.Write(plainTextBytes, 0, plainTextBytes.Length);
}
return ms.ToArray();
}
}
}
解密方法-我想也是AES(也给了一个密钥)
public static string SymmetricDecrypt(byte[] cyphertext, byte[] key)
{
using (MemoryStream ms = new MemoryStream(cyphertext))
using (Rijndael desObj = Rijndael.Create())
{
desObj.Key = key;
desObj.Mode = CipherMode.CFB;
desObj.Padding = PaddingMode.PKCS7;
//Read the IV from the front of the stream and assign it to our object.
byte[] iv = new byte[16];
int offset = 0;
while (offset < iv.Length)
{
offset += ms.Read(iv, offset, iv.Length - offset);
}
desObj.IV = iv;
//Read the bytes to be decrypted
using (CryptoStream cs = new CryptoStream(ms, desObj.CreateDecryptor(), CryptoStreamMode.Read))
using (StreamReader sr = new StreamReader(cs, Encoding.ASCII))
{
return sr.ReadToEnd();
}
}
}
只有在客户端之间的消息加密部分,在我放弃对python服务器的加密之后,我才开始使用这两个函数(在其他尝试中,我甚至没有成功地生成密钥并传输它)。它首先显示“要加密的数据长度无效”,然后显示“填充无效且无法删除”,然后尝试将填充设置为“无”,然后返回“要加密的数据长度无效”。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐