什么是.NET System.Numerics.BigInteger等效的Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned?

时间:2022-01-02 16:48:03

I am currently working with the .NET port of BouncyCastle and I am having some trouble converting a big integer into a System.Guid using the native .NET BigInteger.

我目前正在使用BouncyCastle的.NET端口,我在使用本机.NET BigInteger将大整数转换为System.Guid时遇到了一些麻烦。

For some context, I am using BouncyCastle in one ("source") application to convert a System.Guid to a Org.BouncyCastle.Math.BigInteger. This value is then saved as a string in the format 3A2B847A960F0E4A8D49BD62DDB6EB38.

对于某些上下文,我在一个(“源”)应用程序中使用BouncyCastle将System.Guid转换为Org.BouncyCastle.Math.BigInteger。然后将该值保存为3A2B847A960F0E4A8D49BD62DDB6EB38格式的字符串。

Then, in another ("destination") application, I am taking this saved string value of a BigInteger and am trying to convert it back into a System.Guid.

然后,在另一个(“目标”)应用程序中,我将这个保存的BigInteger字符串值,并尝试将其转换回System.Guid。

Please note that in the destination application I do not want BouncyCastle as a reference and would like to use core .NET libraries for the conversion. However, since I am running into problems converting with core .NET classes, I am using BouncyCastle and the following code does exactly what I would like:

请注意,在目标应用程序中,我不希望BouncyCastle作为参考,并希望使用核心.NET库进行转换。但是,由于我遇到了使用核心.NET类转换的问题,我正在使用BouncyCastle,以下代码完全符合我的要求:

var data = "3A2B847A960F0E4A8D49BD62DDB6EB38";
var integer = new Org.BouncyCastle.Math.BigInteger( data, 16 );
var bytes = integer.ToByteArrayUnsigned();
var guid = new Guid( bytes ); // holds expected value: (7A842B3A-0F96-4A0E-8D49-BD62DDB6EB38)

As you can see, there is a ToByteArrayUnsigned method on the Org.BouncyCastle.Math.BigInteger that makes this work. If I use the ToByteArray on the System.Numerics.BigInteger (even when resizing the array as discussed in this question) it does not work and I get a different System.Guid than expected.

正如您所看到的,Org.BouncyCastle.Math.BigInteger上有一个ToByteArrayUnsigned方法,可以使其工作。如果我在System.Numerics.BigInteger上使用ToByteArray(即使在调整此问题中讨论的数组大小时),它也不起作用,我获得了与预期不同的System.Guid。

So, what is the best way to perform the equivalent to the above operation using native .NET classes?

那么,使用本机.NET类执行与上述操作等效的最佳方法是什么?

Solution

Thanks to @John-Tasler's suggestion, it turns out this was due to endianess... darn you endianess... will your endiness ever end? :P

感谢@ John-Tasler的建议,事实证明这是由于结束...你的结局...你的终结会永远结束吗? :P

var parsed = System.Numerics.BigInteger.Parse( "3A2B847A960F0E4A8D49BD62DDB6EB38", NumberStyles.HexNumber ).ToByteArray(); Array.Resize( ref parsed, 16 ); var guid = new Guid( parsed.Reverse().ToArray() ); // Hoorrrrayyyy!

var parsed = System.Numerics.BigInteger.Parse(“3A2B847A960F0E4A8D49BD62DDB6EB38”,NumberStyles.HexNumber)。ToByteArray(); Array.Resize(ref parsed,16); var guid = new Guid(parsed.Reverse()。ToArray()); // Hoorrrrayyyy!

2 个解决方案

#1


1  

What's the actual value of the resulting Guid when using .NET's BigIntegrer?

使用.NET的BigIntegrer时,生成的Guid的实际价值是多少?

It could be that the two implementations are just storing the bytes differently. Endianess, etc.

可能两个实现只是以不同方式存储字节。 Endianess等

To see what comes back from each implementation's array of bytes:

要查看每个实现的字节数组返回的内容:

var sb = new StringBuilder();

foreach (var b in bytes)
{
    sb.AppendFormat("{0:X2} ", b);
} 
sb.ToString();

It'd be an interesting comparison.

这是一个有趣的比较。

#2


1  

I would avoid BigInteger here entirely. Your data bytes are already in the correct order so you can convert to byte[] directly.

我会完全避免使用BigInteger。您的数据字节已经按正确顺序排列,因此您可以直接转换为byte []。

var data = "3A2B847A960F0E4A8D49BD62DDB6EB38";
var bytes = new byte[16];
for (var i = 0; i < 16; i++)
  bytes[i] = byte.Parse(data.Substring(i * 2, 2), NumberStyles.HexNumber);
var guid = new Guid(bytes); // 7a842b3a-0f96-4a0e-8d49-bd62ddb6eb38

#1


1  

What's the actual value of the resulting Guid when using .NET's BigIntegrer?

使用.NET的BigIntegrer时,生成的Guid的实际价值是多少?

It could be that the two implementations are just storing the bytes differently. Endianess, etc.

可能两个实现只是以不同方式存储字节。 Endianess等

To see what comes back from each implementation's array of bytes:

要查看每个实现的字节数组返回的内容:

var sb = new StringBuilder();

foreach (var b in bytes)
{
    sb.AppendFormat("{0:X2} ", b);
} 
sb.ToString();

It'd be an interesting comparison.

这是一个有趣的比较。

#2


1  

I would avoid BigInteger here entirely. Your data bytes are already in the correct order so you can convert to byte[] directly.

我会完全避免使用BigInteger。您的数据字节已经按正确顺序排列,因此您可以直接转换为byte []。

var data = "3A2B847A960F0E4A8D49BD62DDB6EB38";
var bytes = new byte[16];
for (var i = 0; i < 16; i++)
  bytes[i] = byte.Parse(data.Substring(i * 2, 2), NumberStyles.HexNumber);
var guid = new Guid(bytes); // 7a842b3a-0f96-4a0e-8d49-bd62ddb6eb38