[英]Sending encrypted data via TCP (“Bad Data” exception)
How can i send illegal charecters from tpc client to tcp server.如何将非法字符从 tpc 客户端发送到 tcp 服务器。 This is an example of what the encrypted gibberish looks like:
这是加密乱码的示例:
https://i.stack.imgur.com/wfZdm.png https://i.stack.imgur.com/wfZdm.png
How can i send this pice of gibberish to either my client or server?我如何将这些乱码发送到我的客户端或服务器?
This is my encryption & decryption code这是我的加密和解密代码
public static string Decrypt(string data)
{
byte[] dataToDecrypt = StringToByteArray(data);
byte[] decryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKey);
decryptedData = rsa.Decrypt(dataToDecrypt, false);
}
UnicodeEncoding byteConverter = new UnicodeEncoding();
return ByteArrayToString(decryptedData);
}
public static string Encrypt(string data, string publicKey)
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = StringToByteArray(data);
byte[] encryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
encryptedData = rsa.Encrypt(dataToEncrypt, false);
}
return ByteArrayToString(encryptedData);
}
public static byte[] StringToByteArray(string data)
{
return Encoding.ASCII.GetBytes(data);
}
public static string ByteArrayToString(byte[] bytes)
{
return Encoding.ASCII.GetString(bytes);
}
I have made it so the client and the server share eachothers public keys but i am getting Exception "Bad data".我已经做到了,所以客户端和服务器共享彼此的公钥,但我收到异常“坏数据”。 One more thing if i send encrypted data from a client to the server which data is 128 bytes the server receives only 78 bytes for example
另一件事是,如果我从客户端向服务器发送加密数据,该数据为 128 字节,则服务器仅接收 78 字节
There's a few things wrong with your code:您的代码有一些问题:
String
at all.String
。
String
is meant for text, not arbitrary binary data (I assume you got this impression from C or PHP where their string types are really just synonyms for - or thin-wrappers over - a byte-array). String
用于文本,而不是任意二进制数据(我假设您从 C 或 PHP 得到这种印象,其中它们的字符串类型实际上只是字节数组的同义词或瘦包装器)。Byte[]
buffer you get from rs.Encrypt
and pass that directly to your Socket
, TcpClient
or NetworkStream
that you're using.rs.Encrypt
获得的Byte[]
缓冲区,并将其直接传递给您正在使用的Socket
、 TcpClient
或NetworkStream
。 You'll need to define a binary protocol with length-prefix though. Encoding.ASCII.GetBytes
will convert the UTF-16LE-encoded characters in the String data
instance to 7-bit ASCII, it does this by replacing characters with values above 0x7F
with '?'
Encoding.ASCII.GetBytes
会将String data
实例中的 UTF-16LE 编码字符转换为 7 位 ASCII, 它通过将值高于0x7F
的字符替换为'?'
- this is not what you want! -这不是你想要的! (and this is what's causing the garbage output on your screen: those "illegal characters" are byte-values above
0x7F
that are outside ASCII's 7-bit range. From the documentation: (这就是导致屏幕上出现垃圾 output 的原因:那些“非法字符”是
0x7F
以上的字节值,超出了 ASCII 的 7 位范围。从文档中:
It uses replacement fallback to replace each string that it cannot encode and each byte that it cannot decode with a question mark ("?") character.
它使用替换回退来用问号(“?”)字符替换无法编码的每个字符串和无法解码的每个字节。
If you really do want to transmit data over the network using human-readable text then use Base64 encoding: Convert.ToBase64String( Byte[] buffer )
and convert it back using Convert.FromBase64String( String s )
at the receiving end - but you'll still need to length-prefix or delimit your data.如果您确实想使用人类可读的文本通过网络传输数据,请使用 Base64 编码:
Convert.ToBase64String( Byte[] buffer )
并在接收端使用Convert.FromBase64String( String s )
将其转换回来 - 但你'仍然需要对数据进行长度前缀或分隔。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.