我正在与RSA合作,在客户端和服务器之间发送一些数据。
在我的客户端,点击按钮事件如下:
private void button1_Click(object sender, EventArgs e)
{
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
byte[] outStreamcrypt = cspserver.Encrypt(outStream, false);
int x = outStreamcrypt.Length;
byte[] sizes = BitConverter.GetBytes(x);
serverStream.Write(sizes, 0, sizes.Length);
serverStream.Write(outStreamcrypt, 0, outStreamcrypt.Length)
serverStream.Flush();
}
将csp作为服务器的公钥(是的,服务器),serverStream是NetworkStream。
问题出现在服务器尝试接收数据并使用他的公钥解密:
function foo() {
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesSize = new byte[4];
networkStream.Read(bytesSize, 0,4);
int sizealeer= BitConverter.ToInt32(bytesSize, 0);
byte[] bytescortos = new byte[sizealeer];
networkStream.Read(bytescortos, 0, bytescortos.Length);
var bytesPlainTextData = Program.csp.Decrypt(bytescortos, false);
string plainTextData = Encoding.Unicode.GetString(bytesPlainTextData);
Console.WriteLine("Desencriptado - " + clNo + " : " + plainTextData);
}
Program.csp是服务器密钥(我用来在客户端加密),当我尝试解密时出现问题,它只是给我10个字节作为解密的结果,它们应该更多
我在服务器代码的最开头创建了我的服务器密钥,如下所示:
public static RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);