什么是Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned的.NET System.Numerics.BigInteger等效项? [英] What is the .NET System.Numerics.BigInteger Equivalent of Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned?

查看:156
本文介绍了什么是Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned的.NET System.Numerics.BigInteger等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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将 System.Guid 转换为 Org.BouncyCastle.Math.BigInteger .然后将此值保存为字符串,格式为 3A2B847A960F0E4A8D49BD62DDB6EB38 .

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.

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

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.

请注意,在目标应用程序中,我不希望BouncyCastle作为参考,而是希望使用核心.NET库进行转换.但是,由于我在使用核心.NET类进行转换时遇到问题,因此我使用的是BouncyCastle,下面的代码完全符合我的期望:

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:

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)

如您所见,在 Org.BouncyCastle.Math.BigInteger 上有一个 ToByteArrayUnsigned 方法可以实现此目的.如果我在 System.Numerics.BigInteger 上使用 ToByteArray (即使

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.

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

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

由于@ John-Tasler的建议,事实证明这是由于忍耐力造成的.:P

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

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

推荐答案

使用.NET的BigIntegrer时,所得Guid的实际值是多少?

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

这可能是两个实现只是以不同的方式存储字节.疲劳度等

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

看看从每个实现的字节数组返回的结果:

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.

这篇关于什么是Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned的.NET System.Numerics.BigInteger等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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