- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 .Net 项目中使用 BigInteger ( link ) 的单声道实现在 Java 中我使用 java.math.BigInteger。
相同的代码在 Java 中产生不同的结果。
.Net代码
String inputBytes = "8E5BD77F0DCC30864634C134E28BFB42A149675A320786B616F4530708350D270353C30A40450325801B7AFED12BCCA274B8187072A89CC0CC3F95A24A8251243C1835898246F4D64CA3AC61DB841518F0E8FBC8996A40EB626153AE7F0BB87FD713FAC522719431428DE178E780A3FA45788A72C431926AED990E6DA268D2CC";
String modulus = "00e6b4b4511e0bd1b3d9c82ee189ba6d0c70b1466d94126f99a741af99a92701a789451742a357ddb61a4dea409965ec58dcaa5e30826de871b04700ed0fd46b1693446049734e8f95faba2bf9301347e63ba1771650e71982adef0cca6890b6f7baa7f5421a6533652f4b70c3c4270c480cf54cc06635f22901a42716d1dadf4f";
String exp = "010001";
BigInteger mModulus = new BigInteger(hexStringToByteArray(modulus));
BigInteger mExponent = new BigInteger(hexStringToByteArray(exp));
BigInteger input = new BigInteger(hexStringToByteArray(inputBytes));
BigInteger output = input.ModPow(mExponent, mModulus);
Console.WriteLine("==RESULT==" + byteArray2Hex(output.GetBytes()));
public static byte[] hexStringToByteArray(string hexString)
{
if (hexString.Length % 2 != 0)
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
byte[] HexAsBytes = new byte[hexString.Length / 2];
for (int index = 0; index < HexAsBytes.Length; index++)
{
string byteValue = hexString.Substring(index * 2, 2);
HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return HexAsBytes;
}
==RESULT==01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003020300C06082A864886F70D02050500041009EB0D996BFC1EFA5675997712A1AB6E
Java 代码。相同的 inputBytes 数组,相同的指数和模数,但结果不同。
String inputBytes = "8E5BD77F0DCC30864634C134E28BFB42A149675A320786B616F4530708350D270353C30A40450325801B7AFED12BCCA274B8187072A89CC0CC3F95A24A8251243C1835898246F4D64CA3AC61DB841518F0E8FBC8996A40EB626153AE7F0BB87FD713FAC522719431428DE178E780A3FA45788A72C431926AED990E6DA268D2CC";
String modulus = "00e6b4b4511e0bd1b3d9c82ee189ba6d0c70b1466d94126f99a741af99a92701a789451742a357ddb61a4dea409965ec58dcaa5e30826de871b04700ed0fd46b1693446049734e8f95faba2bf9301347e63ba1771650e71982adef0cca6890b6f7baa7f5421a6533652f4b70c3c4270c480cf54cc06635f22901a42716d1dadf4f";
String exp = "010001";
BigInteger mModulus = new BigInteger(hexStringToByteArray(modulus));
BigInteger mExponent = new BigInteger(hexStringToByteArray(exp));
BigInteger input = new BigInteger(hexStringToByteArray(inputBytes));
BigInteger output = input.modPow(mExponent, mModulus);
System.out.println("==RESULT==" + Utils.byteArray2Hex(output.getBytes()));
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
==RESULT==6ce02bd9536ad76bcfd7633b6a2305ed98b43b0bb5fc2acbf984566f1ab35db02e651e9ed8793bf64b018455872b8ae3a06af082e8d680df407ea1e5df1336a19c6f3e116c6ff1940066396afa1de5633fad814fb42790b3af0e62e6dd53977f78794b2d105cdca9272f9c0feea119fe2c9691b6f6e21db3065fb25d840acea2
我不明白为什么结果不同。
附言例如如果我使用 InputBytes
String inputBytes = "242e35241b85fcfd75a53441ef9fc0941064c16f8e4555dabef5ce8ebc91400c6961b6b607e5dd762dbcabce51b11c8594e7d7183786c8e3c5300c7583c1871fc6f350b817682150b5cd0430ca9a2c3f8315b425c8fea0e7cc18187237ed47d29b082e7e7154888d5fb09f092a6dd5e2d3dac9df8de45837b708b5ae17f03e7f";
Java 和 .Net 的结果是一样的
==RESULT==01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff003020300c06082a864886f70d02050500041046fd8e86a4833e7141cbe4718e8e92f7
神奇在哪里?
最佳答案
来自 java.math.BigInteger(byte[])
的文档:
Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.
来自 System.Numerics.BigInteger(byte[])
的文档:
The individual bytes in the value array should be in little-endian order, from lowest-order byte to highest-order byte.
所以您可能只想尝试反转您所获得的值之一的输入字节 - 不清楚哪个您应该反转设置,因为我们不知道您的值是什么'重新尝试代表。我建议添加诊断,在每种情况下构造后立即打印出正常的十进制表示 - 如果它们不相同,则其余代码无关紧要。
关于Java BigInteger 与 Mono .net BigInteger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10119032/
Mono 适合开发服务器应用程序,还是只适合开发桌面应用程序?我想用 C# 为 Linux 开发服务器应用程序。我想用 C#/XNA 编写一个第一人称射击 (FPS) 游戏,并且我有一个 Linux
今天我的 Ubuntu 将 Mono 更新到了 4.2.1.102。它不会允许我绝对需要运行的某个程序。如何将其降级到 4.0.5.1?我已经尝试过了... sudo apt-get install
我最近一直在使用 Java 中的 react 器库和 Spring 框架学习响应式(Reactive)编程,并且在很大程度上我已经能够掌握它。然而,我发现自己多次遇到同样的情况,并希望得到一些关于我哪
虽然 Mono 支持对我们来说不是什么大问题,但我认为 OpenRasta 支持它,因为它有一些关于它的提交消息.. 好吧,我尝试在 Mono 上构建它并获得了模棱两可的类型引用(在手动创建了 10
如何使用单声道嵌入调用创建通用 List 对象?我可以得到 List 的 MonoClass: MonoClass* list = mono_class_from_name(mscorlibimage
我正在考虑使用 Mono.Cairo 作为轻量级 CAD 系统的基础。 但不知道表现如何。 CAD 系统产生了很多 重绘并且可以在其中包含大量数据和大量文本。 如果不是开罗,那么欢迎任何其他建议。 我
我花了一周的时间尝试让我的 XSP 服务器处理简单的静态内容:html 页面、js 文件、gif、jpegs 等...没有 ASP.NET。当使用浏览器浏览此页面时,该服务器总是随机崩溃。我的环境是:
我想确定构建和安装当前 Mono 运行时的版本(如何在 Git 中正确调用它?)。 $ dmcs --version Mono C# compiler version 2.9.0.0 但这绝对不够。
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10 年前关闭。 Improve this
我已经在 ubuntu 14.0 lts 中下载并安装了 vscode,并包含了现有的 node.js 项目。首先认为我必须使用 vscode 调试我的应用程序,为此我必须调试(ctrl+shift+
嗨,我刚刚开始学习响应式编程 我这里有这段代码,我的流程应该是我将调用 tokenRepository 来获取 token ,然后使用 token.getAccessToken() 用作 cardRe
几天来,我一直在尝试在 Centos 6.3 上运行的 XSP 2.10 软件包中获取 mono 3.0 和 nginx 1.2.4 和 fastcgi-mono-serverX ...XSP4 服务
我正在尝试使用 Mono 创建一个 Mac 包。当我执行时: mkbundle file.exe --deps -o FILE 我在编译过程中得到了这个: fatal error: "
Mono 2.0 was just officially released .您认为最重要的单一功能是什么? 最佳答案 Windows.Forms 绝对领先...这可能是我最兴奋的功能。 LINQ-t
我正在将我的应用程序更新到 ios6,但我遇到了以下问题 无法通过架构构建应用程序 支持 ARMv6 + ARM v7 但仅支持 ARM v7(错误是 iOS6 与 ARM v6 不兼容)。这意味着我
你好,我有 CentOS,我正在尽我最大的努力更新 Mono,我目前有 1.2.4 版,我试图通过 xbuild 编译一些东西,但我想它不起作用,因为我正在使用旧版本的单声道。 请在将任何指南链接到我
我正在尝试使用以下代码创建包含名称(作为标签)和关闭按钮(作为带有图像的按钮)的新 GTK Notebook 选项卡: Label headerLabel = new Label(); headerL
我正在为需要使用 AppleScript 的 OSX 编写一个单声道应用程序。我正在使用 AppleScript class from the Monodevelop source大多数情况下都可以正
我正在尝试构建一个控制台应用程序来测试 redis/mono 通信。我一直在使用 Monodevelop 4.0 (Xamarin Studios)+Nuget Port 在 mac os 上与 Se
为了摆脱软 float 与硬 float ABI 问题,我尝试在我的 Raspberry Pi 上安装最新版本的单声道 git clone https://github.com/mono/mono.g
我是一名优秀的程序员,十分优秀!