我遇到加密和套接字问题,帮助 [英] I'm having problems with cryptography and sockets, help

查看:52
本文介绍了我遇到加密和套接字问题,帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


我正在写一个程序,我需要通过

网络发送机密数据,所以我决定使用加密,使用

System.Security.Cryptography命名空间。


我正在使用套接字进行网络通信,程序首先

使用非对称密码类进行密钥交换,以获得对称密码

的新密钥。我的问题是,虽然我已经检查了

两点得到相同的密钥和初始化向量,当发送方

发送数据时,它没问题,但是接收器被阻止。我正在使用阻止

套接字,所以我知道如果套接字没有收到数据,它就会阻止它直到它为止。但是我知道它确实收到了数据,并且它仍然会阻止,而不是NetworkStream,而是用于解密

数据的CryptoStream。我一直在尝试很多东西,比如在文档示例中使用StreamReader和

StreamWriter,但是没有用。


我需要帮助,请。已经过了整整2天的尝试,请帮忙。


亚历杭德罗。


以下是一些代码行:


这是接收器代码(它在Read函数中阻塞):


string mensaje;

NetworkStream流= clienteTcp.GetStream();

// Crear elstreamcriptográfico

CryptoStream crStream = new

CryptoStream(stream,TDES.CreateDecryptor(tdesClave) ,tdesIV),

CryptoStreamMode.Read);

//Búferde8Kb

byte [] datos = new byte [8192];

int bytes;

int intentos = 25;

// Detectar si hay datos

while(intentos> 0)

{

if(stream.DataAvailable)

break;

intentos - ;

System.Threading.Thread.Sleep(200);

}

// Si no hay datos salir

if(b) !stream.DataAvailable)

返回String.Empty;

// Leer mensaje

bytes = crStream .Read(datos,0,datos.Length);

// Pasar a string

mensaje = System.Text.Encoding.Unicode.GetString(datos,0,byt es);

这是发件人代码:


NetworkStream stream = new NetworkStream(clienteTcp);

// Crear el streamcriptográfico

CryptoStream crStream = new

CryptoStream(stream,TDES.CreateEncryptor(tdesClave,tdesIV),

CryptoStreamMode.Write);

//转换字符串a una matriz de bytes

byte [] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);

// Transmitir mensaje

crStream.Write(datos,0,datos.Length);

crStream.Flush();

Hi.

I''m writing a program, and I need to send confidential data through the
network, so I decided to use encryption, using the
System.Security.Cryptography namespace.

I''m using the sockets for the network communications, and the program first
does a key exchange, with the asymetric cipher classes, to get a new key for
the symmetric cipher. My problem is, that although I have checked that the
two points get to the same key and initialization vector, when the sender
sends the data, its ok, but the receiver gets blocked. I am using blocking
sockets, so I am aware that if the socket does not have received data, it
blocks until it does. But I know that it does receive the data, and still
it blocks, not the NetworkStream, but the CryptoStream used to decrypt the
data. I have been trying many things, using the StreamReader and
StreamWriter like in the documentation examples, but doesn''t work.

I need help, please. It''s been 2 full days trying, please help.

Alejandro.

Here are some lines of the code:

This is the receiver code (where it blocks, in the Read function):

string mensaje;
NetworkStream stream = clienteTcp.GetStream();
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateDecryptor(tdesClave ,tdesIV),
CryptoStreamMode.Read);
// Búfer de 8Kb
byte[] datos = new byte[8192];
int bytes;
int intentos = 25;
// Detectar si hay datos
while(intentos > 0)
{
if (stream.DataAvailable)
break;
intentos--;
System.Threading.Thread.Sleep(200);
}
// Si no hay datos salir
if (!stream.DataAvailable)
return String.Empty;
// Leer mensaje
bytes = crStream.Read(datos,0,datos.Length);
// Pasar a string
mensaje = System.Text.Encoding.Unicode.GetString(datos,0,byt es);
This is the sender code:

NetworkStream stream = new NetworkStream(clienteTcp);
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateEncryptor(tdesClave ,tdesIV),
CryptoStreamMode.Write);
// Convertir el string a una matriz de bytes
byte[] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
// Transmitir mensaje
crStream.Write(datos,0,datos.Length);
crStream.Flush();

推荐答案

亚历杭德罗,


看起来你正试图读取太多数据。您正在对套接字类的Read方法发出

调用,传递长度为8192

字节。如果发送方没有发送8192个字节,则对Read的调用将转到

block,直到它获得8192个字节。


有两种处理方式。第一个是标记您的消息

,其标识符表示消息已完成

传输。这需要你逐字节读取,这不是一件好事。


第二个是实际发送消息的长度在你自己发送消息之前发送消息。通过这种方式,您可以调用Read方法,并且

知道您不需要比线路上更多的数据。


希望这有助于。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Alejandro Casta?aza" <哒***************** @ itelgua.com>在消息中写道

news:%2 **************** @ tk2msftngp13.phx.gbl ...
Alejandro,

It looks like you are trying to read too much data. You are issuing a
call to the Read method on the socket class passing in a length of 8192
bytes. If the sender does not send 8192 bytes, the call to Read is going to
block until it gets 8192 bytes.

There are two ways of handling this. The first is to mark your message
with an identifier which indicates that the message has completed
transmission. This would require you to read byte-by-byte, which is not a
good thing.

The second is to actually send the length of the message before you send
the message yourself. This way, you can issue a call to the Read method and
know you are not asking for more data than is on the line.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alejandro Casta?aza" <da*****************@itelgua.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
嗨。

我正在写一个程序,我需要通过
网络发送机密数据,所以我决定使用加密,使用
System.Security.Cryptography名称空间。

我正在使用套接字进行网络通信,程序首先使用不对称的密码类进行密钥交换,以获得新的密钥。对称密码。我的问题是,虽然我已经检查过两点得到相同的密钥和初始化向量,但是当发送方发送数据时,它没问题,但接收方被阻止了。我使用阻塞套接字,所以我知道如果套接字没有收到数据,它会阻塞直到它。但我知道它确实收到了数据,但它仍然阻止,而不是NetworkStream,而是用于解密数据的CryptoStream。我一直在尝试很多东西,使用文档示例中的StreamReader和StreamWriter,但
不起作用。

我需要帮助。已经整整2天了,请帮忙。

亚历杭德罗。

这里有一些代码:

这是接收器代码(在其中阻塞,在Read函数中):

string mensaje;
NetworkStream stream = clienteTcp.GetStream();
// Crear elstreamcritptográfico CryptoStream crStream = new
CryptoStream(stream,TDES.CreateDecryptor(tdesClave,tdesIV),

CryptoStreamMode.Read);
//Búferde8Kb
byte [ ] datos = new byte [8192];
int bytes;
int intentos = 25;
// Detectar si hay datos
while(intentos> 0)
{
if(stream.DataAvailable)
break;
intentos--;
System.Threading.Thread.Sleep(200);
}
/ / Si no hay datos salir
if(!stream.DataAvailable)
返回String.Empty;
// Leer mensaje
bytes = crStream.Read(datos,0,datos。长度);
// Pasar a string
mensaje = System.Text.Encoding.Unicode.GetString( datos,0,byt es);

这是发件人代码:

NetworkStream stream = new NetworkStream(clienteTcp);
// Crear elstreamcriptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateEncryptor(tdesClave,tdesIV),
CryptoStreamMode.Write);
//转换字符串a una matriz de bytes
byte [] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
// Transmitir mensaje
crStream.Write(datos,0,datos.Length);
crStream。 Flush();

Hi.

I''m writing a program, and I need to send confidential data through the
network, so I decided to use encryption, using the
System.Security.Cryptography namespace.

I''m using the sockets for the network communications, and the program
first does a key exchange, with the asymetric cipher classes, to get a new
key for the symmetric cipher. My problem is, that although I have checked
that the two points get to the same key and initialization vector, when
the sender sends the data, its ok, but the receiver gets blocked. I am
using blocking sockets, so I am aware that if the socket does not have
received data, it blocks until it does. But I know that it does receive
the data, and still it blocks, not the NetworkStream, but the CryptoStream
used to decrypt the data. I have been trying many things, using the
StreamReader and StreamWriter like in the documentation examples, but
doesn''t work.

I need help, please. It''s been 2 full days trying, please help.

Alejandro.

Here are some lines of the code:

This is the receiver code (where it blocks, in the Read function):

string mensaje;
NetworkStream stream = clienteTcp.GetStream();
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateDecryptor(tdesClave ,tdesIV),

CryptoStreamMode.Read);
// Búfer de 8Kb
byte[] datos = new byte[8192];
int bytes;
int intentos = 25;
// Detectar si hay datos
while(intentos > 0)
{
if (stream.DataAvailable)
break;
intentos--;
System.Threading.Thread.Sleep(200);
}
// Si no hay datos salir
if (!stream.DataAvailable)
return String.Empty;
// Leer mensaje
bytes = crStream.Read(datos,0,datos.Length);
// Pasar a string
mensaje = System.Text.Encoding.Unicode.GetString(datos,0,byt es);
This is the sender code:

NetworkStream stream = new NetworkStream(clienteTcp);
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateEncryptor(tdesClave ,tdesIV),
CryptoStreamMode.Write);
// Convertir el string a una matriz de bytes
byte[] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
// Transmitir mensaje
crStream.Write(datos,0,datos.Length);
crStream.Flush();



谢谢Nicholas。

我写的接收程序用于接收普通数据几乎相同,只有它调用了NetworkStream对象中的Read而不是

CryptoStream,它的长度相同,为8192字节,并且它没有''' t block,和

返回实际读取的字节数。


CryptoStream在这个问题上与NetworkStream不同吗?

&quo t; Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com> escribió

en el mensaje新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Thank you Nicholas.
The receiver routine I wrote for receiving plain data is almost the same,
only that it calls Read in the NetworkStream object instead of the
CryptoStream, with the same length of 8192 bytes, and it doesn''t block, and
returns the number of bytes actually read.

Is the CryptoStream different to the NetworkStream in this matter?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> escribió
en el mensaje news:%2****************@TK2MSFTNGP10.phx.gbl...
亚历杭德罗,

看起来你正试图读取太多数据。您正在对套接字类的Read方法发出
调用,传递长度为8192
字节。如果发送方没有发送8192个字节,则对Read的调用将被阻塞,直到它获得8192个字节。

有两种方法可以处理它。第一种是用标识符标记您的消息
,该标识符表示消息已完成传输。这将需要你逐字节读取,这不是一件好事。

第二个是在你发送之前实际发送消息的长度给自己留言。通过这种方式,您可以调用Read
方法,并且知道您不需要比线路上更多的数据。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse。 com

Alejandro Casta?aza <哒***************** @ itelgua.com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Alejandro,

It looks like you are trying to read too much data. You are issuing a
call to the Read method on the socket class passing in a length of 8192
bytes. If the sender does not send 8192 bytes, the call to Read is going
to block until it gets 8192 bytes.

There are two ways of handling this. The first is to mark your message
with an identifier which indicates that the message has completed
transmission. This would require you to read byte-by-byte, which is not a
good thing.

The second is to actually send the length of the message before you
send the message yourself. This way, you can issue a call to the Read
method and know you are not asking for more data than is on the line.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alejandro Casta?aza" <da*****************@itelgua.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
嗨。

我正在编写一个程序,我需要通过
网络发送机密数据,所以我决定使用加密,使用
System.Security.Cryptography命名空间。

我正在使用套接字进行网络通信,程序首先使用非对称密码类进行密钥交换,以获得对称密码的新密钥。我的问题是,虽然我已经检查过这两个点得到了相同的密钥和初始化
向量,当发送方发送数据时,它没问题,但接收方被阻止了。我正在使用阻塞套接字,所以我知道如果套接字
没有收到数据,它会阻塞直到它。但我知道它确实收到了数据,但它仍然阻止,而不是NetworkStream,而是用于解密数据的CryptoStream。我一直在尝试很多事情,使用StreamReader和StreamWriter就像文档中的示例一样,但是没有用。

我需要帮助。已经整整2天了,请帮忙。

亚历杭德罗。

这里有一些代码:

这是接收器代码(在其中阻塞,在Read函数中):

string mensaje;
NetworkStream stream = clienteTcp.GetStream();
// Crear elstreamcritptográfico CryptoStream crStream = new
CryptoStream(stream,TDES.CreateDecryptor(tdesClave,tdesIV),

CryptoStreamMode.Read);
//Búferde8Kb
byte [ ] datos = new byte [8192];
int bytes;
int intentos = 25;
// Detectar si hay datos
while(intentos> 0)
{
if(stream.DataAvailable)
break;
intentos--;
System.Threading.Thread.Sleep(200);
}
/ / Si no hay datos salir
if(!stream.DataAvailable)
返回String.Empty;
// Leer mensaje
bytes = crStream.Read(datos,0,datos。长度);
// Pasar a string
mensaje = System.Text.Encoding.Unicode.GetString( datos,0,byt es);

这是发件人代码:

NetworkStream stream = new NetworkStream(clienteTcp);
// Crear elstreamcriptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateEncryptor(tdesClave,tdesIV),
CryptoStreamMode.Write);
//转换字符串a una matriz de bytes
byte [] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
// Transmitir mensaje
crStream.Write(datos,0,datos.Length);
crStream。同花顺();

Hi.

I''m writing a program, and I need to send confidential data through the
network, so I decided to use encryption, using the
System.Security.Cryptography namespace.

I''m using the sockets for the network communications, and the program
first does a key exchange, with the asymetric cipher classes, to get a
new key for the symmetric cipher. My problem is, that although I have
checked that the two points get to the same key and initialization
vector, when the sender sends the data, its ok, but the receiver gets
blocked. I am using blocking sockets, so I am aware that if the socket
does not have received data, it blocks until it does. But I know that it
does receive the data, and still it blocks, not the NetworkStream, but
the CryptoStream used to decrypt the data. I have been trying many
things, using the StreamReader and StreamWriter like in the documentation
examples, but doesn''t work.

I need help, please. It''s been 2 full days trying, please help.

Alejandro.

Here are some lines of the code:

This is the receiver code (where it blocks, in the Read function):

string mensaje;
NetworkStream stream = clienteTcp.GetStream();
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateDecryptor(tdesClave ,tdesIV),

CryptoStreamMode.Read);
// Búfer de 8Kb
byte[] datos = new byte[8192];
int bytes;
int intentos = 25;
// Detectar si hay datos
while(intentos > 0)
{
if (stream.DataAvailable)
break;
intentos--;
System.Threading.Thread.Sleep(200);
}
// Si no hay datos salir
if (!stream.DataAvailable)
return String.Empty;
// Leer mensaje
bytes = crStream.Read(datos,0,datos.Length);
// Pasar a string
mensaje = System.Text.Encoding.Unicode.GetString(datos,0,byt es);
This is the sender code:

NetworkStream stream = new NetworkStream(clienteTcp);
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateEncryptor(tdesClave ,tdesIV),
CryptoStreamMode.Write);
// Convertir el string a una matriz de bytes
byte[] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
// Transmitir mensaje
crStream.Write(datos,0,datos.Length);
crStream.Flush();




顺便说一句......

是否有将int转换为字节数组的方法?


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com> escribió

en el mensaje新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
By the way...
Is there a method to convert an int to an array of bytes?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> escribió
en el mensaje news:%2****************@TK2MSFTNGP10.phx.gbl...
亚历杭德罗,

看起来你正试图读取太多数据。您正在对套接字类的Read方法发出
调用,传递长度为8192
字节。如果发送方没有发送8192个字节,则对Read的调用将被阻塞,直到它获得8192个字节。

有两种方法可以处理它。第一种是用标识符标记您的消息
,该标识符表示消息已完成传输。这将需要你逐字节读取,这不是一件好事。

第二个是在你发送之前实际发送消息的长度给自己留言。通过这种方式,您可以调用Read
方法,并且知道您不需要比线路上更多的数据。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse。 com

Alejandro Casta?aza <哒***************** @ itelgua.com>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Alejandro,

It looks like you are trying to read too much data. You are issuing a
call to the Read method on the socket class passing in a length of 8192
bytes. If the sender does not send 8192 bytes, the call to Read is going
to block until it gets 8192 bytes.

There are two ways of handling this. The first is to mark your message
with an identifier which indicates that the message has completed
transmission. This would require you to read byte-by-byte, which is not a
good thing.

The second is to actually send the length of the message before you
send the message yourself. This way, you can issue a call to the Read
method and know you are not asking for more data than is on the line.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alejandro Casta?aza" <da*****************@itelgua.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
嗨。

我正在编写一个程序,我需要通过
网络发送机密数据,所以我决定使用加密,使用
System.Security.Cryptography命名空间。

我正在使用套接字进行网络通信,程序首先使用非对称密码类进行密钥交换,以获得对称密码的新密钥。我的问题是,虽然我已经检查过这两个点得到了相同的密钥和初始化
向量,当发送方发送数据时,它没问题,但接收方被阻止了。我正在使用阻塞套接字,所以我知道如果套接字
没有收到数据,它会阻塞直到它。但我知道它确实收到了数据,但它仍然阻止,而不是NetworkStream,而是用于解密数据的CryptoStream。我一直在尝试很多事情,使用StreamReader和StreamWriter就像文档中的示例一样,但是没有用。

我需要帮助。已经整整2天了,请帮忙。

亚历杭德罗。

这里有一些代码:

这是接收器代码(在其中阻塞,在Read函数中):

string mensaje;
NetworkStream stream = clienteTcp.GetStream();
// Crear elstreamcritptográfico CryptoStream crStream = new
CryptoStream(stream,TDES.CreateDecryptor(tdesClave,tdesIV),

CryptoStreamMode.Read);
//Búferde8Kb
byte [ ] datos = new byte [8192];
int bytes;
int intentos = 25;
// Detectar si hay datos
while(intentos> 0)
{
if(stream.DataAvailable)
break;
intentos--;
System.Threading.Thread.Sleep(200);
}
/ / Si no hay datos salir
if(!stream.DataAvailable)
返回String.Empty;
// Leer mensaje
bytes = crStream.Read(datos,0,datos。长度);
// Pasar a string
mensaje = System.Text.Encoding.Unicode.GetString( datos,0,byt es);

这是发件人代码:

NetworkStream stream = new NetworkStream(clienteTcp);
// Crear elstreamcriptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateEncryptor(tdesClave,tdesIV),
CryptoStreamMode.Write);
//转换字符串a una matriz de bytes
byte [] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
// Transmitir mensaje
crStream.Write(datos,0,datos.Length);
crStream。同花顺();

Hi.

I''m writing a program, and I need to send confidential data through the
network, so I decided to use encryption, using the
System.Security.Cryptography namespace.

I''m using the sockets for the network communications, and the program
first does a key exchange, with the asymetric cipher classes, to get a
new key for the symmetric cipher. My problem is, that although I have
checked that the two points get to the same key and initialization
vector, when the sender sends the data, its ok, but the receiver gets
blocked. I am using blocking sockets, so I am aware that if the socket
does not have received data, it blocks until it does. But I know that it
does receive the data, and still it blocks, not the NetworkStream, but
the CryptoStream used to decrypt the data. I have been trying many
things, using the StreamReader and StreamWriter like in the documentation
examples, but doesn''t work.

I need help, please. It''s been 2 full days trying, please help.

Alejandro.

Here are some lines of the code:

This is the receiver code (where it blocks, in the Read function):

string mensaje;
NetworkStream stream = clienteTcp.GetStream();
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateDecryptor(tdesClave ,tdesIV),

CryptoStreamMode.Read);
// Búfer de 8Kb
byte[] datos = new byte[8192];
int bytes;
int intentos = 25;
// Detectar si hay datos
while(intentos > 0)
{
if (stream.DataAvailable)
break;
intentos--;
System.Threading.Thread.Sleep(200);
}
// Si no hay datos salir
if (!stream.DataAvailable)
return String.Empty;
// Leer mensaje
bytes = crStream.Read(datos,0,datos.Length);
// Pasar a string
mensaje = System.Text.Encoding.Unicode.GetString(datos,0,byt es);
This is the sender code:

NetworkStream stream = new NetworkStream(clienteTcp);
// Crear el stream criptográfico
CryptoStream crStream = new
CryptoStream(stream,TDES.CreateEncryptor(tdesClave ,tdesIV),
CryptoStreamMode.Write);
// Convertir el string a una matriz de bytes
byte[] datos = System.Text.Encoding.Unicode.GetBytes(mensaje);
// Transmitir mensaje
crStream.Write(datos,0,datos.Length);
crStream.Flush();




这篇关于我遇到加密和套接字问题,帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆