Java 是否可能生成BigInteger值冲突?

Java 是否可能生成BigInteger值冲突?,java,biginteger,collision,Java,Biginteger,Collision,我在用大整数表示的文件上实现一个数字签名系统。 不幸的是,由于“零长度BigInteger”异常,无法通过字节[]将空文件转换为BigInteger 因此,我用以下内容表示空文件: bigmesssage = new BigInteger ("0"); 我现在关心的是:当我在下面的程序BigIntegerTest.java中读取文件“test1”时,任何表示为BigInteger(“0”)的文件是否都可能发生值冲突 包含单个“0”的文件“test1”的程序输出结果: 非常感谢 杰瑞特 impo

我在用大整数表示的文件上实现一个数字签名系统。 不幸的是,由于“零长度BigInteger”异常,无法通过字节[]将空文件转换为BigInteger

因此,我用以下内容表示空文件:

bigmesssage = new BigInteger ("0");
我现在关心的是:当我在下面的程序BigIntegerTest.java中读取文件“test1”时,任何表示为BigInteger(“0”)的文件是否都可能发生值冲突

包含单个“0”的文件“test1”的程序输出结果:

非常感谢 杰瑞特

import java.io.*;
导入java.math.biginger;
导入java.security.NoSuchAlgorithmException;
公共类大整数测试{
/**
*@param args
*@NoSuchAlgorithmException
*/
公共静态void main(字符串[]args)抛出NoSuchAlgorithmException{
//TODO自动生成的方法存根
字节[]消息1;
BigInteger bigmessage1=null;
BigInteger bigmessage2=新的BigInteger(“0”);
System.out.println(“0:+bigmessage2”);
字符串filename=“./bin/test1”;
message1=到字节数组(文件名);
bigmessage1=新的BigInteger(message1);
System.out.println(“test1:+bigmessage1”);
}
公共静态字节[]到字节数组(字符串文件名)抛出java.security.nosuchagorithmexception{
//我最爱的人,我最爱的人,我最爱的人,我最爱的人,我最爱的人
//消息中的speichert sie als字节数组。
//lokale Variablen:
字节[]数据=null;
//MessageDigest hash=MessageDigest.getInstance(“SHA-512”);//SHA2//已删除
//溪流,在:
File textFile;//Textdatei
FileInputStream in;//Dateieingabe流
试一试{
textFile=新文件(文件名);
in=新文件输入流(textFile);
int size=(int)textFile.length();//DateIlenge
int read=0;//Anzahl der Gelesen Zeichen
数据=新字节[大小];//Lesepuffer
//达蒂酒店
while(读取<大小)
读取=+英寸读取(数据、读取、大小读取);
in.close();
//消息摘要中的消息摘要和消息中的哈希值
//hash.update(数据);//已删除
//message=hash.digest();//已删除
}//试一试
捕获(IOEX异常){
例如printStackTrace();
}
返回数据;//已添加
}//到字节数组
}

应该可以创建一个不等于任何其他
biginger
biginger

在内部,
biginger
存储一个
int signum
(表示数字的符号)和一个
int[]mag
,其中包含表示数字二进制值的字节

但是,
biginger.ZERO
包含一个
int[]mag=new int[0]
和一个
signum=0
(以确保在所有情况下0==0)。如果你制作一个特殊的
zero
包含相同的
mag
signum
为'1',你将拥有一个唯一的
biginger
,它不等于
biginger.zero
,但在数学上与
biginger.zero
完全相同

这似乎创建了一个不等于零的
biginger
,但对它进行数学运算似乎会打乱
biginger
数学

public void test() throws Exception {
    BigInteger NAN = bigNAN();
    System.out.println("NAN==ZERO " + NAN.equals(BigInteger.ZERO));
    System.out.println("NAN+0 " + NAN.add(BigInteger.ZERO));
}

public static BigInteger bigNAN() throws Exception {
    BigInteger NAN = new BigInteger(new byte[]{0});
    Field hack = BigInteger.class.getDeclaredField("signum");
    hack.setAccessible(true);
    hack.set(NAN, 1);
    return NAN;
}
印刷品

NAN==ZERO false
java.lang.ArrayIndexOutOfBoundsException: 0
...
使用该常量,它不会对任何其他“零”实例使用
=

然后可以使用
==
测试文件是否为空:

if (bigmesssage == BigInteger.ZERO)
    // the file is empty

我找不到方法来生成一个唯一的BigInteger值,该值的行为与所有其他BigInteger值类似。我正在考虑由哈希函数sha256sum计算的唯一值:

leder@leder-HP-Pavilion-dv7-Notebook-PC:~/workspace1/gmr-digital-signature-2$ sha256sum test1
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  test1
test1是空文件,并且:

leder@leder-HP-Pavilion-dv7-Notebook-PC:~/workspace1/gmr-digital-signature-2$ echo -ne '' | sha256sum 
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  -
与ASCII值^@或\x00相比:

leder@leder-HP-Pavilion-dv7-Notebook-PC:~/workspace1/BigIntegerTest/bin$ echo -ne '\x00' | sha256sum 
6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d  -
因此,我排除了我的系统中的空文件

冲突将是使用值“0”两次:对于空文件和
echo-ne'\x00'>test1a
。 从以下演示中可以看出,BigInteger计算有副作用:

import  java.io.*;
import  java.math.BigInteger;
import java.security.NoSuchAlgorithmException;


public class BigIntegerTest {



/**
 * @param args
 * @throws NoSuchAlgorithmException 
 */
public static void main(String[] args) throws NoSuchAlgorithmException {
    // TODO Auto-generated method stub

    byte[] message3;
    BigInteger bigmessage1 = null;
    BigInteger bigmessage2 = null;      
    BigInteger bigmessage3 = null;



    bigmessage1 = BigInteger.ZERO; 

    System.out.println("BigInteger.ZERO: "+bigmessage1);

    System.out.println("null: "+bigmessage2);

    String filename="./bin/test1a";

    message3 = To_Byte_Array(filename);

    bigmessage3= new BigInteger (message3);

    System.out.println("test1a: "+ bigmessage3);

    String comp = null;

    if (bigmessage1==bigmessage3) {comp="true";} else {comp="false";};

    System.out.println("bigmessage1==bigmessage3: " + comp);

    if (BigInteger.ZERO==BigInteger.ZERO) {comp="true";} else {comp="false";};

    System.out.println("BigInteger.ZERO==BigInteger.ZERO: " + comp);

}

public static byte[] To_Byte_Array (String filename) throws java.security.NoSuchAlgorithmException {
      //Liest die Nachricht, die in der Datei filename gespeichert ist, ein und
      //speichert sie als byte-Array in message.

      //lokale Variablen:
      byte[] data = null;
     // MessageDigest hash = MessageDigest.getInstance("SHA-512");//SHA2 //removed

      //Streams, in:
      File textFile;//Textdatei
      FileInputStream in;//Dateieingabe-Stream

      try {

          textFile = new File(filename);
          in = new FileInputStream(textFile);
          int size = (int)textFile.length(); // Dateilaenge
          int read = 0;    // Anzahl der gelesenen Zeichen
          data = new byte[size];      // Lesepuffer
          // Auslesen der Datei
          while (read < size)
            read =+ in.read(data, read, size-read);
          in.close();
          // Schreiben des Lesepuffers in Instanz von MessageDigest und speichern des Hash-Werts in message
          //hash.update (data);//removed
          //message=hash.digest ();//removed


      }//try
      catch (IOException ex) {
        ex.printStackTrace();
      }
      return data;//added
    }//To_Byte_Array


}

是的,一个一字节的文件和值
0
。我尝试了使用ASCII字符0的文件,该文件的值为:
test1:12298
该文件有两个字节。是的,您是对的:一个字节^@具有相同的BigInteger值。您知道如何将empy文件编码为唯一的BigInteger值吗?您的提示给了我零长度的BigInteger异常。和
BigInteger bigmessage2=null也不可能。因为我需要一个有效的BigInteger值来计算数字签名。现在我被卡住了,我想…顺便说一句,用这个命令生成了一个ASCII字符的文件:
echo-ne'\x00'>test1a
“它不等于任何其他的“零”Insrance”在大整数中不是只有一个零表示吗?@kajacx否-有许多“零”值:请参阅。另外,其他实例可能是
equals()
,但不会是
=
。我需要
bigmessage
上的完整数学。或者我现在尝试使用null指针
null
执行文件空测试…不幸的是,我需要对表示空文件的值使用BigInteger数学。。。现在,我使用空指针
null
寻找解决方案。
leder@leder-HP-Pavilion-dv7-Notebook-PC:~/workspace1/BigIntegerTest/bin$ echo -ne '\x00' | sha256sum 
6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d  -
import  java.io.*;
import  java.math.BigInteger;
import java.security.NoSuchAlgorithmException;


public class BigIntegerTest {



/**
 * @param args
 * @throws NoSuchAlgorithmException 
 */
public static void main(String[] args) throws NoSuchAlgorithmException {
    // TODO Auto-generated method stub

    byte[] message3;
    BigInteger bigmessage1 = null;
    BigInteger bigmessage2 = null;      
    BigInteger bigmessage3 = null;



    bigmessage1 = BigInteger.ZERO; 

    System.out.println("BigInteger.ZERO: "+bigmessage1);

    System.out.println("null: "+bigmessage2);

    String filename="./bin/test1a";

    message3 = To_Byte_Array(filename);

    bigmessage3= new BigInteger (message3);

    System.out.println("test1a: "+ bigmessage3);

    String comp = null;

    if (bigmessage1==bigmessage3) {comp="true";} else {comp="false";};

    System.out.println("bigmessage1==bigmessage3: " + comp);

    if (BigInteger.ZERO==BigInteger.ZERO) {comp="true";} else {comp="false";};

    System.out.println("BigInteger.ZERO==BigInteger.ZERO: " + comp);

}

public static byte[] To_Byte_Array (String filename) throws java.security.NoSuchAlgorithmException {
      //Liest die Nachricht, die in der Datei filename gespeichert ist, ein und
      //speichert sie als byte-Array in message.

      //lokale Variablen:
      byte[] data = null;
     // MessageDigest hash = MessageDigest.getInstance("SHA-512");//SHA2 //removed

      //Streams, in:
      File textFile;//Textdatei
      FileInputStream in;//Dateieingabe-Stream

      try {

          textFile = new File(filename);
          in = new FileInputStream(textFile);
          int size = (int)textFile.length(); // Dateilaenge
          int read = 0;    // Anzahl der gelesenen Zeichen
          data = new byte[size];      // Lesepuffer
          // Auslesen der Datei
          while (read < size)
            read =+ in.read(data, read, size-read);
          in.close();
          // Schreiben des Lesepuffers in Instanz von MessageDigest und speichern des Hash-Werts in message
          //hash.update (data);//removed
          //message=hash.digest ();//removed


      }//try
      catch (IOException ex) {
        ex.printStackTrace();
      }
      return data;//added
    }//To_Byte_Array


}
BigInteger.ZERO: 0
null: null
test1a: 0
bigmessage1==bigmessage3: false
BigInteger.ZERO==BigInteger.ZERO: true