c#序列化ascii混淆
这是密码c#序列化ascii混淆,c#,.net,serialization,string,ascii,C#,.net,Serialization,String,Ascii,这是密码 [Serializable] public class HostedGame { public int ID { get; set; } public int UID { get; set; } public String Name { get; set; } public Boolean Available { get; set; } public String Description { get; set; } public
[Serializable]
public class HostedGame
{
public int ID { get; set; }
public int UID { get; set; }
public String Name { get; set; }
public Boolean Available { get; set; }
public String Description { get; set; }
public List<int> Users { get; set; }
public int Port { get; set; }
public HostedGame(int uid, String name, String description, int port)
{
UID = uid;
Name = name;
Description = description;
Available = true;
Port = port;
Users = new List<int>();
}
public int CompareTo(Object obj)
{
int result = 1;
if(obj != null && obj is HostedGame)
{
HostedGame w = obj as HostedGame;
result = this.ID.CompareTo(w.ID);
}
return result;
}
static public int Compare(HostedGame x, HostedGame y)
{
int result = 1;
if(x != null && y != null)
{
result = x.CompareTo(y);
}
return result;
}
public static HostedGame DeSerialize(byte[] data)
{
MemoryStream ms = new MemoryStream(data);
BinaryFormatter bff = new BinaryFormatter();
return (HostedGame)bff.Deserialize(ms);
}
public static byte[] Serialize(HostedGame obj)
{
BinaryFormatter bff = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bff.Serialize(ms, obj);
return ms.ToArray();
}
}
HACK.Port
出于某种原因被证明是7999
当我这么做的时候
HostedGame HACK = HostedGame.DeSerialize(HostedGame.Serialize(hs));
它很好用
所以,我要问的是
不能使用
Encoding.ASCII.GetString
将任何字节数组转换为字符串。这样做会丢失一些数据。改用Convert.ToBase64String
。这个将从任何字节序列生成一个字符串,而不会丢失数据
HostedGame hs = new HostedGame(12,"Name", "Description", 8088);
String s = Convert.ToBase64String(HostedGame.Serialize(hs));
HostedGame HACK= HostedGame.DeSerialize(Convert.FromBase64String(s));
var testBytes = new byte[] { 250, 251, 252 };
var text = Encoding.ASCII.GetString(testBytes);
var bytes = Encoding.ASCII.GetBytes(result); // will be 63, 63, 63
下面是一个示例,它显示了如何使用编码.ASCII
丢失数据
HostedGame hs = new HostedGame(12,"Name", "Description", 8088);
String s = Convert.ToBase64String(HostedGame.Serialize(hs));
HostedGame HACK= HostedGame.DeSerialize(Convert.FromBase64String(s));
var testBytes = new byte[] { 250, 251, 252 };
var text = Encoding.ASCII.GetString(testBytes);
var bytes = Encoding.ASCII.GetBytes(result); // will be 63, 63, 63
二进制序列化生成一个字节数组,该数组在任何编码中(不一定)都是有效字符串 当您尝试将其作为ASCII文本读取时,ASCII解码器会将任何无效字节(>128)转换为
?
字符。因此,当您将其转换回ASCII字节时,最终会得到一组不同的字节 简而言之,不要将二进制数据视为ASCII或任何其他文本编码
如果您需要以纯文本形式发送二进制数据,请使用Base64将其安全地转换为文本。请解释。是什么让它成为一个更好的选择?它不是一个更好的选择。这是一个可行的选择。你的不是,因此它隐含地更好:)