本文介绍了Java的BigInteger的VS单.NET的BigInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在.NET项目单实施BigInteger的使用(链接)在Java中我用java.math.BigInteger中。
同样的code产生在Java中不同的结果。
净code
字符串inputBytes = "8E5BD77F0DCC30864634C134E28BFB42A149675A320786B616F4530708350D270353C30A40450325801B7AFED12BCCA274B8187072A89CC0CC3F95A24A8251243C1835898246F4D64CA3AC61DB841518F0E8FBC8996A40EB626153AE7F0BB87FD713FAC522719431428DE178E780A3FA45788A72C431926AED990E6DA268D2CC";
串模数= "00e6b4b4511e0bd1b3d9c82ee189ba6d0c70b1466d94126f99a741af99a92701a789451742a357ddb61a4dea409965ec58dcaa5e30826de871b04700ed0fd46b1693446049734e8f95faba2bf9301347e63ba1771650e71982adef0cca6890b6f7baa7f5421a6533652f4b70c3c4270c480cf54cc06635f22901a42716d1dadf4f";
字符串EXP =010001;
BigInteger的mModulus =新的BigInteger(hexStringToByteArray(模数));
BigInteger的mExponent =新的BigInteger(hexStringToByteArray(EXP));
BigInteger的输入=新的BigInteger(hexStringToByteArray(inputBytes));
BigInteger的输出= input.ModPow(mExponent,mModulus);
Console.WriteLine(==的结果为+ byteArray2Hex(output.GetBytes()));
公共静态的byte [] hexStringToByteArray(字符串十六进制串)
{
如果(hexString.Length%2!= 0)
抛出新的ArgumentException(的String.Format(CultureInfo.InvariantCulture,二进制钥匙不能有奇数个数字:{0},十六进制串));
byte []的HexAsBytes =新的字节[hexString.Length / 2];
对于(INT指数= 0;指数< HexAsBytes.Length;指数++)
{
字符串=的byteValue hexString.Substring(指数* 2,2);
HexAsBytes [指数] = byte.Parse(的byteValue,NumberStyles.HexNumber,CultureInfo.InvariantCulture);
}
返回HexAsBytes;
}
==RESULT==01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003020300C06082A864886F70D02050500041009EB0D996BFC1EFA5675997712A1AB6E
Java的code。同一inputBytes阵列,相同的指数和弹性模量,但不同的结果。
字符串inputBytes = "8E5BD77F0DCC30864634C134E28BFB42A149675A320786B616F4530708350D270353C30A40450325801B7AFED12BCCA274B8187072A89CC0CC3F95A24A8251243C1835898246F4D64CA3AC61DB841518F0E8FBC8996A40EB626153AE7F0BB87FD713FAC522719431428DE178E780A3FA45788A72C431926AED990E6DA268D2CC";
串模数= "00e6b4b4511e0bd1b3d9c82ee189ba6d0c70b1466d94126f99a741af99a92701a789451742a357ddb61a4dea409965ec58dcaa5e30826de871b04700ed0fd46b1693446049734e8f95faba2bf9301347e63ba1771650e71982adef0cca6890b6f7baa7f5421a6533652f4b70c3c4270c480cf54cc06635f22901a42716d1dadf4f";
字符串EXP =010001;
BigInteger的mModulus =新的BigInteger(hexStringToByteArray(模数));
BigInteger的mExponent =新的BigInteger(hexStringToByteArray(EXP));
BigInteger的输入=新的BigInteger(hexStringToByteArray(inputBytes));
BigInteger的输出= input.modPow(mExponent,mModulus);
的System.out.println(==的结果为+ Utils.byteArray2Hex(output.getBytes()));
公共静态的byte [] hexStringToByteArray(String s)将{
INT的len = s.length();
byte []的数据=新的字节[长度/ 2];
的for(int i = 0; I< LEN;我+ = 2){
数据[I / 2] =(字节)((Character.digit将(s.charAt(ⅰ),16)其中; 4;)
+ Character.digit将(s.charAt第(i + 1),16));
}
返回的数据;
}
==RESULT==6ce02bd9536ad76bcfd7633b6a2305ed98b43b0bb5fc2acbf984566f1ab35db02e651e9ed8793bf64b018455872b8ae3a06af082e8d680df407ea1e5df1336a19c6f3e116c6ff1940066396afa1de5633fad814fb42790b3af0e62e6dd53977f78794b2d105cdca9272f9c0feea119fe2c9691b6f6e21db3065fb25d840acea2
我不明白为什么结果是不同的。
P.S。例如如果我使用InputBytes
字符串inputBytes = "242e35241b85fcfd75a53441ef9fc0941064c16f8e4555dabef5ce8ebc91400c6961b6b607e5dd762dbcabce51b11c8594e7d7183786c8e3c5300c7583c1871fc6f350b817682150b5cd0430ca9a2c3f8315b425c8fea0e7cc18187237ed47d29b082e7e7154888d5fb09f092a6dd5e2d3dac9df8de45837b708b5ae17f03e7f";
的结果在Java和.NET是相同的。
<$p$p><$c$c>==RESULT==01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff003020300c06082a864886f70d02050500041046fd8e86a4833e7141cbe4718e8e92f7在哪里神奇?
解决方案
我通过增加0位在inputBytes的开头解决我的问题。
I use in .Net project mono implementation of BigInteger (link) In Java I use java.math.BigInteger.
The same code produces different results in Java.
.Net code
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 code.The same inputBytes array, the same exponent and modulus, but different result.
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
I do not understand why the results are different.
P.S.e.g. if I use InputBytes
String inputBytes = "242e35241b85fcfd75a53441ef9fc0941064c16f8e4555dabef5ce8ebc91400c6961b6b607e5dd762dbcabce51b11c8594e7d7183786c8e3c5300c7583c1871fc6f350b817682150b5cd0430ca9a2c3f8315b425c8fea0e7cc18187237ed47d29b082e7e7154888d5fb09f092a6dd5e2d3dac9df8de45837b708b5ae17f03e7f";
the the results in Java and .Net are the same
==RESULT==01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff003020300c06082a864886f70d02050500041046fd8e86a4833e7141cbe4718e8e92f7
Where is the magic?
解决方案
I solved my problem by adding 0 bit at the begining of inputBytes.
这篇关于Java的BigInteger的VS单.NET的BigInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!