将对象转换为字节[]
我正在尝试将检索到的注册表值从 object
转换为 byte[]
。它存储为REG_BINARY
。我尝试将 BinaryFormatter
与 MemoryStream
一起使用。但是,它添加了我不想要的开销信息。当我通过执行函数 Convert.ToBase64String(..)
将字节数组转换为字符串时,我观察到了这一点。我执行这些功能是因为我正在测试注册表中加密密钥的存储和检索。
I am trying to convert a retrieved registry value from object
to byte[]
. It is stored as REG_BINARY
. I tried using BinaryFormatter
with MemoryStream
. However, it adds overhead information that I do not want. I observed this when I then converted the byte array to a string by performing the function Convert.ToBase64String(..)
. I am performing these functions because I am testing the storing and retrieval of an encrypted key in the registry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
恍梦境°2024-10-19 05:44:26
试试这个。如果它已经是 REG_BINARY,您需要做的就是转换它:
static byte[] GetFoo()
{
var obj = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\Software", "foo", null);
//TODO: Write a better exception for when it isn't found
if (obj == null) throw new Exception();
var bytearray = obj as byte[];
//TODO: Write a better exception for when its found but not a REG_BINARY
if (bytearray == null) throw new Exception();
return bytearray;
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果它是 REG_BINARY,那么当您检索它时,它应该已经是一个字节数组...难道您不能将其转换为
byte[]
吗?或者,如果您尚未验证代码中的REG_BINARY,您可能需要使用:
If it's a REG_BINARY then it should already be a byte array when you retrieve it... can't you just cast it to
byte[]
?Alternatively, if you haven't already verified that it's REG_BINARY in the code, you may want to use: